Full answer
GROUP BY accepts any expression, so a CASE that maps each salary to a label is enough to build a histogram.
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 CASE WHEN salary IS NULL THEN 'unknown'
WHEN salary < 60000 THEN 'under 60k'
WHEN salary < 100000 THEN '60k-99,999'
ELSE '100k+' END AS band,
CASE WHEN salary IS NULL THEN 4
WHEN salary < 60000 THEN 1
WHEN salary < 100000 THEN 2
ELSE 3 END AS sort_key,
COUNT(*) AS people
FROM employees
GROUP BY band, sort_key
ORDER BY sort_key;| band | sort_key | people |
|---|---|---|
| under 60k | 1 | 2 |
| 60k-99,999 | 2 | 4 |
| 100k+ | 3 | 1 |
| unknown | 4 | 1 |
The sort_key keeps the bands in a sensible order; sorting by the label alone would put '100k+' before 'under 60k' as text. Lena's unknown salary gets its own band instead of being silently dropped. With ELSE missing, CASE would return NULL for her and she would still appear, but as an unlabelled group.
Boundaries deserve care. The bands use < 60000 and < 100000, so 60000 falls in the middle band and 100000 in the top one, and no salary falls between two bands. Writing BETWEEN 60000 AND 100000 for the middle band would overlap with >= 100000, and CASE would silently assign 100000 to whichever branch comes first.
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 CASE WHEN salary IS NULL THEN 'unknown'
WHEN salary < 60000 THEN 'under 60k'
WHEN salary < 100000 THEN '60k-99,999'
ELSE '100k+' END AS band,
CASE WHEN salary IS NULL THEN 4
WHEN salary < 60000 THEN 1
WHEN salary < 100000 THEN 2
ELSE 3 END AS sort_key,
COUNT(*) AS people
FROM employees
GROUP BY band, sort_key
ORDER BY sort_key;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- GROUP BY can use a CASE expression
- Sort bands by a numeric key, not the label
- Give NULLs an explicit band or exclude them on purpose
- Use half-open boundaries so bands neither overlap nor leave gaps
Common mistakes
- Overlapping BETWEEN ranges in CASE
- Sorting bands alphabetically
Follow-ups
- How would you show bands with zero employees?
- How would you compute equal-width bands automatically?
Tips
- Mention the boundary rule explicitly