DISTINCT
“Which cities do our students come from?” is not the same question as “list every student's city”. The second gives you twelve answers with repeats; the first wants each place named once. distinct is the difference.
1The problem
Asking for the city column plainly gives you one row per student:
Singtam appears three times, Gangtok twice, Namchi twice. If what you wanted was the list of places, you now have to read down the column and cross out repeats by eye.
2distinct does it for you
The keyword goes immediately after select, before the column name:
Twelve rows became six. Each city is named once, in the order it was first met going down the table.
Five grades are represented among the twelve students. Note the order is not sorted — distinct removes repeats, it does not arrange anything. Sorting is order by, coming up shortly, and the two are often used together.
3NULL counts as one of the values
Look again at the city list: NULL is in it, on a row of its own. Two students have no city, and distinct reported “no city” once, exactly as it reported Singtam once.
distinct treats all NULLs as the same thing and lists them once — even though null = null is not true. The aggregate functions in the next chapter go the other way and skip NULLs entirely. Both behaviours are worth remembering, because exam questions turn on them.If you want the list of real cities, exclude the missing ones with the operator from the last lesson: select distinct city from students where city is not null;
4Counting the distinct values
The obvious follow-up question — “how many different cities are there?” — is answered by the row count at the bottom of the resultset: 6 rows in set. There is also a way to get the number by itself, count(distinct city), which will make more sense once count() has been introduced in the next chapter.
5Recap
Lists each different value once.
Straight after select, before the column name.
All the missing values are reported as one NULL row.
Values appear in the order they were first met. Use order by to arrange them.
A table of 12 students has cities Singtam (3), Gangtok (2), Namchi (2), Kolkata (2), Siliguri (1) and 2 with NULL. How many rows does select distinct city from students; return?
Where does the distinct keyword go?
Does distinct sort the values?