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

Get SQL: Visual QuickStart Guide 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.