O'Reilly logo

Practical SQL Handbook, The: Using SQL Variants, Fourth Edition by Sandra L. Emerson, Marcy Darnovsky, Judith S. Bowman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Distinguishing DISTINCTs

Given a table with two identifying columns, how do you find all the unique combinations? DISTINCT seems like an obvious answer, but more than one user on the Internet complained of getting nothing but syntax errors or confusing results from what seemed like straightforward DISTINCT queries.

This is probably because DISTINCT occurs in two unique forms:

  • With columns or expressions

  • With aggregates

The syntax of these two cases differs just enough to cloud the occasional user's judgment.

With column names and expressions, you use DISTINCT just once, and it applies to everything that comes after it. It is the first word in the select list and requires no parentheses. When you opt to use DISTINCT this way, you're locked in; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required