CS FundamentalsHardIBM · Deloitte pattern

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). What does this query 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). What does this query return?
SELECT category,
       COUNT(*) FILTER (WHERE stock = 0) AS out_of_stock
FROM Products
GROUP BY category;
  1. A. Stationery: 1; Electronics: 1; Furniture: 0
  2. B. Stationery: 0; Electronics: 0; Furniture: 0
  3. C. Stationery: 1; Electronics: 0; Furniture: 0
  4. D. Stationery: 2; Electronics: 0; Furniture: 0

Correct answer: C. Stationery: 1; Electronics: 0; Furniture: 0

The FILTER clause restricts which rows COUNT(*) counts within each group, without needing a separate CASE expression. Only Notebook (Stationery) has stock = 0 exactly; Mouse's stock is NULL (not 0, and NULL = 0 is UNKNOWN, not TRUE), so it is not counted. Electronics and Furniture both have 0 matching rows.

More DBMS and SQL questions

Exams that ask this topic