CS FundamentalsVery hardProduct company interview · GATE level

Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3, 'Laptop', 'Electronics', 50000, 5), (4, 'Mouse', 'Electronics', 500, NULL), (5, 'Chair', 'Furniture', 2000, 20), (6, 'Desk', 'Furniture', NULL, 3), (7, 'Pencil', 'Stationery', 5, 200). Which product names does this correlated subquery return?

Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3, 'Laptop', 'Electronics', 50000, 5), (4, 'Mouse', 'Electronics', 500, NULL), (5, 'Chair', 'Furniture', 2000, 20), (6, 'Desk', 'Furniture', NULL, 3), (7, 'Pencil', 'Stationery', 5, 200). Which product names does this correlated subquery return?
SELECT pname
FROM Products p
WHERE price > (SELECT AVG(price) FROM Products WHERE category = p.category);
  1. A. Pen, Notebook, Laptop
  2. B. Laptop only
  3. C. Notebook, Laptop
  4. D. Notebook, Laptop, Chair

Correct answer: C. Notebook, Laptop

Category averages (ignoring NULL prices): Stationery ≈ 21.7, Electronics = 25250, Furniture = 2000 (Desk's NULL is excluded from the average, leaving just Chair). Within Stationery only Notebook (50) beats 21.7; within Electronics only Laptop (50000) beats 25250; within Furniture, Chair (2000) is not strictly greater than 2000, and Desk's NULL price makes the comparison UNKNOWN. So only Notebook and Laptop qualify.

More DBMS and SQL questions

Exams that ask this topic