SQL Joins · Question 19 · Hard

SQL Joins question 19

Table Staff:
emp_id | name | manager_id
1 | Nandini | NULL
2 | Omkar | 1
3 | Pallavi | 1
4 | Qasim | 2
5 | Ritu | 2

manager_id refers to emp_id in the same table. Which names does this query return?

SELECT e.name
FROM Staff e
JOIN Staff m ON e.manager_id = m.emp_id
JOIN Staff g ON m.manager_id = g.emp_id
ORDER BY e.name;
  1. Omkar; Pallavi
  2. Qasim; Ritu
  3. Nandini
  4. Omkar; Pallavi; Qasim; Ritu
Answer and explanation

Answer: B. Qasim; Ritu

Explanation: m is the employee's manager and g is the manager's manager. Omkar and Pallavi report to Nandini, whose manager_id is NULL, so the second join drops them. Qasim and Ritu report to Omkar, who reports to Nandini.

Previous: Table Invoices: inv_id | customer | total 1 | Zoya | 1000 2 | Arun | 500 Table… · Next: Table A has m rows. Table B has n rows and B.k is its primary key. How many…

Practise sql joins

Try a fresh set of sql joins questions with an explanation after every answer, free and without signing up: SQL Joins practice. Or sit a full placement mock test.

More sql joins questions

More sql practice

Aggregates & GROUP BY · Subqueries & CTEs · Window Functions · NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge