CS FundamentalsModerateProduct-company interview pattern

Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen', 'Delhi', 700), (5, 'Dia', NULL, 200), (6, 'Ben', 'Delhi', 700). What does this query return?

Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen', 'Delhi', 700), (5, 'Dia', NULL, 200), (6, 'Ben', 'Delhi', 700). What does this query return?
SELECT cust FROM Orders
GROUP BY cust
HAVING COUNT(*) > 1 AND SUM(amount) > 700;
  1. A. 'Ana' and 'Ben'
  2. B. Only 'Ana'
  3. C. Only 'Ben'
  4. D. No rows

Correct answer: B. Only 'Ana'

Ana has 2 rows with sum 800 (passes). Ben has 2 rows but SUM ignores his NULL, giving 700, which is not > 700. Chen and Dia have one row each. Choosing Ben too treats the condition as ≥ 700 or the NULL as a value.

More DBMS and SQL questions

Exams that ask this topic