Helix Systems' HR team is building an org chart and needs to distinguish leadership from the general workforce.
Write a query to return each employee's name and a level label:
'executive'for employees who have no manager on record (i.e.,manager_idisNULL).'staff'for everyone else.
Assumptions:
- The
employeestable contains every active and former employee at Helix Systems. - The
manager_idcolumn links each employee to their direct manager; top-of-hierarchy executives havemanager_idset toNULL.
Output:
- One row per employee, with columns
nameandlevel.
Schema · hr 4 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
name,
CASE
WHEN manager_id IS NULL THEN 'executive'
ELSE 'staff'
END AS level
FROM
employees The shape
CASE WHEN manager_id IS NULL THEN 'executive' ELSE 'staff' END splits the workforce on a single test: does this employee have a manager on record? Top-of-hierarchy employees have manager_id IS NULL, and the CASE returns 'executive' for them. Everyone else is 'staff'.
Clause by clause
SELECT namereturns the employee's name column unchanged. The org chart needs the name alongside the level label.CASE WHEN manager_id IS NULL THEN 'executive' ELSE 'staff' END AS levelis the derived column. TheWHENusesIS NULL— the only operator that asks "is this value missing." When that's true, theTHENbranch returns'executive'. OtherwiseELSE 'staff'fires.FROM employeesis the source set: every employee Helix Systems has on file.
The trap
Writing the branch as WHEN manager_id = NULL looks reasonable but is silently wrong. NULL is the absence of a value, and any comparison against it with = or <> returns NULL (PostgreSQL's three-valued logic), which a WHEN branch treats as not-matching. Every executive would fall through to ELSE, get labelled 'staff', and the bug is invisible — no error, no warning, just every executive misclassified. IS NULL is the only operator that returns a real boolean against a missing value.
You practiced testing for NULL inside a CASE branch using IS NULL. The same rule from WHERE carries over: manager_id = NULL would always evaluate to unknown and the branch would never match — IS NULL is the only correct test.