CS FundamentalsVery hardTCS Digital · Infosys SP · GATE CS

Table Product(pid, cat, price) rows: (1, 'X', 10), (2, 'X', 20), (3, 'Y', 20), (4, NULL, 30), (5, 'Y', NULL), (6, 'Z', 5). What does this query return?

Table Product(pid, cat, price) rows: (1, 'X', 10), (2, 'X', 20), (3, 'Y', 20), (4, NULL, 30), (5, 'Y', NULL), (6, 'Z', 5). What does this query return?
SELECT COUNT(*) FROM Product p
WHERE NOT EXISTS (SELECT * FROM Product q
                  WHERE q.cat = p.cat AND q.price > p.price);
  1. A. 3
  2. B. 6
  3. C. 4
  4. D. 5

Correct answer: D. 5

Only pid 1 is excluded (pid 2 is pricier in X). pid 2, 3 and 6 are the top of their categories; pid 5's NULL price makes q.price > NULL unknown, and pid 4's NULL category matches no row, so NOT EXISTS is TRUE for both: 5.

More DBMS and SQL questions

Exams that ask this topic