CS FundamentalsModerateGATE CS · SQL

Table Sale(id, rep, region, amt) rows: (1, 'Ira', 'N', 100), (2, 'Jai', 'S', 250), (3, 'Ira', 'S', NULL), (4, 'Kim', 'N', 300), (5, 'Jai', 'N', 150), (6, 'Ira', 'N', 200), (7, 'Lal', NULL, 120). What does this query return?

Table Sale(id, rep, region, amt) rows: (1, 'Ira', 'N', 100), (2, 'Jai', 'S', 250), (3, 'Ira', 'S', NULL), (4, 'Kim', 'N', 300), (5, 'Jai', 'N', 150), (6, 'Ira', 'N', 200), (7, 'Lal', NULL, 120). What does this query return?
SELECT COUNT(*) FROM (SELECT DISTINCT rep, region FROM Sale);
  1. A. 5
  2. B. 3
  3. C. 6
  4. D. 2

Correct answer: C. 6

DISTINCT applies to the (rep, region) pair: (Ira, N), (Jai, S), (Ira, S), (Kim, N), (Jai, N), (Lal, NULL). Only Ira's two N sales collapse: 7 − 1 = 6. DISTINCT keeps the NULL pair (5 would be COUNT(DISTINCT rep, region), which skips it); 3 and 2 come from looking at region alone.

More DBMS and SQL questions

Exams that ask this topic