UNION, UNION ALL, EXCEPT, INTERSECT Flashcards

1
Q

How does UNION work?

A

It returns all rows from the two queries, discarding duplicates.

SQL Server 70-461 04-02b to 04-03

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

What is the general format of two queries using UNION?

A
<qry1 Select Statement>
UNION
<qry2 Select Statement>

Just making a sentence long enough so the code will left justify for this flash card.

SQL Server 70-461 04-02b to 04-03

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

How is UNION ALL different from UNION?

A

It doesn’t elminate duplicates

SQL Server 70-461 04-02b to 04-03

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

If UNION and UNION ALL will return the same results, i.e. there are no duplicates, which should you use?

A

UNION ALL. UNION may attempt to eliminate duplicates, even though there aren’t any, and it could cause a performance issue.

SQL Server 70-461 04-02b to 04-03

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

How does INTERSECT work?

A

It returns only distinct rows that are common to both sets. If a row appears at least one time in the first set and one time in the second set it will be included one time in the result of the INTERSECT.

SQL Server 70-461 04-02b to 04-03

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

What is the general form of two queries using INTERSECT?

A
<qry1 Select Statement>
INTERSECT
<qry2 Select Statement>

Just making a sentence long enough so the code will left justify for this flash card.

SQL Server 70-461 04-02b to 04-03

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

What is the general form of two queries using EXCEPT?

A
<qry1 Select Statement>
EXCEPT
<qry2 Select Statement>

Just making a sentence long enough so the code will left justify for this flash card.

SQL Server 70-461 04-02b to 04-03

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

How would <qry1> UNION <qry2> INTERSECT <qry3> be evaluated?

A
  • The intersection between qry2 and qry3 happens first.
  • Next, a union between the result of the intersection and qry1 takes place.

SQL Server 70-461 04-02b to 04-03

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

What can you do to force precendence the way you want it to be for joins involving more than one set operator?

A
  • Use Parentheses
  • From previous example:
    (<qry1> UNION <qry2>)
    INTERSECT <qry3>

SQL Server 70-461 04-02b to 04-03

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

What are the three types of set operators?

A
  1. UNION
  2. INTERSECT
  3. EXCEPT

SQL Server 70-461 04-02b to 04-03

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

What is the multi-set operator?

A

UNION ALL

SQL Server 70-461 04-02b to 04-03

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

How does EXCEPT work?

A
  • It returns distinct rows that appear in the first query but not in the second.
  • If a row occurs at least once in the first query and zero times in the second, it’s returned once in the output.

SQL Server 70-461 04-02b to 04-03

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

What four guidelines must you follow when working with set operators?

A
  1. Number of columns has to be the same and column types of corresponding columns must be compatable.
  2. The individual queries can’t use ORDER BY but the result of the set operator can.
  3. Column names determined by first query.
  4. Set operators consider two NULLS equal.

SQL Server 70-461 04-02b to 04-03

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

What is the order of precedence for set operators?

A
  • Intersect precedes UNION and EXCEPT
  • UNION and EXCEPT are evaluated from left to right based on their position in the expression.

SQL Server 70-461 04-02b to 04-03

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