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;
  1. A. 2000
  2. B. NULL, because Desk's own price is NULL
  3. C. 0
  4. 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

Exams that ask this topic