CS FundamentalsHardProduct-company MCQ 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 each 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 each query return?
-- Q1
SELECT e.name FROM Emp e LEFT JOIN Dept d
  ON e.dept = d.dname
WHERE d.city <> 'Delhi';
-- Q2
SELECT e.name FROM Emp e LEFT JOIN Dept d
  ON e.dept = d.dname AND d.city <> 'Delhi';
  1. A. Q1: 1, Q2: 7
  2. B. Q1: 4, Q2: 7
  3. C. Q1: 4, Q2: 4
  4. D. Q1: 1, Q2: 1

Correct answer: A. Q1: 1, Q2: 7

Q1: the WHERE runs after the join. Rows with city NULL (John, Lata, Dev) give UNKNOWN and IT rows give FALSE, leaving only Asha (Pune): 1 row; the outer join has become an inner join. Q2: the condition is part of ON, so every Emp row is kept (unmatched ones get NULLs): 7.

More DBMS and SQL questions

Exams that ask this topic