CS FundamentalsModerateWipro · Cognizant 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 does this query 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 does this query return?
SELECT e.name, d.city
FROM Emp e LEFT JOIN Dept d ON e.dept = d.dname;
  1. A. 8
  2. B. 7
  3. C. 5
  4. D. 4

Correct answer: B. 7

A LEFT JOIN keeps every Emp row: the 4 matches plus John, Lata (Sales, no Dept row) and Dev (NULL dept) with city NULL = 7. Ops is dropped because it is on the right side.

More DBMS and SQL questions

Exams that ask this topic