SQL Joins · Question 12 · Medium
SQL Joins question 12
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. What does this query return?
SELECT m.name, COUNT(*)
FROM Staff e
JOIN Staff m ON e.manager_id = m.emp_id
GROUP BY m.name
ORDER BY m.name;
- Nandini, 4; Omkar, 2
- Nandini, 2; Omkar, 2
- Nandini, 1; Omkar, 2
- Nandini, 2; Omkar, 2; Pallavi, 0; Qasim, 0; Ritu, 0
Answer and explanation
Answer: B. Nandini, 2; Omkar, 2
Explanation: The self join gives one row per employee who has a manager: Omkar and Pallavi under Nandini, Qasim and Ritu under Omkar. Only direct reports are counted, and people with no reports never appear as m.
Previous: Table Learners: name | city Isha | Delhi Jatin | Delhi Kiran | Pune Leela |… · Next: Table Riders: rider_id | name 1 | Manoj 2 | Nisha 3 | Om Table Trips: trip_id…
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
- Table Teams: team_id | team | city 1 | CSK | Chennai 2 | MI | Mumbai 3 | RCB |…
- Table Teams: team_id | team | city 1 | CSK | Chennai 2 | MI | Mumbai 3 | RCB |…
- Table Learners: name | city Isha | Delhi Jatin | Delhi Kiran | Pune Leela |…
- Table Riders: rider_id | name 1 | Manoj 2 | Nisha 3 | Om Table Trips: trip_id…
- Table Riders: rider_id | name 1 | Manoj 2 | Nisha 3 | Om Table Trips: trip_id…
- Tables Authors(author_id, name) and Books(book_id, title, author_id) exist…
- Table Teams: team_id | team | city 1 | CSK | Chennai 2 | MI | Mumbai 3 | RCB |…
- Table Riders: rider_id | name 1 | Manoj 2 | Nisha 3 | Om Table Trips: trip_id…
- Table Invoices: inv_id | customer | total 1 | Zoya | 1000 2 | Arun | 500 Table…
- Table Staff: emp_id | name | manager_id 1 | Nandini | NULL 2 | Omkar | 1 3 |…
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