Removing Duplicates

It's true—SQL is not a formatting tool. There are some things it just can't do or can't do without expensive gyrations. Sometimes you simply have to give up and turn your results over to a good report writer. Removing duplicated elements from output to produce a nicely formatted display is one of those cases.

But hope springs eternal: More than one Internet respondent has wondered if there is any SQL method for cleaning up these kinds of results:

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 ...

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.