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
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response