CS FundamentalsHardTCS Digital · Infosys SP · GATE CS

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 rep FROM Sale s
WHERE region IS NOT NULL
GROUP BY rep
HAVING COUNT(DISTINCT region) =
       (SELECT COUNT(DISTINCT region) FROM Sale)
ORDER BY rep;
  1. A. Jai
  2. B. Ira, Jai, Kim
  3. C. Ira, Jai
  4. D. No rows (empty result)

Correct answer: C. Ira, Jai

COUNT(DISTINCT region) over the table ignores NULL, so it is 2 (N and S). Ira and Jai have sold in both regions; Kim only in N. This is division with GROUP BY/HAVING.

More DBMS and SQL questions

Exams that ask this topic