CHAPTER 5: Joining Tables Flashcards
Q: What is the purpose of joining tables in T-SQL?
A: To combine relational data stored in multiple tables and present it as a single result set.
Q: What type of relationship typically exists between two tables when they are joined?
A: A parent-child relationship, where the parent table has a primary key, and the child table has a foreign key referring to the parent.
Q: Which type of join is most commonly used in T-SQL?
A: INNER JOIN.
Q: What does an INNER JOIN do?
A: It returns only the rows that match in both tables based on the join condition.
Q: Provide the basic syntax for an INNER JOIN.
SELECT <columns>
FROM <table1>
[INNER] JOIN <table2> ON <table1>.<col1> = <table2>.<col2>;</col2></table2></col1></table1></table2></table1></columns>
Q: What happens if a row in the parent table does not have a matching row in the child table in an INNER JOIN?
A: The row from the parent table will not appear in the result set.
Q: Why is it necessary to fully qualify column names in a join?
A: To avoid ambiguity when columns with the same name exist in both tables.
Q: Write an example query to join Sales.SalesOrderHeader and Sales.SalesOrderDetail tables.
SELECT s.SalesOrderID, s.OrderDate, s.TotalDue,
d.SalesOrderDetailID, d.ProductID, d.OrderQty
FROM Sales.SalesOrderHeader AS s
INNER JOIN Sales.SalesOrderDetail AS d
ON s.SalesOrderID = d.SalesOrderID;
Q: Why should you use table aliases in a query with joins?
A: To save typing and make the query more readable, especially when fully qualifying column names.
Q: Why is using meaningful aliases for tables recommended over generic ones like “A” or “B”?
A: Meaningful aliases improve query readability and help identify the source of columns quickly.
Q: What happens if you alias tables in a query?
A: The alias must be used to qualify column names instead of the full table name.
Q: What is a composite primary key, and how is it used in joins?
A: A composite primary key consists of two or more columns that uniquely identify a row. It can be used as part of a join condition to match related rows in another table.
Q: What is the primary factor that affects the performance of an INNER JOIN?
A: Indexes on the columns used in the join condition can significantly improve performance by enabling index seeks instead of scans.
Q: What happens if the ON condition in a join always evaluates to TRUE, such as ON 1 = 1?
A: It creates a Cartesian product, where every row from the first table is joined with every row from the second table, resulting in an excessive and nonsensical number of rows.
Q: What is a Cartesian product in SQL?
A: The result of an incorrect join condition where all rows from one table are combined with all rows from another table.
Q: How can you avoid incorrect join conditions?
A: Ensure the ON clause correctly matches columns based on the relationship between the tables, such as matching foreign keys to primary keys.
Q: Can you join tables if the columns have different names?
A: Yes, as long as the columns have compatible data types and a logical relationship.
Q: Write an example of joining two tables with different column names.
SELECT c.CustomerID, c.PersonID, p.BusinessEntityID, p.LastName
FROM Sales.Customer AS c
INNER JOIN Person.Person AS p
ON c.PersonID = p.BusinessEntityID;
Q: What is a composite primary key?
A: A primary key composed of multiple columns, requiring all the columns in the key to uniquely identify a row.
Q: How do you join tables on multiple columns?
A: Use the AND operator to include all the necessary column comparisons in the ON clause.
Example:
SELECT sod.SalesOrderID, sod.SalesOrderDetailID,
so.ProductID, so.SpecialOfferID
FROM Sales.SalesOrderDetail AS sod
INNER JOIN Sales.SpecialOfferProduct AS so
ON so.ProductID = sod.ProductID
AND so.SpecialOfferID = sod.SpecialOfferID;
Q: What happens if you omit one column in a multi-column join condition?
A: The query may produce incorrect results, showing rows that partially match but do not fully meet the intended relationship.
Q: Why is understanding the relationship between tables critical when writing joins?
A: To ensure that the join condition accurately reflects the logical connections between the tables and avoids incorrect or nonsensical results.
Q: Why should you test queries with incomplete join conditions?
A: To observe the impact of the missing condition and better understand the importance of writing accurate joins.
Q: When would you need to join three or more tables?
A: When dealing with relationships like many-to-many or when data in one table indirectly connects two other tables, requiring all three for a complete result set.
Q: Provide the syntax for joining three tables in T-SQL.
SELECT <select>
FROM <table1>
[INNER] JOIN <table2> ON <table1>.<col1> = <table2>.<col2>
[INNER] JOIN <table3> ON <table2>.<col2> = <table3>.<col3>;</col3></table3></col2></table2></table3></col2></table2></col1></table1></table2></table1></select>
Q: Write a query to join Sales.SalesOrderHeader, Sales.SalesOrderDetail, and Production.Product tables to display sales order IDs, order dates, product IDs, and product names.
SELECT soh.SalesOrderID, soh.OrderDate, p.ProductID, p.Name
FROM Sales.SalesOrderHeader AS soh
INNER JOIN Sales.SalesOrderDetail AS sod ON soh.SalesOrderID = sod.SalesOrderID
INNER JOIN Production.Product AS p ON sod.ProductID = p.ProductID
ORDER BY soh.SalesOrderID;
Q: In a three-table join, why might some columns not appear in the final SELECT list?
A: The table serves as a bridge to connect the other two tables, and its columns may not be needed in the final output.
Q: What approach can help if you have trouble figuring out how to join multiple tables?
A: Start by joining two tables and verify the results, then add the third table to refine the query step-by-step.
Q: Why is it important to carefully choose join columns, especially when joining multiple tables?
A: Incorrect joins can lead to invalid results or Cartesian products, making the data meaningless.
Q: How should you handle join columns with different names across tables?
A: Use fully qualified column names in the ON clause to specify the correct relationship.
Q: How does a many-to-many relationship usually connect tables?
A: Through a junction table that acts as a bridge between two main tables.
Q: What is a critical skill for T-SQL developers when working with relational databases?
A: Understanding and writing queries that effectively join multiple tables to produce accurate and meaningful results.
Q: What is the purpose of OUTER JOIN in SQL?
A: OUTER JOIN retrieves all rows from one table and matching rows from another table, including unmatched rows with NULL values for non-matching columns.
Q: What are the types of OUTER JOIN?
A: The types are LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.
Q: What does LEFT OUTER JOIN do?
A: It returns all rows from the left table and matching rows from the right table. If there is no match, the right table’s columns are filled with NULL.
Q: Provide the syntax for LEFT OUTER JOIN.
SELECT <select>
FROM <table1>
LEFT [OUTER] JOIN <table2> ON <table1>.<col1> = <table2>.<col2>;</col2></table2></col1></table1></table2></table1></select>
Q: Write a query to list all customers and their orders, including customers with no orders, using LEFT OUTER JOIN.
SELECT c.CustomerID, s.SalesOrderID, s.OrderDate
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader AS s ON c.CustomerID = s.CustomerID
WHERE c.CustomerID IN (11028,11029,1,2,3,4);
Q: What happens when a row from the left table has no match in the right table in LEFT OUTER JOIN?
A: The columns from the right table for that row will contain NULL values.
Q: What does RIGHT OUTER JOIN do?
A: It returns all rows from the right table and matching rows from the left table. If there is no match, the left table’s columns are filled with NULL.