Helix Systems' HR team is generating a managed-headcount report for the board, covering only staff who report to a manager.
Write a query to return the name and title of every qualifying employee.
Assumptions:
- The
employeestable contains every active and former employee at Helix Systems. - The
manager_idcolumn links each employee to their direct manager. - Top-level executives have
manager_idset toNULLand should be excluded from this report.
Output:
- One row per managed employee, 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 NOT NULL The shape
IS NOT NULL is the complement: keep rows where the value is present. The managed-headcount report excludes top-level executives, and "top-level" is encoded as a NULL manager_id. Filtering to non-null manager links is the exclusion.
Clause by clause
SELECT name, titlereturns the two columns the board report needs — who, and what role.FROM employeesreads the staff records.WHERE manager_id IS NOT NULLkeeps only rows where the manager link is present.IS NOT NULLreturnstruefor everyone who reports to someone andfalsefor the root, so Sarah Chen drops out and the 60 managed staff stay in.
Why this and not WHERE manager_id <> NULL
Both shapes look like they should mean "manager_id is not null," but only IS NOT NULL works. manager_id <> NULL is a comparison: it asks whether the manager link is not equal to NULL. Comparing anything to NULL produces unknown, never true, so the condition fails for every row — the ones with managers and the one without. The query returns zero rows, and the board report comes back empty.
IS NOT NULL is not a comparison. It's a direct test for the presence of a value, and it always returns true or false.
The trap
Any operator that compares to NULL (=, <>, <, >) silently produces an empty or wrong result. No error, no warning. The query runs, the board gets a report, and the headcount is wrong. When the test is for presence or absence of a value, use IS NULL or IS NOT NULL. Never = or <> against NULL.
You practiced testing for the presence of a value with IS NOT NULL. The complement of IS NULL flips the semantic from 'missing' to 'present' and is just as common.