Helix Systems' HR team is building an organizational chart and needs to identify the top of the reporting hierarchy — the executives who do not report to anyone.
Write a query to return the name and title of every such employee.
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 have
manager_idset toNULL(no one is above them).
Output:
- One row per top-level executive, with columns
nameandtitle.
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,
title
FROM
employees
WHERE
manager_id IS NULL The shape
The top of a reporting hierarchy is encoded as the absence of a manager. Sarah Chen, the CEO, has no one above her, so her manager_id is NULL. IS NULL is what surfaces that row.
Clause by clause
SELECT name, titlereturns the two pieces of information the org chart needs at the root level — who, and what role.manager_iditself stays out of the output; it'sNULLfor every row in the result.FROM employeesreads the staff records. The prompt's assumption that this table contains everyone (active and former) is what guarantees the root is reachable from this single table.WHERE manager_id IS NULLkeeps only the row whose manager link is absent. In a hierarchy with a single CEO, that returns one row.
The trap
WHERE manager_id = NULL returns zero rows no matter how many top-level executives exist. Comparing to NULL with = produces unknown, never true, and WHERE drops rows whose condition isn't clearly true. The query runs, the org chart comes back empty at the top, and the structural assumption ("someone has to be at the root") quietly fails. IS NULL is the only test that asks the right question.
You practiced using IS NULL to find the top of a hierarchy. Self-referential tables (employees who manage other employees) almost always use NULL to mark the root.