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;- A. Stationery: 1; Electronics: 1; Furniture: 0
- B. Stationery: 0; Electronics: 0; Furniture: 0
- C. Stationery: 1; Electronics: 0; Furniture: 0
- 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
- A database stores timestamps in UTC and converts to local time only at the application layer. A query does WHERE createdat BETWEEN…
- A column price NUMERIC (arbitrary precision decimal) is compared against a literal using price = 19.99. Why is this generally SAFE from…
- A relation R(A, B, C, D) has candidate key ABD (the ONLY candidate key) and FD AB → C (C is non-prime). Which normal form violation does…
- A query optimizer must decide between two equivalent plans for A JOIN B: (1) nested-loop join using an index on B, with A as the outer…
- A relation R(A, B, C, D, E) has FDs AB → C, C → DE. Which of the following is the candidate key, and how many non-prime attributes does R…
- Which pair of properties must every primary key satisfy?