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;
- A. 5 and 10
- B. 4 and 9
- C. 4 and 10
- 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
- Transaction T2 reads a value that T1 has written but not yet committed; T1 later rolls back. What is this anomaly called?
- Table Emp(id, name, dept, salary, mgr) rows: (1, 'Asha', 'HR', 50000, NULL), (2, 'Ravi', 'IT', 70000, 1), (3, 'Meena', 'IT', 70000, 2)…
- A candidate key is best described as:
- In an ER diagram, entity sets Student and Course are related by a many-to-many relationship Enrols that has an attribute grade. What is…
- Table Emp(id, name, dept, salary, mgr) rows: (1, 'Asha', 'HR', 50000, NULL), (2, 'Ravi', 'IT', 70000, 1), (3, 'Meena', 'IT', 70000, 2)…
- Which mechanism does a DBMS mainly rely on to guarantee durability of committed transactions?