Full answer
The three forms answer three different questions.
COUNT(*): how many rows are there? NULLs do not matter, because it counts rows, not values.COUNT(x): how many rows have a value in x? NULLs are skipped.COUNT(DISTINCT x): how many different values does x take? NULLs are skipped, and duplicates are counted once.
sql
WITH t(x) AS (VALUES (5), (NULL), (5), (8))
SELECT COUNT(*) AS count_star,
COUNT(x) AS count_x,
COUNT(DISTINCT x) AS count_distinct_x
FROM t;| count_star | count_x | count_distinct_x |
|---|---|---|
| 4 | 3 | 2 |
The gap between COUNT(*) and COUNT(x) is often useful on purpose. For example, the number of enrolments that still have no marks entered:
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 COUNT(*) AS enrolments,
COUNT(marks) AS marked,
COUNT(*) - COUNT(marks) AS waiting_for_marks
FROM enrolments;| enrolments | marked | waiting_for_marks |
|---|---|---|
| 8 | 7 | 1 |
COUNT(1) and COUNT('anything') behave exactly like COUNT(*), because the constant is never NULL. There is no speed difference worth arguing about in modern engines.
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);
WITH t(x) AS (VALUES (5), (NULL), (5), (8))
SELECT COUNT(*) AS count_star,
COUNT(x) AS count_x,
COUNT(DISTINCT x) AS count_distinct_x
FROM t;
SELECT COUNT(*) AS enrolments,
COUNT(marks) AS marked,
COUNT(*) - COUNT(marks) AS waiting_for_marks
FROM enrolments;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- COUNT(*) counts rows
- COUNT(col) counts non-NULL values
- COUNT(DISTINCT col) counts distinct non-NULL values
- COUNT(*) - COUNT(col) gives the number of NULLs
Common mistakes
- Using COUNT(col) to count rows on a nullable column
- Expecting COUNT(DISTINCT col) to count NULL as a value
Follow-ups
- How do you count distinct pairs of columns?
- What does COUNT return on an empty table?
Tips
- Answer with the three numbers first, then the rule behind each