Chapter 4: Combining Sets Flashcards

1
Q

Does a Cross Join affect the logical query processing?

A

Yes, according to the main logical query processing, the FROM clause is evaluated first that the WHERE clause. But SQL Server Optimizer says let’s evaluate first the WHERE clause to filter the joined tables, and then let’s evaluate the FROM clause doing the MATCH in the ON clause.

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

What is a Inner Join?

A

Match rows from two tables based on a predicate, usally one that compares a primary key value in one side to a foreign key value in another side.

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

What is a Outer Join?

A

With outer joins, you can request to preserve all rows from one or both sides of the join, never
mind if there are matching rows in the other side based on the ON predicate.

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

What happens in a LEFT or RIGHT JOIN when doesn’t exist a match between two tables?

A

If it is a LEFT JOIN, the column values for the right table will be placeholded with NULL. In case of a RIGHT JOIN, the column values for the left table will be placeholded with NULL.

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

In a OUTER JOIN, what means a preserved side?

A

If we indicate a LEFT JOIN, the preserved side will be the left table, eg. “T1 LEFT JOIN T2”, T1 will be the preserved table. In a RIGHT JOIN, the preserved side will be the right table. Also, this means if the predicate which appears on the ON clause returns false or unknown with a specific row, the column values from the row of preserved side will be returned.

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

What is the principal difference between ON clause and the WHERE clause?

A

The ON clause just appear on the JOIN operations, so it is used for matching purpose. The WHERE clause is used for filtering purpose.

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

What is a FULL OUTER JOIN?

A

It’s a JOIN operation that preserves both tables.

A full outer join returns the inner rows that are normally returned from an inner join; plus
rows from the left that don’t have matches in the right, with NULLs used as placeholders in
the right side; plus rows from the right that don’t have matches in the left, with NULLs used as
placeholders in the left side.

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

What is the difference between the old and new syntax for cross joins?

A

The new syntax has the CROSS JOIN keywords between the table names and
the old syntax has a comma.

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

What are the different types of outer joins?

A

Left, right, and full.

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

What is a self contained subquery?

A

It’s a inner query that doesn’t depends to the outer query. It can runs independently.

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

What is a correlated subquery?

A

Correlated subqueries are subqueries where the inner query has a reference to a column from
the table in the outer query. They are trickier to work with compared to self-contained subqueries
because you can’t just highlight the inner portion and run it independently.

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

What is a table expression?

A

It is a named query.

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

What are the forms of table expressions?

A

(1) Derived tables.
(2) Common Table Expression (CTE).
(3) Views.
(4) Inline table-valued functions.

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

What is a derived table?

A

It is an inner query in the FROM clause. For example, SELECT * FROM (SELECT * FROM TABLE). The inner query is a derived table.

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

What is a Common Table Expression (CTE)?

A

A common table expression (CTE) is a similar concept to a derived table in the sense that it’s
a named table expression that is visible only to the statement that defines it. Like a query
against a derived table, a query against a CTE involves three main parts:

(1) The inner query.
(2) The name you assign to the query and its columns.
(3) The outer query.

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

How a CTE recursived can be achieved?

A

NOTE: Review this

17
Q

What’s the difference between the table expressions like derived tables and CTE and the table expressions like Views and Inline Table-Valued Functions?

A

Derived Tables and CTE aren’t defined in the database as objects, whereas Views and Inline Table - Valued Functions are stored in the database as objects, making both reusable.

18
Q

What’s the difference between Views and Inline Table Valued Functions?

A

The former doesn’t use input parameters, whereas the latter use input parameters (it’s like a function).

19
Q

What’s the APPLY operator?

A

The APPLY operator is a powerful operator that you can use to apply a table expression given
to it as the right input to each row from a table expression given to it as the left input. What’s
interesting about the APPLY operator as compared to a join is that the right table expression
can be correlated to the left table; in other words, the inner query in the right table expression
can have a reference to an element from the left table. So conceptually, the right table expression is evaluated separately for each left row. This means that you can replace the use of cursors in some cases with the APPLY operator.

20
Q

What’s the difference between a CROSS APPLY and an OUTER APPLY?

A

The CROSS APPLY operator return left rows only for which the right table expression doesn’t return an empty set, whereas OUTER APPLY returns also left rows for those rows which the right table expression return an empty set.

21
Q

What is the difference between self-contained and correlated subqueries?

A

Self-contained subqueries are independent of the outer query, whereas correlated subqueries have a reference to an element from the table in the outer
query.

22
Q

What is the difference between the APPLY and JOIN operators?

A

With a JOIN operator, both inputs represent static relations. With APPLY, the left side is a static relation, but the right side can be a table expression with
correlations to elements from the left table.

23
Q

What are the guidelines to use set operators?

A
  • . The number of columns in the queries has to be the same and the column types of corresponding columns need to be compatible (implicitly convertible).
  • . Set operators consider two NULLs as equal for the purpose of comparison. This is quite unusual when compared to filtering clauses like WHERE and ON.
  • . Just an ORDER BY clause it’s accepted and it needs to be in the last query. An individual query cannot have a ORDER BY clause.
  • . The column names of result columns are determined by the first query.
24
Q

What are the different set operators?

A

UNION (UNION ALL also), INTERSECT and EXCEPT.

25
Q

What is the difference between an UNION and an UNION ALL?

A

UNION removes duplicates, whereas UNION ALL doesn’t remove duplicates.

26
Q

Give a brief of set operators.

A
  • . The UNION operator unifies the input sets, returning distinct rows.
  • . The UNION ALL operator unifies the inputs without eliminating duplicates.
  • . The INTERSECT operator returns only rows that appear in both input sets, returning distinct rows.
  • . The EXCEPT operator returns the rows that appear in the first set but not the second, returning distinct rows.
27
Q

What are the table operators which support T SQL?

A

JOIN (Standard), PIVOT (No standard), UNPIVOT (No standard) and APPLY (No standard)

28
Q

What’s the rule of precedence for set operators?

A

INTERSECT is the first operation that is achieved, then UNION and EXCEPT are achieved from left to right.