skip to today's puzzle
SQL Sip

COUNT(DISTINCT ...) counts different values

COUNT(*) counts rows. COUNT(DISTINCT class_id) first folds repeated values together, then counts what is left. Twenty students spread across four classes is twenty rows but four different class_id values — the question you are asking decides which of the two numbers you want.

A tiny example

In plain English: Tell me how many different classes the students are spread across.

SELECT COUNT(DISTINCT class_id)
FROM students;

What comes back

COUNT(DISTINCT class_id)
4

The 24 students sit in only 4 different classes — repeats count once, so the answer is 4, not 24.

Related SQL lessons

Ready for a break?

Take on today's cozy puzzle. A fresh, bite-sized query waits every morning — no timers, no pressure.

play today's sip