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');The query as written:
sql
SELECT DISTINCT(dept_id), name FROM employees ORDER BY dept_id, name;| dept_id | name |
|---|---|
| NULL | Lena |
| 1 | Asha |
| 1 | Mei |
| 1 | Ravi |
| 2 | Sara |
| 2 | Tom |
| 3 | Diego |
| 3 | Omar |
The query returns all eight employees. DISTINCT(dept_id), name means DISTINCT dept_id, name: duplicates are removed only when both columns match.
A corrected version:
sql
SELECT dept_id, COUNT(*) AS staff, group_concat(name, ', ') AS names FROM employees GROUP BY dept_id ORDER BY dept_id;| dept_id | staff | names |
|---|---|---|
| NULL | 1 | Lena |
| 1 | 3 | Asha, Ravi, Mei |
| 2 | 2 | Tom, Sara |
| 3 | 2 | Diego, Omar |
Grouping by department gives one row each, with a count and a list of names. The order of names inside group_concat is not guaranteed unless an ORDER BY is given inside the aggregate (group_concat(name, ', ' ORDER BY name) in SQLite 3.44+). PostgreSQL's DISTINCT ON (dept_id) is the tool when one whole row per department is wanted.
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 DISTINCT(dept_id), name FROM employees ORDER BY dept_id, name;
SELECT dept_id, COUNT(*) AS staff, group_concat(name, ', ') AS names FROM employees GROUP BY dept_id ORDER BY dept_id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- DISTINCT applies to the entire row
- Parentheses do not make it per column
- Use GROUP BY with aggregates for one row per key
- DISTINCT ON picks one row per key in PostgreSQL
Common mistakes
- Treating DISTINCT as a function
- Expecting one arbitrary name per department
Follow-ups
- How would you show the highest-paid name per department?
- How does COUNT(DISTINCT x) differ?
Tips
- Rewrite without the parentheses