Full answer
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);SQLite does not accept two arguments to COUNT(DISTINCT ...):
SELECT COUNT(DISTINCT city, year) FROM students; -- error on purposeError: wrong number of arguments to function COUNT()The portable form counts the rows of a DISTINCT subquery:
SELECT COUNT(*) AS distinct_pairs
FROM (SELECT DISTINCT city, year FROM students);| distinct_pairs |
|---|
| 3 |
There are three distinct pairs: (Pune, 1), (Leeds, 2) and (NULL, 3). The subquery's DISTINCT treats NULLs as equal, so Dara's (NULL, 3) pair is one combination.
PostgreSQL accepts a row value inside COUNT(DISTINCT):
SELECT COUNT(DISTINCT (city, year)) AS distinct_pairs FROM students;| distinct_pairs |
|---|
| 3 |
PostgreSQL returns the same 3 on this data, counting the pair that contains a NULL. MySQL's COUNT(DISTINCT a, b) is documented to skip any combination containing a NULL (not run here), so the same data could give 2 there. Rather than memorise each engine's corner cases, use the subquery form, and if incomplete pairs should not count, say so with WHERE city IS NOT NULL AND year IS NOT NULL.
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);
-- error on purpose
SELECT COUNT(DISTINCT city, year) FROM students;
SELECT COUNT(*) AS distinct_pairs
FROM (SELECT DISTINCT city, year FROM students);Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- COUNT(DISTINCT a, b) is not standard SQL
- SELECT COUNT(*) FROM (SELECT DISTINCT a, b ...) is portable
- PostgreSQL supports COUNT(DISTINCT (a, b))
- Decide whether pairs containing NULL count, and filter explicitly
Common mistakes
- Concatenating columns into one string to count, which can merge different pairs like ('ab','c') and ('a','bc')
- Ignoring rows with NULL in one of the columns
Follow-ups
- How would you count distinct pairs per department?
- Why is concatenating columns a risky shortcut?
Tips
- Show the subquery form first; it works on every engine