advancedTop 30 Scenario-Based Questions
You need to find all managers and the count of their direct reports (including managers with 0 reports).
SELECT e.emp_id, e.first_name || ' ' || e.last_name AS manager_name, COUNT(r.emp_id) AS direct_reports FROM employees e LEFT JOIN employees r ON r.manager_id = e.emp_id GROUP BY e.emp_id, e.first_name, e.last_name ORDER BY direct_reports DESC; Key points: LEFT JOIN ensures managers with 0 reports appear (COUNT gives 0). SELF JOIN on manager_id = emp_id. GROUP BY on the manager's columns. Alternati
This is a Pro chapter
Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.
You need to find all managers and the count of their direct reports (including managers with 0 reports).