CS FundamentalsModerateTCS NQT · Infosys pattern

Table Emp(id, name, dept, salary, mgr) rows: (1, 'Asha', 'HR', 50000, NULL), (2, 'Ravi', 'IT', 70000, 1), (3, 'Meena', 'IT', 70000, 2), (4, 'John', 'Sales', 40000, 1), (5, 'Kiran', 'IT', 90000, 2), (6, 'Dev', NULL, 45000, 4), (7, 'Lata', 'Sales', NULL, 4). Table Dept(dname, city) rows: ('HR', 'Pune'), ('IT', 'Delhi'), ('Ops', 'Mumbai'). How many rows do the two queries return?

Table Emp(id, name, dept, salary, mgr) rows: (1, 'Asha', 'HR', 50000, NULL), (2, 'Ravi', 'IT', 70000, 1), (3, 'Meena', 'IT', 70000, 2), (4, 'John', 'Sales', 40000, 1), (5, 'Kiran', 'IT', 90000, 2), (6, 'Dev', NULL, 45000, 4), (7, 'Lata', 'Sales', NULL, 4). Table Dept(dname, city) rows: ('HR', 'Pune'), ('IT', 'Delhi'), ('Ops', 'Mumbai'). How many rows do the two queries return?
-- Query 1
SELECT dept FROM Emp UNION SELECT dname FROM Dept;
-- Query 2
SELECT dept FROM Emp UNION ALL SELECT dname FROM Dept;
  1. A. 5 and 10
  2. B. 4 and 9
  3. C. 4 and 10
  4. D. 5 and 9

Correct answer: A. 5 and 10

UNION ALL keeps everything: 7 + 3 = 10. UNION removes duplicates and treats the NULLs as one value: {NULL, HR, IT, Sales, Ops} = 5. Forgetting that NULL survives as a row gives 4.

More DBMS and SQL questions

Exams that ask this topic