CHAPTER 5: Joining Tables Flashcards
(102 cards)
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.