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;
- A. 4 and 7
- B. 5 and 9
- C. 4 and 2
- 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
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Table Products(pid, pname, category, price, stock) rows: (1, 'Pen', 'Stationery', 10, 100), (2, 'Notebook', 'Stationery', 50, 0), (3…
- Why does adding an index on a frequently-UPDATEd or frequently-INSERTed column have a real cost, beyond the storage the index itself uses?
- ARIES recovery uses a Log Sequence Number (LSN) stored on each data page, recording the LSN of the last log record that modified that…
- ARIES recovery performs three phases on restart: analysis, redo, then undo. Why must redo happen BEFORE undo, rather than undoing first…