CS FundamentalsHardGATE CS · TCS NQT

DEPT(d, name) has rows (10,A), (20,B), (30,C), (40,D) and EMP(id, dept) has 3 employees in dept 10, 2 in dept 20, 1 in dept 30 and none in dept 40. How many rows does this query return? SELECT name FROM DEPT LEFT JOIN EMP ON EMP.dept = DEPT.d GROUP BY name HAVING COUNT(*) = 1;

  1. A. 1
  2. B. 2
  3. C. 3
  4. D. 0

Correct answer: B. 2

The left join keeps dept D with a single NULL-extended row, so COUNT(*) is 1 for both C (one real employee) and D (no employee). The result has 2 rows; COUNT(EMP.id) would have counted only C.

More DBMS and SQL questions

Exams that ask this topic