← Back to SQL, page 7

sql-0133 · Easy fresher

Write a query that labels each employee's salary as 'high' (100000 or more), 'mid' (60000 to 99999) or 'low', and says 'unknown' when the salary is missing.

Use a searched CASE: CASE WHEN salary IS NULL THEN 'unknown' WHEN salary >= 100000 THEN 'high' WHEN salary >= 60000 THEN 'mid' ELSE 'low' END. Branches are tested in order and the first true one wins, so ordered thresholds need only a lower bound.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT name, salary,
       CASE
         WHEN salary IS NULL    THEN 'unknown'
         WHEN salary >= 100000  THEN 'high'
         WHEN salary >= 60000   THEN 'mid'
         ELSE 'low'
       END AS band
FROM employees
ORDER BY id;
namesalaryband
Asha120000high
Ravi95000mid
Mei95000mid
Tom70000mid
Sara62000mid
Diego55000low
LenaNULLunknown
Omar48000low

The first true WHEN wins, so the mid branch does not need AND salary < 100000: any salary that high already matched high.

The NULL check comes first on purpose. If it were left out, Lena's NULL salary would fail every comparison and land in ELSE as low, which is wrong. Without an ELSE at all, CASE returns NULL when nothing matches.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT name, salary,
       CASE
         WHEN salary IS NULL    THEN 'unknown'
         WHEN salary >= 100000  THEN 'high'
         WHEN salary >= 60000   THEN 'mid'
         ELSE 'low'
       END AS band
FROM employees
ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Searched CASE tests conditions in order
  • The first true branch wins
  • Without ELSE, unmatched rows get NULL
  • Handle NULL explicitly before comparisons

Common mistakes

  • Letting NULL fall into the ELSE branch
  • Writing overlapping ranges in the wrong order

Follow-ups

  • How would you count employees per band?
  • What is the difference in syntax between simple and searched CASE?

Tips

  • Order thresholds from highest to lowest