CS FundamentalsHardTCS 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 price > (SELECT AVG(price) FROM Product q
               WHERE q.cat = p.cat);
  1. A. 2
  2. B. 1
  3. C. 3
  4. D. 0

Correct answer: B. 1

X average is 15, so pid 2 (20) qualifies. Y's average is 20 and 20 > 20 is false; Z has one row; the NULL-cat row's subquery matches nothing. Only 1 row.

More DBMS and SQL questions

Exams that ask this topic