CS FundamentalsHardTCS NQT · Infosys pattern

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 rows do the two queries return?

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 rows do the two queries return?
-- Query 1
SELECT cust FROM Sales
UNION
SELECT cust FROM Sales WHERE pid = 3;
-- Query 2
SELECT cust FROM Sales
UNION ALL
SELECT cust FROM Sales WHERE pid = 3;
  1. A. 4 and 7
  2. B. 5 and 9
  3. C. 4 and 2
  4. D. 4 and 9

Correct answer: D. 4 and 9

The distinct custs across all of Sales are Asha, Ravi, Meena and NULL (UNION treats NULL as one value for duplicate elimination) = 4 rows. UNION ALL keeps every row: all 7 from Sales plus the 2 rows where pid = 3 (sid 102 and 107) = 9 rows.

More DBMS and SQL questions

Exams that ask this topic