CS FundamentalsHardProduct-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). Which ids does this correlated 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). Which ids does this correlated query return?
SELECT id FROM Orders o
WHERE amount > (SELECT AVG(amount) FROM Orders WHERE city = o.city);
  1. A. 1 and 4
  2. B. Only 1
  3. C. 1, 4 and 6
  4. D. 1 and 5

Correct answer: B. Only 1

Pune average = (500 + 300)/2 = 400, so id 1 qualifies. Delhi average ignores the NULL: (700 + 700)/2 = 700, and 700 > 700 is false. For id 5, city = NULL matches no rows, the AVG is NULL and the comparison is UNKNOWN. Only id 1. Choosing 4 and 6 averages Delhi over 3 rows (466.67).

More DBMS and SQL questions

Exams that ask this topic