Skill 1.2 Query multiple tables by using joins Flashcards
What types of joins does T-SQL support?
cross
inner
outer
What is the output of a cross join?
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.
What is an equijoin?
A join using a predicate with an equality operator
What is the output of an inner join?
An inner join returns a combination of rows from each table where the predicate evaluates to true.
Where is the inner join’s matching predicate specified?
In the ON clause
What is the functional difference between the ON and the WHERE clause in a query using an INNER JOIN?
There is no difference. Both clauses serve to filter rows and only return those where the predicate evaluates to true.
What is the output of an OUTER JOIN?
An outer join preserves all rows from one or both sides of the join.
In a LEFT OUTER JOIN, what is used as a placeholder for rows where the right side has no match?
NULLs
What is the difference between a LEFT OUTER JOIN and a RIGHT OUTER JOIN?
A left outer join preserves all rows from the left table. A right outer join preserves all rows from the right table.
What is the functional difference between the ON and the WHERE clause in a query using an OUTER JOIN?
In an outer join, the WHERE clause performs a filtering function (FINAL), and the ON clause performs a matching function (NONFINAL).
What is the output of a FULL OUTER JOIN?
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.
What is a composite join?
A composite join is a join between two tables based on multiple columns.
What is a simple way to write a predicate in a join where values can be NULL?
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)
)
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?
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)
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?
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)
)