List Employees with No Department
EasySQLPractice library · +10 XPnull handlingsqlemployees
A company wants to track its employees and their assigned departments. Some employees might not have been assigned a department yet.
Tables:
Employees(employee_id INT PRIMARY KEY, employee_name VARCHAR(100) NOT NULL, department_id INT)
Return the employee_name of all employees who do not have a department assigned. If no employees are unassigned, return an empty result. Employees should be listed in alphabetical order by name.
Tables
CREATE TABLE Employees (
employee_id SERIAL PRIMARY KEY,
employee_name VARCHAR(100) NOT NULL,
department_id INT
);Example data
INSERT INTO Employees (employee_id, employee_name, department_id) VALUES (1, 'Alice', 101), (2, 'Bob', 102), (3, 'Charlie', NULL), (4, 'David', 101), (5, 'Eve', NULL), (6, 'Frank', 103), (7, 'Grace', NULL);
Your query is also checked on hidden datasets with different data.
Hints
-- Select employee names without a department
SELECT employee_name
FROM Employees
Your query runs on the example tables shown with the problem, then on hidden datasets when you submit.
Write standard SQL. MySQL habits work too: IFNULL, IF, YEAR(), DATE_FORMAT, GROUP_CONCAT, `backticks` and LIMIT 5, 10. It runs on PostgreSQL, so integer division keeps whole numbers (5 / 2 = 2): write 5.0 / 2 for 2.5.