SQL online course

SQL SELECT DISTINCT Statement

Learn how SQL SELECT DISTINCT removes duplicate values from query results, including single columns, column combinations, filtering, and ordering.

The SQL SELECT DISTINCT statement returns unique values or unique combinations of values from a table. It is useful when you want to see which cities, states, categories, or other values are represented without displaying repetitions.

A normal SELECT can return the same value many times because different table rows may contain that value. DISTINCT removes duplicate rows from the query's result set. It does not delete, update, or otherwise modify data stored in the table.

What SELECT DISTINCT Means

SELECT is the SQL clause used to choose columns or expressions for a result. A column is a named field in a table, such as city. A table is a collection of rows and columns, such as Customer.

DISTINCT is a keyword that removes duplicate selected values or duplicate combinations of selected values. The rows returned by a query are called its result set.

For example, if three customer rows contain the city values Hope, Harmony, and Hope, an ordinary city selection can return all three values. A distinct city selection returns Hope and Harmony once each.

Basic SELECT DISTINCT Syntax

SELECT DISTINCT column_name
FROM table_name;

For multiple columns, write the column list after DISTINCT:

SELECT DISTINCT column_name1, column_name2
FROM table_name;
PartPurpose
SELECTChooses the columns or expressions to return.
DISTINCTRemoves duplicate selected rows from the result set.
Column listNames the values used to build each result row.
FROMIdentifies the source table.
Source tableProvides the rows being queried.

DISTINCT must appear immediately after SELECT. For example, SELECT city DISTINCT FROM Customer; is not the correct form.

Example Customer Table

Use this Customer table to see how repeated values are handled:

idnameaddresscitystatezip
1Bill Smith123 Main StreetHopeCA98765
2Mary Smith123 Dorian StreetHarmonyAZ98765
3Bob Smith123 Laugh StreetHopeCA98765

The city Hope occurs in two source rows. The city Harmony occurs in one source row.

Single-Column Distinct Results

To list each city represented by customers, select only the city column and place DISTINCT immediately after SELECT:

SELECT DISTINCT city
FROM Customer;

The result set contains one row for each unique city value:

city
Hope
Harmony

Although Hope appears in two Customer rows, it appears only once in this result. With one selected column, uniqueness is determined by the value in that column.

Comparing SELECT with SELECT DISTINCT

An ordinary query returns the selected value from every qualifying source row:

SELECT city
FROM Customer;
city
Hope
Harmony
Hope

The distinct version removes the repeated result value:

SELECT DISTINCT city
FROM Customer;
city
Hope
Harmony

This distinguishes duplicate source rows from duplicate values in a selected column. The two customers in Hope are different customers, but their selected city values are the same.

How Uniqueness Is Determined

When one column is selected, DISTINCT keeps one result row for each different value:

SELECT DISTINCT city
FROM Customer;

When multiple columns are selected, DISTINCT evaluates the complete combination of selected values. It does not independently make each column unique.

SELECT DISTINCT city, state
FROM Customer;

Here, uniqueness applies to each city-and-state pair. A pair such as Hope, CA is returned once, but Hope, CA and Hope, AZ would be different combinations and could both appear.

Adding another column can make rows different. For example:

SELECT DISTINCT city, name
FROM Customer;

The two Hope rows have different names, so both combinations can appear. If the goal is one row per city, select only city, or select the specific columns that define the desired combination.

Using DISTINCT with WHERE

WHERE filters source rows according to a condition. You can combine it with DISTINCT to find unique values among only the rows that pass the filter.

SELECT DISTINCT city
FROM Customer
WHERE state = 'CA';

The query first considers customers whose state is CA. It then removes repeated city values from those qualifying rows. In this example, the result contains Hope once.

This makes SELECT DISTINCT a query-reading and result-shaping technique, not a table-cleanup operation. For more filtering examples, see the SQL WHERE Clause lesson.

Result Ordering

DISTINCT removes duplicates, but it does not guarantee alphabetical or numerical order. The database may return unique rows in an order that varies between executions.

Use ORDER BY when the presentation order matters:

SELECT DISTINCT city
FROM Customer
ORDER BY city;

This requests the distinct cities in ascending order. See the SQL ORDER BY Clause lesson for more sorting options.

Common Problems and Fixes

ProblemLikely causeFix
Repeated values still appearMore than one column was selected, and the complete combinations differ.Select only the column or columns that define the needed uniqueness.
Results are not alphabeticalDISTINCT does not sort results.Add ORDER BY city.
Every customer row appearsThe query uses ordinary SELECT city or includes unique columns such as id or name.Use SELECT DISTINCT city and omit columns that make each row different.
Duplicate records were not deletedDISTINCT was confused with a data-modification statement.Remember that it affects only query output. Deleting or consolidating stored data requires a separate data-management operation.

Exam-Relevant Notes

  • DISTINCT appears immediately after SELECT.
  • With one selected column, each unique value appears once.
  • With multiple selected columns, uniqueness applies to the complete selected combination.
  • DISTINCT does not change stored table data.
  • WHERE limits source rows before the distinct result is produced.
  • ORDER BY is needed when a predictable result order is required.

Related SQL Concepts

Before using DISTINCT, review the basic SQL SELECT Statement and SQL Syntax. For counting unique values, compare this statement with SQL COUNT Function and the related pattern COUNT(DISTINCT column_name).

GROUP BY and aggregate functions can also summarize rows, but SELECT DISTINCT is the direct choice when you only need unique selected values or combinations.