CS Atlas GATE 2027
DBMS course
0 / 119 complete

DBMS/Phase 3 · Query Languages

Lesson 3.3.7 · Item 53 of 119

UNION / INTERSECT / EXCEPT and their ALL variants

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the bottom.

  1. After a LEFT JOIN, which COUNT gives 0 for unmatched rows? from Lesson 3.3.6
  2. What are the size bounds of R ∪ S in relational algebra? from Lesson 3.1.2
  3. Does SQL remove duplicates by default in SELECT? from Lesson 3.3.3

1 · The Hook

SELECT keeps duplicates. But UNION, INTERSECT and EXCEPT, by default, remove them, behaving like relational algebra. Add ALL and they keep duplicates, with exact counting rules. GATE gives two bags and asks how many rows each version returns.

2 · Predict First

A = SELECT roll FROM ENROLL WHERE grade = 'A' gives 1, 1, 3. B = SELECT roll FROM ENROLL WHERE grade = 'B' gives 1, 2, 3. How many rows does A UNION B return? A UNION ALL B?

Make a guess in your head. The answer opens Section 3.

3 · Core Concept

Predict First — the answer

UNION: {1, 2, 3} → 3 rows (duplicates removed). UNION ALL: all 6 values kept → 6 rows.

The set operators

query1 UNION     query2      -- rows in either, duplicates removed
query1 INTERSECT query2      -- rows in both, duplicates removed
query1 EXCEPT    query2      -- rows in query1 not in query2, duplicates removed (Oracle: MINUS)

Both queries must be union compatible: same number of columns, compatible types (Lesson 3.1.2). Column names come from the first query.

ALL versions (bag counting)

If a row appears m times in query1 and n times in query2:

OperatorCopies in result
UNION ALLm + n
INTERSECT ALLmin(m, n)
EXCEPT ALLmax(0, m − n)
UNION / INTERSECT / EXCEPT1 if the set rule says the row belongs, else 0

With A = {1, 1, 3} and B = {1, 2, 3}:

ResultRows
A UNION B1, 2, 33
A UNION ALL B1, 1, 3, 1, 2, 36
A INTERSECT B1, 32
A INTERSECT ALL B1, 3 (1: min(2, 1))2
A EXCEPT Bnone0
A EXCEPT ALL B1 (2 − 1 copies)1
B EXCEPT A21

Note: the plain versions remove duplicates even when the inputs are bags.

Using them

  • "Rolls who got an A or a B" → UNION.
  • "Rolls who got both an A and a B" → INTERSECT (1 and 3).
  • "Students who took no course" → SELECT roll FROM STUDENT EXCEPT SELECT roll FROM ENROLL → 5 (Esha).

Support varies: MySQL added INTERSECT/EXCEPT only in version 8.0.31; SQLite supports UNION ALL but not INTERSECT ALL or EXCEPT ALL. GATE assumes standard SQL.

NULL in set operators

Set operators compare rows for duplicates, and for that purpose two NULLs are treated as equal (like DISTINCT). So {NULL} INTERSECT {NULL} returns one NULL row, although NULL = NULL is UNKNOWN in WHERE.

4 · Visual

value 1:  A has 2 copies, B has 1 copy
  UNION ALL      2 + 1          = 3 copies
  INTERSECT ALL  min(2, 1)      = 1 copy
  EXCEPT ALL     max(0, 2 - 1)  = 1 copy
  UNION / INTERSECT / EXCEPT    -> 1, 1, 0 copies (set rules)

5 · Worked Examples

Example 1 Easy

SELECT dept FROM STUDENT UNION SELECT dept FROM COURSE; How many rows?

Solution

STUDENT depts {CS, EE, ME}, COURSE depts {CS, EE, ME} → 3.

Example 2 Medium

SELECT dept FROM STUDENT UNION ALL SELECT dept FROM COURSE; How many rows?

Solution

5 + 4 = 9.

Example 3 GATE

R has a value v 5 times; S has v 3 times. How many copies of v in R EXCEPT ALL S, S EXCEPT ALL R, R INTERSECT ALL S, R EXCEPT S?

Solution

max(0, 5 − 3) = 2; max(0, 3 − 5) = 0; min(5, 3) = 3; R EXCEPT S: v is in S, so 0.

6 · GATE Traps

  • Plain UNION/INTERSECT/EXCEPT remove duplicates; ALL versions keep them.
  • EXCEPT ALL subtracts counts; EXCEPT (set) removes a value completely if it appears in the second query at all.
  • Set operators treat NULLs as equal when matching rows.
  • Union compatibility: same number of columns and compatible types.

7 · Memory Hook

"ALL keeps the crowd; without ALL, one of each." Counting: add, min, subtract (not below zero).

8 · Connect the Dots

Lesson 3.3.6 combined tables side by side (joins); this lesson stacked whole results (set operators). Next: putting one query inside another: subqueries with IN, ANY, ALL and EXISTS.

9 · Self-Test

GATE format. Try every question before opening the vault.

Q1 · NAT · 1 mark(s) — UNION ALL

How many rows: SELECT roll FROM ENROLL UNION ALL SELECT roll FROM STUDENT;

Numerical answer: ________

Q2 · NAT · 1 mark(s) — UNION

How many rows: SELECT roll FROM ENROLL UNION SELECT roll FROM STUDENT;

Numerical answer: ________

Q3 · NAT · 2 mark(s) — EXCEPT ALL

R contains the values 4, 4, 4, 7, 7, 9 and S contains 4, 7, 7, 7, 8. How many rows in R EXCEPT ALL S?

Numerical answer: ________

Q4 · NAT · 2 mark(s) — INTERSECT ALL

Same R and S. How many rows in R INTERSECT ALL S?

Numerical answer: ________

Q5 · MCQ · 1 mark(s) — EXCEPT

SELECT cid FROM COURSE EXCEPT SELECT cid FROM ENROLL; returns:

  1. (A) C1, C2, C3, C4
  2. (B) C4
  3. (C) an error
  4. (D) no rows

10 · Rule Card

  • UNION / INTERSECT / EXCEPT: set semantics (duplicates removed); need union compatibility.
  • ALL: UNION m + n; INTERSECT min(m, n); EXCEPT max(0, m − n).
  • Set operators treat NULL rows as equal to each other.
  • Oracle's MINUS = EXCEPT.

11 · Concept Ledger

Unlocked in this lesson. From here on, these terms may be used.

UNIONINTERSECTEXCEPT (MINUS)UNION ALLINTERSECT ALLEXCEPT ALLbag counting rules

12 · Answer Vault

Reveal answers when you’re ready

Recall Warm-up

  1. COUNT of a column from the right table.
  2. max(r, s) to r + s.
  3. No; only with DISTINCT.

Self-Test

Q1 Answer: 12 · 1 mark(s)

7 + 5.

Re-read: Lesson 3.3.7 — UNION / INTERSECT / EXCEPT and their ALL variants

Q2 Answer: 5 · 1 mark(s)

{1, 2, 3, 4, 5}.

Re-read: Lesson 3.3.7 — UNION / INTERSECT / EXCEPT and their ALL variants

Q3 Answer: 3 · 2 mark(s)

4: 3 − 1 = 2; 7: max(0, 2 − 3) = 0; 9: 1. Total 3.

Re-read: Lesson 3.3.7 — UNION / INTERSECT / EXCEPT and their ALL variants

Q4 Answer: 3 · 2 mark(s)

4: min(3, 1) = 1; 7: min(2, 3) = 2; 9, 8: 0. Total 3.

Re-read: Lesson 3.3.7 — UNION / INTERSECT / EXCEPT and their ALL variants

Q5 Answer: (D) · 1 mark(s)

Every course has an enrolment.

  • (A) is wrong: All are in ENROLL.
  • (B) is wrong: C4 has Diya.
  • (C) is wrong: Valid query.

Re-read: Lesson 3.3.7 — UNION / INTERSECT / EXCEPT and their ALL variants

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.