CS FundamentalsHardProduct-company MCQ 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). 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, '2024-01-06', 'Asha'), (104, 5, 2, NULL, 'Meena'), (105, 4, 3, '2024-01-08', 'Ravi'), (106, 7, 20, '2024-01-09', NULL), (107, 3, 1, '2024-01-10', 'Asha'). For Desk's group, what do COUNT() and COUNT(s.sid) each 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). 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, '2024-01-06', 'Asha'), (104, 5, 2, NULL, 'Meena'), (105, 4, 3, '2024-01-08', 'Ravi'), (106, 7, 20, '2024-01-09', NULL), (107, 3, 1, '2024-01-10', 'Asha'). For Desk's group, what do COUNT(*) and COUNT(s.sid) each return?
SELECT p.pname, COUNT(*), COUNT(s.sid)
FROM Products p LEFT JOIN Sales s ON p.pid = s.pid
GROUP BY p.pname;
  1. A. COUNT(*) = 1, COUNT(s.sid) = 0
  2. B. COUNT(*) = 0, COUNT(s.sid) = 0
  3. C. COUNT(*) = 1, COUNT(s.sid) = 1
  4. D. COUNT(*) = 0, COUNT(s.sid) = 1

Correct answer: A. COUNT(*) = 1, COUNT(s.sid) = 0

The LEFT JOIN still produces one row for Desk (with every Sales column NULL, since no sale matches), so COUNT(*) counts that row: 1. But COUNT(s.sid) only counts non-NULL values of s.sid, and here sid is NULL, so it counts 0 — the standard COUNT(*) vs COUNT(column) gap on an unmatched LEFT JOIN row.

More DBMS and SQL questions

Exams that ask this topic