O'Reilly logo

SQL: Visual QuickStart Guide by Chris Fehily

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

Eliminating Duplicate Rows with DISTINCT

Columns often contain duplicate values, and it’s common to want a result that lists each duplicate only once. If I type Listing 4.6 to list the states where the authors live, the result, Figure 4.6, contains unneeded duplicates. The DISTINCT keyword eliminates duplicate rows from a result.

Listing 4.6. List the states in which the authors live. See Figure 4.6 for the result.
SELECT state
  FROM authors;
Figure 4.6. Result of Listing 4.6. This result contains unneeded duplicates of CA and NY.
state
-----
NY
CO
CA
					CA
					NY
					CA
FL

To eliminate duplicate rows:

  • Type:
    SELECT DISTINCT columns
      FROM table;
    
    columns is one or more comma-separated column names, and table is the name of the table that contains ...

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