Skill 1.2 Query multiple tables by using joins Flashcards

1
Q

What types of joins does T-SQL support?

A

cross
inner
outer

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

What is the output of a cross join?

A

A cross join yields a multiplication between the tables. The output will have a number of rows equal to the number of rows in the first table multiplied by the number of rows in the second table, giving every possible combination of rows from the two tables.
This is known as a cartesian product.

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

What is an equijoin?

A

A join using a predicate with an equality operator

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

What is the output of an inner join?

A

An inner join returns a combination of rows from each table where the predicate evaluates to true.

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

Where is the inner join’s matching predicate specified?

A

In the ON clause

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

What is the functional difference between the ON and the WHERE clause in a query using an INNER JOIN?

A

There is no difference. Both clauses serve to filter rows and only return those where the predicate evaluates to true.

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

What is the output of an OUTER JOIN?

A

An outer join preserves all rows from one or both sides of the join.

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

In a LEFT OUTER JOIN, what is used as a placeholder for rows where the right side has no match?

A

NULLs

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

What is the difference between a LEFT OUTER JOIN and a RIGHT OUTER JOIN?

A

A left outer join preserves all rows from the left table. A right outer join preserves all rows from the right table.

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

What is the functional difference between the ON and the WHERE clause in a query using an OUTER JOIN?

A

In an outer join, the WHERE clause performs a filtering function (FINAL), and the ON clause performs a matching function (NONFINAL).

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

What is the output of a FULL OUTER JOIN?

A

A full outer join returns the matched rows (as an inner join would), plus all unmatched rows from the left with NULLs as placeholders on the right, and plus all unmatched rows from the right with NULLs as placeholders on the left.

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

What is a composite join?

A

A composite join is a join between two tables based on multiple columns.

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

What is a simple way to write a predicate in a join where values can be NULL?

A

Use ISNULL or COALESCE to substitute a value that can’t normally appear in the data on both sides
select * from table1 INNER JOIN table2
ON (
COALESCE(table1.col,-999) = COALESCE(table2.col,-999)
)

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

Why is it not a good idea to simply use ISNULL or COALESCE to substitute a value that can’t normally appear in the data on both sides when writing a predicate in a join where values can be NULL?

A

Since the column values have been manipulated, SQL Server cannot trust that the values preserve the ordering behavior of the original values, and may not be able to use an index to seek matching rows (will have to scan instead)

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

What’s a better option than to simply use ISNULL or COALESCE to substitute a value that can’t normally appear in the data on both sides when writing a predicate in a join where values can be NULL?

A

Switch the comparison to a three-part comparison:
select * from table1 INNER JOIN table2
ON (
table1.col = table2.col
OR
(table1.col IS NULL and table2.col IS NULL)
)

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

Can you use a set operator in the ON clause of a join?

A
Yes.  You could write something like:
select * from table1 INNER JOIN table2
ON EXISTS
(
SELECT table1.col
INTERSECT
SELECT table2.col
)
17
Q

In a multi-join query, are the joins evaluated at the same time?

A

No. joins are evaluated conceptually from left to right. The ON clause ordering is what defines the logical ordering.

18
Q

Do you have to use parentheses to force SQL Server to evaluate joins in a certain order

A

No. SQL Server looks at the ON clause ordering. Parentheses may make things clearer visually, but are not required.