CHAPTER 5: Joining Tables Flashcards

(102 cards)

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: 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.
24
Q: Provide the syntax for joining three tables in T-SQL.
SELECT FROM LEFT [OUTER] JOIN ON . = .;
36
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);
37
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.
38
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.
39
40
40
41
42
Q: Provide the syntax for RIGHT OUTER JOIN.
SELECT