DISTINCT with Multiple SELECT List Items

When there is more than one item in the SELECT list, DISTINCT finds the rows where the combination of items is unique.

Here's an example. First, take a look at the listing of pub_ids and types in the titles table.

SQL
select pub_id, type
from titles
order by pub_id
pub_id type
====== ============
0736   psychology
						0736   psychology
0736   business
0736   psychology
						0736   psychology
						0736   psychology
0877   mod_cook
0877   trad_cook
						0877   trad_cook
0877   mod_cook
0877   trad_cook
0877   (NULL)
1389   popular_comp
1389   business
1389   business
1389   popular_comp
1389   business
1389   popular_comp
[18 rows]

There are eighteen rows. Notice that there is some repetition. (The ORDER BY is there only to make the display easy to read.) If ...

Get Practical SQL Handbook, The: Using SQL Variants, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.