CHAPTER 5: Joining Tables Flashcards

1
Q

Q: What is the purpose of joining tables in T-SQL?

A

A: To combine relational data stored in multiple tables and present it as a single result set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: What type of relationship typically exists between two tables when they are joined?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: Which type of join is most commonly used in T-SQL?

A

A: INNER JOIN.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: What does an INNER JOIN do?

A

A: It returns only the rows that match in both tables based on the join condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: Provide the basic syntax for an INNER JOIN.

A

SELECT <columns>
FROM <table1>
[INNER] JOIN <table2> ON <table1>.<col1> = <table2>.<col2>;</col2></table2></col1></table1></table2></table1></columns>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A: The row from the parent table will not appear in the result set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: Why is it necessary to fully qualify column names in a join?

A

A: To avoid ambiguity when columns with the same name exist in both tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: Write an example query to join Sales.SalesOrderHeader and Sales.SalesOrderDetail tables.

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: Why should you use table aliases in a query with joins?

A

A: To save typing and make the query more readable, especially when fully qualifying column names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: Why is using meaningful aliases for tables recommended over generic ones like “A” or “B”?

A

A: Meaningful aliases improve query readability and help identify the source of columns quickly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: What happens if you alias tables in a query?

A

A: The alias must be used to qualify column names instead of the full table name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: What is a composite primary key, and how is it used in joins?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: What is the primary factor that affects the performance of an INNER JOIN?

A

A: Indexes on the columns used in the join condition can significantly improve performance by enabling index seeks instead of scans.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: What happens if the ON condition in a join always evaluates to TRUE, such as ON 1 = 1?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: What is a Cartesian product in SQL?

A

A: The result of an incorrect join condition where all rows from one table are combined with all rows from another table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: How can you avoid incorrect join conditions?

A

A: Ensure the ON clause correctly matches columns based on the relationship between the tables, such as matching foreign keys to primary keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: Can you join tables if the columns have different names?

A

A: Yes, as long as the columns have compatible data types and a logical relationship.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: Write an example of joining two tables with different column names.

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q: What is a composite primary key?

A

A: A primary key composed of multiple columns, requiring all the columns in the key to uniquely identify a row.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Q: How do you join tables on multiple columns?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Q: What happens if you omit one column in a multi-column join condition?

A

A: The query may produce incorrect results, showing rows that partially match but do not fully meet the intended relationship.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Q: Why is understanding the relationship between tables critical when writing joins?

A

A: To ensure that the join condition accurately reflects the logical connections between the tables and avoids incorrect or nonsensical results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Q: Why should you test queries with incomplete join conditions?

A

A: To observe the impact of the missing condition and better understand the importance of writing accurate joins.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Q: When would you need to join three or more tables?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Q: Provide the syntax for joining three tables in T-SQL.

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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.

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Q: In a three-table join, why might some columns not appear in the final SELECT list?

A

A: The table serves as a bridge to connect the other two tables, and its columns may not be needed in the final output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Q: What approach can help if you have trouble figuring out how to join multiple tables?

A

A: Start by joining two tables and verify the results, then add the third table to refine the query step-by-step.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Q: Why is it important to carefully choose join columns, especially when joining multiple tables?

A

A: Incorrect joins can lead to invalid results or Cartesian products, making the data meaningless.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Q: How should you handle join columns with different names across tables?

A

A: Use fully qualified column names in the ON clause to specify the correct relationship.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Q: How does a many-to-many relationship usually connect tables?

A

A: Through a junction table that acts as a bridge between two main tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Q: What is a critical skill for T-SQL developers when working with relational databases?

A

A: Understanding and writing queries that effectively join multiple tables to produce accurate and meaningful results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Q: What is the purpose of OUTER JOIN in SQL?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Q: What are the types of OUTER JOIN?

A

A: The types are LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Q: What does LEFT OUTER JOIN do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Q: Provide the syntax for LEFT OUTER JOIN.

A

SELECT <select>
FROM <table1>
LEFT [OUTER] JOIN <table2> ON <table1>.<col1> = <table2>.<col2>;</col2></table2></col1></table1></table2></table1></select>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Q: Write a query to list all customers and their orders, including customers with no orders, using LEFT OUTER JOIN.

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Q: What happens when a row from the left table has no match in the right table in LEFT OUTER JOIN?

A

A: The columns from the right table for that row will contain NULL values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Q: What does RIGHT OUTER JOIN do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q
A
40
Q
A
40
Q
A
41
Q
A
42
Q

Q: Provide the syntax for RIGHT OUTER JOIN.

A

SELECT <select>
FROM <table2>
RIGHT [OUTER] JOIN <table1> ON <table1>.<col1> = <table2>.<col2>;</col2></table2></col1></table1></table1></table2></select>

42
Q

Q: Write a query to list all customers and their orders, including customers with no orders, using RIGHT OUTER JOIN.

A

SELECT c.CustomerID, s.SalesOrderID, s.OrderDate
FROM Sales.SalesOrderHeader AS s
RIGHT OUTER JOIN Sales.Customer AS c ON c.CustomerID = s.CustomerID
WHERE c.CustomerID IN (11028,11029,1,2,3,4);

43
Q

Q: Why is LEFT OUTER JOIN generally preferred over RIGHT OUTER JOIN?

A

A: Because using LEFT OUTER JOIN keeps the main table consistent as the leftmost table, making queries easier to understand and maintain when joining multiple tables.

44
Q

Q: What is the difference between LEFT OUTER JOIN and RIGHT OUTER JOIN?

A

A: LEFT OUTER JOIN returns all rows from the left table, while RIGHT OUTER JOIN returns all rows from the right table.

45
Q

Q: What do LEFT OUTER JOIN and RIGHT OUTER JOIN have in common?

A

A: Both return all rows from one table and matching rows from the other table, filling unmatched columns with NULL.

46
Q

Q: How can you use LEFT OUTER JOIN to find rows with no match in another table?

A

A: By adding a WHERE clause that checks for NULL in the columns of the right-side table.
Example:

SELECT c.CustomerID
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader AS s ON c.CustomerID = s.CustomerID
WHERE s.SalesOrderID IS NULL;

47
Q

Q: What does the LEFT OUTER JOIN return when there is no matching row in the right table?

A

A: It returns all rows from the left table, with NULL in the columns from the right table for nonmatching rows.

48
Q

Q: Write the syntax for a query using LEFT OUTER JOIN to find rows without a match.

A

SELECT <columns>
FROM <left_table>
LEFT OUTER JOIN <right_table> ON <condition>
WHERE <right_table_column> IS NULL;</right_table_column></condition></right_table></left_table></columns>

49
Q

Q: How do you join three tables with LEFT OUTER JOIN while ensuring unmatched rows remain in the results?

A

A: Use LEFT OUTER JOIN between all table pairs and ensure the leftmost table includes all rows.
Example:

SELECT C.CustomerID, SOH.SalesOrderID, SOD.SalesOrderDetailID
FROM Sales.Customer AS C
LEFT OUTER JOIN Sales.SalesOrderHeader AS SOH
ON C.CustomerID = SOH.CustomerID
LEFT OUTER JOIN Sales.SalesOrderDetail AS SOD
ON SOH.SalesOrderID = SOD.SalesOrderID;

49
Q
A
50
Q
A
51
Q
A
52
Q
A
53
Q

Q: Why must you continue to use LEFT OUTER JOIN when adding more tables to the query?

A

A: To ensure that unmatched rows from the primary (leftmost) table persist in the result set.

53
Q

Q: What happens if you change a LEFT OUTER JOIN to INNER JOIN in a multi-table query?

A

A: Rows with NULL in the join condition will be excluded, potentially removing unmatched rows from the result set.

54
Q

Q: What is a practical use case for finding rows with no match using LEFT OUTER JOIN?

A

A: To find customers who have not placed any orders.

54
Q

Q: When joining multiple tables, why is it recommended to use LEFT OUTER JOIN consistently?

A

A: It simplifies the query structure and avoids switching between LEFT and RIGHT joins, which can be confusing.

55
Q
A
56
Q
A
57
Q

Q: What should you consider when writing queries with LEFT OUTER JOIN?

A

A: Ensure the main table is on the left, fully qualify column names, and use WHERE clauses carefully to avoid inadvertently excluding rows.

57
Q

Q: What does the following query do?

SELECT c.CustomerID, s.SalesOrderID, s.OrderDate
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader AS s ON c.CustomerID = s.CustomerID
LEFT OUTER JOIN Sales.SalesOrderDetail AS sod ON s.SalesOrderID = sod.SalesOrderID;

A

A: It retrieves all customers, their orders (if any), and the details of those orders. Customers without orders still appear with NULL values in the order and detail columns.

58
Q

Q: When adding a table to the main table of a LEFT JOIN, which type of join should you use to avoid losing rows?

A

A: Use LEFT OUTER JOIN to ensure that no rows from the main table are lost, even if there are no matching rows in the new table.

59
Q

Q: Why might it be safe to use INNER JOIN instead of LEFT OUTER JOIN in certain cases?

A

A: If you know that every row in the main table has a corresponding match in the new table (e.g., customers must always have a territory), then INNER JOIN can safely be used.

60
Q

Q: Write a query to add a Territory table to a LEFT JOIN query involving Customer, SalesOrderHeader, and SalesOrderDetail.

A

SELECT C.CustomerID, SOH.SalesOrderID, SOD.SalesOrderDetailID,
SOD.ProductID, T.Name
FROM Sales.Customer AS C
LEFT OUTER JOIN Sales.SalesOrderHeader AS SOH
ON C.CustomerID = SOH.CustomerID
LEFT OUTER JOIN Sales.SalesOrderDetail AS SOD
ON SOH.SalesOrderID = SOD.SalesOrderID
LEFT OUTER JOIN Sales.SalesTerritory AS T
ON C.TerritoryID = T.TerritoryID
WHERE C.CustomerID IN (11028,11029,1,2,3,4);

61
Q

Q: What does FULL OUTER JOIN return?

A

It returns all rows from both tables:

Rows with matches are returned as one combined row.
Rows from the left table with no match in the right table have NULL values in the right table’s columns.
Rows from the right table with no match in the left table have NULL values in the left table’s columns.

62
Q

Q: Write the syntax for a FULL OUTER JOIN.

A

SELECT <column>
FROM <table1>
FULL [OUTER] JOIN <table2> ON <table1>.<col1> = <table2>.<col2>;</col2></table2></col1></table1></table2></table1></column>

63
Q

Q: Provide an example of a FULL OUTER JOIN query using a Product table and a ProductColor table.

A

SELECT c.Color AS “Color from list”, p.Color, p.ProductID
FROM Production.Product AS p
FULL OUTER JOIN Production.ProductColor AS c ON p.Color = c.Color
ORDER BY p.ProductID;

64
Q

Q: What is a practical use of FULL OUTER JOIN?

A

A: It is useful for finding data discrepancies, such as values in one table that do not exist in another, to clean up data before loading it into a production system or data warehouse.

65
Q

Q: What happens when there is no match in a FULL OUTER JOIN for a row in one of the tables?

A

For unmatched rows from the left table, columns from the right table will contain NULL.

For unmatched rows from the right table, columns from the left table will contain NULL.

66
Q

Q: What does a CROSS JOIN do?

A

A: A CROSS JOIN combines every row from one table with every row from another table, creating a Cartesian product.

67
Q

Q: Write the syntax for a CROSS JOIN.

A

SELECT <columns>
FROM <table1>
CROSS JOIN <table2>;</table2></table1></columns>

67
Q

Q: When is CROSS JOIN typically used?

A

A: It is used when creating all possible combinations of rows, such as generating inventory worksheets or matching every product with every location.

68
Q

Q: Provide an example of a CROSS JOIN query using Product and Location.

A

SELECT p.ProductID, l.LocationID
FROM Production.Product AS p
CROSS JOIN Production.Location AS l
ORDER BY ProductID;

69
Q
A
70
Q

Q: What is a potential risk of using a CROSS JOIN unintentionally?

A

A: It can generate an extremely large number of rows, consuming significant resources and potentially causing the query to hang.

71
Q

Q: What is a self-join?

A

A: A self-join is a query where a table is joined to itself, often to represent hierarchical or unary relationships.

72
Q

Q: Why do we need to use aliases in a self-join?

A

A: Aliases are required because the same table is used twice in the query, and SQL does not allow two identical table names in a single query.

73
Q

Q: Write the syntax for a self-join using LEFT OUTER JOIN.

A

SELECT a.col1, b.col2
FROM <table> AS a
LEFT OUTER JOIN <table> AS b ON a.<col> = b.<col>;

74
Q

Q: What is an example use case of a self-join?

A

A: A self-join is often used to represent manager-employee relationships where both managers and employees are stored in the same table.

75
Q

Q: Provide a self-join example to show employee-manager relationships.

A

SELECT a.EmployeeID AS Employee,
a.Title AS EmployeeTitle,
b.EmployeeID AS ManagerID,
b.Title AS ManagerTitle
FROM #Employee AS a
LEFT OUTER JOIN #Employee AS b ON a.ManagerID = b.EmployeeID;

76
Q

Q: Why is LEFT OUTER JOIN used in a self-join for hierarchical data?

A

A: It ensures that rows with no matching parent (e.g., a CEO with no manager) are still included in the results.

77
Q

Q: What is a Merge Join in SQL Server?

A

A: A Merge Join is a physical operator used when both inputs are sorted on the join key and the optimizer estimates a relatively large number of rows from each side.

78
Q

Q: When is a Merge Join preferred?

A

A: It is preferred when both inputs are sorted by the join key, such as when the columns used in the join have clustered or covering indexes.

79
Q

Q: What is the key benefit of a Merge Join?

A

A: It is highly efficient because it processes sorted data in a single pass for each side, minimizing comparisons.

80
Q

Q: What is a drawback of a Merge Join?

A

A: It requires both inputs to be sorted, which can be a costly operation if no suitable indexes exist.

81
Q

Q: What is a Nested Loop Join?

A

A: A Nested Loop Join is a physical operator where the database engine iterates through rows in one input (usually smaller) and searches for matches in the other input.

82
Q
A
83
Q
A
84
Q

Q: What are the advantages of a Nested Loop Join?

A

A: It is efficient for small datasets and works well when indexes exist on the larger input.

84
Q

Q: When does SQL Server use a Nested Loop Join?

A

A: It is used when one side of the join has a small number of rows, regardless of whether the data is sorted.

85
Q

Q: What are the disadvantages of a Nested Loop Join?

A

A: It becomes inefficient when the smaller input grows larger, as the cost increases with the number of rows in the smaller input.

86
Q

Q: What is a Hash Match Join?

A

A: A Hash Match Join uses hash tables to join two inputs. It creates hash tables for one input and probes them with the second input to find matches.

87
Q

Q: When is a Hash Match Join used?

A

A: It is used when inputs are not sorted, and the optimizer estimates that a large number of rows will be returned from both sides.

88
Q

Q: What are the benefits of a Hash Match Join?

A

A: It handles unsorted data efficiently and is useful for joining large datasets when sorting is not feasible.

89
Q

Q: What are the limitations of a Hash Match Join?

A

A: It uses more memory and CPU resources than a Merge Join and can be slower if the hash table spills to disk.

90
Q

Q: How can indexes affect join performance?

A

A: Proper indexes (especially on foreign keys) can reduce the cost of joins by enabling efficient seeks or sorted inputs for Merge Joins.

91
Q

Q: How does SQL Server choose the join operator?

A

A: The optimizer selects the best join operator (Merge, Nested Loop, or Hash) based on input sizes, indexes, sorting, and estimated row counts.

92
Q

Q: What can you do to optimize joins?

A

A: Create appropriate indexes, avoid unnecessary columns in SELECT, and ensure join conditions use indexed columns when possible.