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);
- A. 1 and 4
- B. Only 1
- C. 1, 4 and 6
- 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
- Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen'…
- Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen'…
- Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen'…
- R(A, B, C, D) has FDs A → B and C → D. It is decomposed into R1(A, B), R2(C, D) and R3(A, C). Which statement is correct?
- R(A, B, C, D) has exactly two candidate keys: A and BC. How many superkeys does R have?
- You need the customers who have placed no orders. Orders.cust may contain NULL values. Which query is correct in that case?