Skip to content
Quantoros

Return departmentname of departments that have no employees. Table definitions (from the source exercise):

SQLLevel: IntermediateAnti-joins and missing data

Return department_name of departments that have no employees.

Table definitions (from the source exercise):

CREATE TABLE departments(department_id INTEGER PRIMARY KEY, department_name TEXT, created_date TEXT);
CREATE TABLE employees(emp_id INTEGER PRIMARY KEY, name TEXT, department_id INTEGER, manager_id INTEGER, salary INTEGER, hire_date TEXT, job_title TEXT, gender TEXT);
Hints
  • Define one output row and the source-table keys.
  • Check duplicates, NULLs and the order of filtering and aggregation.

Answer

Approach: LEFT JOIN employees, keep the NULL side.

SELECT d.department_name
FROM departments d
LEFT JOIN employees e ON e.department_id = d.department_id
WHERE e.emp_id IS NULL;

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions