← Back to SQL, page 1

sql-0019 · Medium mid

When you only want unique values, should you use SELECT DISTINCT or GROUP BY?

For returning unique combinations of columns, SELECT DISTINCT a, b and SELECT a, b GROUP BY a, b give the same rows, and planners often execute them the same way. GROUP BY is needed as soon as you also want an aggregate per group, such as a count. DISTINCT states intent more clearly when no aggregate is involved.

Full answer

Both remove duplicates, and for a plain "unique values" query they return the same result:

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);
sql
SELECT DISTINCT city FROM students WHERE city IS NOT NULL ORDER BY city;
city
Leeds
Pune
sql
SELECT city FROM students WHERE city IS NOT NULL GROUP BY city ORDER BY city;
city
Leeds
Pune

Most planners turn both into the same work: sort or hash the rows on the listed columns and keep one per key. You can check what SQLite plans for each with EXPLAIN QUERY PLAN:

sql
EXPLAIN QUERY PLAN SELECT DISTINCT city FROM students;
text
QUERY PLAN
SCAN students
USE TEMP B-TREE FOR DISTINCT
sql
EXPLAIN QUERY PLAN SELECT city FROM students GROUP BY city;
text
QUERY PLAN
SCAN students
USE TEMP B-TREE FOR GROUP BY

SQLite reports a temporary B-tree for each: one "FOR DISTINCT", the other "FOR GROUP BY". Either way it builds the same kind of structure to find unique keys.

The difference is what you can do next. GROUP BY lets you attach aggregates to each unique value:

sql
SELECT city, COUNT(*) AS students FROM students WHERE city IS NOT NULL GROUP BY city ORDER BY city;
citystudents
Leeds2
Pune2

DISTINCT cannot do that. Use DISTINCT when you only want unique rows, because it reads as exactly that. Use GROUP BY when you need counts, sums or other per-group values. Using GROUP BY without aggregates just to remove duplicates works, but reviewers will wonder whether an aggregate was forgotten.

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

SELECT DISTINCT city FROM students WHERE city IS NOT NULL ORDER BY city;

SELECT city FROM students WHERE city IS NOT NULL GROUP BY city ORDER BY city;

EXPLAIN QUERY PLAN SELECT DISTINCT city FROM students;

EXPLAIN QUERY PLAN SELECT city FROM students GROUP BY city;

SELECT city, COUNT(*) AS students FROM students WHERE city IS NOT NULL GROUP BY city ORDER BY city;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • DISTINCT and GROUP BY without aggregates return the same rows
  • Planners usually execute them the same way
  • GROUP BY is required for per-group aggregates
  • Pick the one that states the intent

Common mistakes

  • Using DISTINCT to cover duplicates caused by a bad join
  • Claiming one is always faster without checking the plan

Follow-ups

  • What does COUNT(DISTINCT city) do?
  • How would you get the number of unique cities per year?

Tips

  • Back up any performance claim with EXPLAIN on the target engine