← Back to SQL, page 23

sql-0444 · Easy fresher

SELECT DISTINCT(dept_id), name returns a row for every employee instead of one per department. Why?

DISTINCT is not a function and applies to the whole select list; the parentheses around dept_id only group that expression. Since every (dept_id, name) pair is unique, nothing is removed. Select only dept_id, or aggregate the names per department.

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_idname
NULLLena
1Asha
1Mei
1Ravi
2Sara
2Tom
3Diego
3Omar

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_idstaffnames
NULL1Lena
13Asha, Ravi, Mei
22Tom, Sara
32Diego, 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