CS FundamentalsHardGATE CS (PYQ pattern)

A1(x) holds 1, 1, 1, 2, 2, 3, NULL, NULL and B1(x) holds 1, 2, 2, 2, NULL, 4. Using the standard bag semantics (a value occurring m times in A1 and n times in B1 appears m + n times in UNION ALL, min(m, n) times in INTERSECT ALL and max(m - n, 0) times in EXCEPT ALL; NULLs are treated as equal in these operations), how many rows do A1 UNION ALL B1, A1 INTERSECT ALL B1 and A1 EXCEPT ALL B1 return?

  1. A. 14, 3 and 5
  2. B. 14, 4 and 4
  3. C. 5, 3 and 1
  4. D. 14, 4 and 2

Correct answer: B. 14, 4 and 4

UNION ALL keeps everything: 8 + 6 = 14. INTERSECT ALL: 1 -> min(3,1) = 1, 2 -> min(2,3) = 2, NULL -> min(2,1) = 1, total 4. EXCEPT ALL: 1 -> 2, 2 -> 0, 3 -> 1, NULL -> 1, total 4. If NULLs were never matched, INTERSECT ALL would give 3 and EXCEPT ALL 5 (both NULLs kept); the duplicate-removing set versions give 5, 3 and 1.

More DBMS and SQL questions

Exams that ask this topic