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);
- A. Pen, Notebook, Laptop
- B. Laptop only
- C. Notebook, Laptop
- 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
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Sales(sid, pid, qty, saledate, cust) rows: (101, 1, 10, '2024-01-05', 'Asha'), (102, 3, 1, '2024-01-06', 'Ravi'), (103, 2, 5…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…