← Back to SQL, page 1

sql-0004 · Easy fresher

What does SELECT DISTINCT do when it is applied to more than one column?

DISTINCT removes duplicate rows from the output, comparing all selected columns together. With two columns, a row is a duplicate only if both values match another row. DISTINCT treats NULLs as equal to each other for this purpose, so several NULLs collapse into one.

Full answer

DISTINCT is not a function applied to the first column. It applies to the whole output row. With SELECT DISTINCT city, year, two rows are duplicates only when the city and the year are both the same.

Using the students table:

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);

One column first. Pune and Leeds appear twice each, and Dara has no city:

sql
SELECT DISTINCT city FROM students ORDER BY city;
city
NULL
Leeds
Pune

Now two columns. Asha and Chen are both from Pune in year 1, so they collapse into one row. Ben and Eli (Leeds, year 2) collapse too:

sql
SELECT DISTINCT city, year FROM students ORDER BY city, year;
cityyear
NULL3
Leeds2
Pune1

Notice the NULL row in both results. When DISTINCT compares rows, two NULLs count as "not distinct", so all missing cities fold into a single row. That is different from =, where NULL = NULL is not true. The standard calls this "IS NOT DISTINCT FROM" behaviour, and GROUP BY uses the same rule.

Writing SELECT DISTINCT(city), year does not change anything. The parentheses only wrap the expression city; DISTINCT still applies to the full row.

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 ORDER BY city;

SELECT DISTINCT city, year FROM students ORDER BY city, year;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • DISTINCT compares the whole selected row, not one column
  • NULLs are treated as equal for DISTINCT, so they collapse into one row
  • Parentheses after DISTINCT do not limit it to one column

Common mistakes

  • Writing DISTINCT(col1), col2 and expecting uniqueness on col1 only
  • Using DISTINCT to hide duplicate rows caused by a wrong join

Follow-ups

  • How is DISTINCT different from GROUP BY with no aggregates?
  • How do you keep one full row per city, not just the distinct city names?

Tips

  • If DISTINCT fixes a duplicate problem, ask whether a join is producing extra rows
  • Mention the NULL rule; interviewers often check it