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). 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'). How many distinct products has Asha bought, according to this query?

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'). How many distinct products has Asha bought, according to this query?
SELECT COUNT(*)
FROM Products p
WHERE EXISTS (SELECT 1 FROM Sales s WHERE s.pid = p.pid AND s.cust = 'Asha');
  1. A. 7
  2. B. 3
  3. C. 4
  4. D. 2

Correct answer: B. 3

Asha's sales are sid 101 (pid 1, Pen), 103 (pid 2, Notebook) and 107 (pid 3, Laptop) — 3 distinct pids, so EXISTS is true for exactly 3 products regardless of how many times each pid repeats in Sales.

More DBMS and SQL questions

Exams that ask this topic