CS FundamentalsHardTCS Digital · Infosys SP · GATE CS

Table Staff(id, name, dept, pay, boss) rows: (1, 'Anu', 'Ops', 40, NULL), (2, 'Bala', 'Ops', 55, 1), (3, 'Chitra', 'Tech', 70, 1), (4, 'Dev', 'Tech', NULL, 3), (5, 'Esha', NULL, 35, 3), (6, 'Farid', 'Tech', 70, 2). How many rows does this query return?

Table Staff(id, name, dept, pay, boss) rows: (1, 'Anu', 'Ops', 40, NULL), (2, 'Bala', 'Ops', 55, 1), (3, 'Chitra', 'Tech', 70, 1), (4, 'Dev', 'Tech', NULL, 3), (5, 'Esha', NULL, 35, 3), (6, 'Farid', 'Tech', 70, 2). How many rows does this query return?
SELECT dept, COUNT(*) FROM Staff GROUP BY dept
UNION ALL
SELECT dept, COUNT(*) FROM Staff WHERE pay > 50 GROUP BY dept;
  1. A. 3
  2. B. 5
  3. C. 6
  4. D. 4

Correct answer: B. 5

The first query gives 3 groups (Ops, Tech, NULL). The second keeps Bala, Chitra, Farid and gives 2 groups (Ops, Tech). UNION ALL keeps all 5 rows.

More DBMS and SQL questions

Exams that ask this topic