MediumSQL
Departments Without Employees
SQLDatabases
Write a SQL query that finds all departments that have no employees,
ordered by department name.
Tables
departments: id, nameemployees: id, name, dept_id
| dept | name |
|---|---|
| 1 | Engineering |
| 2 | Marketing |
| 3 | Legal |
| 4 | Sales |
Employees exist in Engineering (1), Marketing (2) and Sales (4) — but not Legal (3).
Expected output (column: department_name)
LegalSQLite output format — rows are pipe-separated (
|) with no column headers.Example:
SELECT id, name FROM users WHERE id = 1→1|Alice
Hint — use a
LEFT JOINand check forIS NULLon the employee side.
Sample tests
Test #1Legal (3) has no employees
Input: "CREATE TABLE departments (id INTEGER, name TEXT);\nCREATE TABLE employees (id INTEGER, name TEXT, dept_id INTEGER);\nINSERT INTO departments VALUES (1, 'Engineering'), (2, 'Marketing'), (3, 'Legal'), (4, 'Sales');\nINSERT INTO employees VALUES\n (1, 'Alice', 1), (2, 'Bob', 2), (3, 'Carol', 1), (4, 'Dave', 4);"
Output: "Legal"