Employees Without a Manager
Easysql
Some employees have no manager (manager_id is NULL — they're top-level staff). Write a query returning id and name for employees with no manager, ordered by id ascending.
Reminder: test for NULL with IS NULL, never = NULL (see NULL Semantics) — = NULL never matches anything.
sqlCREATE TABLE employees ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, department TEXT NOT NULL, salary INTEGER NOT NULL, manager_id INTEGER );
Sample data (this is what your query runs against when you press Run):
sqlINSERT INTO employees (id, name, department, salary, manager_id) VALUES (1, 'Asha', 'Engineering', 85000, NULL), (2, 'Rohan', 'Engineering', 72000, 1), (3, 'Neha', 'Sales', 68000, NULL), (4, 'Vikram', 'Sales', 61000, 3), (5, 'Priya', 'Engineering', 79000, 1), (6, 'Karan', 'Marketing', 55000, NULL), (7, 'Divya', 'Marketing', 58000, 6), (8, 'Aman', 'Sales', 64000, 3);
Example 1
Input
(none)
Output
id name 1 Asha 3 Neha 6 Karan
WHERE manager_id IS NULL is the only correct way to test for NULL — = NULL always evaluates to UNKNOWN and matches nothing, even for NULL rows. Reference: SELECT id, name FROM employees WHERE manager_id IS NULL ORDER BY id;