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). For Desk's row, what does cattotal show?
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).
For Desk's row, what does `cat_total` show?
SELECT pname, price,
SUM(price) OVER (PARTITION BY category) AS cat_total
FROM Products;- A. 2000
- B. NULL, because Desk's own price is NULL
- C. 0
- D. 4000
Correct answer: A. 2000
Window aggregates, like ordinary aggregates, skip NULLs when computing the sum: the Furniture partition (Chair, Desk) sums just Chair's 2000. That total is attached to every row of the partition, including Desk's — Desk's OWN price stays NULL, but its cat_total is still 2000.
More DBMS and SQL questions
- Table Sales(sid, pid, qty, saledate, cust) rows: (101, 1, 10, ...), (102, 3, 1, ...), (103, 2, 5, ...), (104, 5, 2, ...), (105, 4, 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…
- 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…