Departments with Their Employees (LEFT JOIN — Empty Groups Visible)
Return every department and its employee details, using NULL employee fields for empty departments.
- Joins
- CASE expressions
- NULL handling
- Sorting
Exercise brief
Understand the request
Workforce planning analyst A capacity roster must keep departments visible even when no employee is assigned.
Show every department alongside each of its employees. Departments with zero employees still appear (with NULL employee fields). Return department_id, department_name, location, employee_id, first_name, last_name, salary — ordered by department_id, salary DESC NULLS LAST, employee_id.
Return
- Return department_id, department_name, location, employee_id, first_name, last_name, and salary.
- Order deterministically by department and employee salary.
Constraints
- Start from departments and preserve it with LEFT JOIN.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
departments
department_idINTEGERdepartment_nameVARCHAR(50)locationVARCHAR(100)
employees
employee_idINTEGERfirst_nameVARCHAR(50)last_nameVARCHAR(50)salaryINTEGERdepartment_idINTEGER
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Operations dept (id 50) has no employees — LEFT JOIN gives it ONE row with all employee columns NULL. The result has 11 rows, not 10.
Hint 2
Sorting NULLs is engine-specific: some put NULLs first, some last. The CASE-WHEN-IS-NULL trick forces explicit "NULLs last" portably.
Hint 3
Group by department_id first (primary sort), then salary DESC within department.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT d.department_id, d.department_name, d.location, e.employee_id, e.first_name, e.last_name, e.salary FROM departments d LEFT JOIN employees e ON d.department_id = e.department_id ORDER BY d.department_id, CASE WHEN e.salary IS NULL THEN 1 ELSE 0 END, e.salary DESC, e.employee_id;Why this works
LEFT JOIN's promise: 'every left row, at least once'. Empty right-side groups still emit a row — useful for reporting (counts, completeness checks) and for spotting orphan/empty buckets.
Success check
All departments remain visible and Operations has one NULL-padded employee row.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| department_id | department_name | location | employee_id | first_name | last_name | salary |
|---|---|---|---|---|---|---|
| 10 | IT | San Francisco | 100 | John | Smith | 120000 |
| 10 | IT | San Francisco | 107 | Grace | White | 90000 |
| 10 | IT | San Francisco | 101 | Alice | Johnson | 85000 |
| 10 | IT | San Francisco | 102 | Bob | Wilson | 80000 |
| 20 | HR | New York | 103 | Carol | Davis | 60000 |
| 20 | HR | New York | 108 | Henry | Clark | 55000 |
| 30 | Finance | Chicago | 104 | David | Brown | 70000 |
| 30 | Finance | Chicago | 109 | Ivy | Martinez | 68000 |
| 40 | Marketing | Los Angeles | 105 | Emma | Taylor | 95000 |
| 40 | Marketing | Los Angeles | 106 | Frank | Green | 65000 |
Previewing 10 of 11 expected rows. Run the query in the editor to inspect the full result.
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
Self Joins & Hierarchical Queries
Query organization charts, trees, and parent-child relationships.
SQL Subqueries
Practice scalar, derived-table, correlated, EXISTS, NULL-safe anti-subquery, quantified, and row-subquery patterns.
SQL Aggregations
Build reliable SQL metrics from aggregate functions through grain, fan-out, weighted ratios, rollups, percentiles, and approximate counts.
Open the interactive workspace and practice across SQL topics.