SQL SELECT DISTINCT: Getting Unique Values

In SQL, the SELECT DISTINCT statement is used to retrieve unique values, eliminating duplicate rows of data.

Sometimes when using the SELECT statement to retrieve data from a table, we may get duplicate rows. Using SELECT DISTINCT can ensure that the results returned only contain unique rows.

For example, consider the following Customers table:

1
John
New York

2
Mary
London

3
John
Paris

4
Peter
New York

If we run the following query:

SELECT CustomerName FROM Customers;

Will yield the following results:

CustomerName
------------
John
Mary
John
Peter

If we use SELECT DISTINCT to perform the query:

SELECT DISTINCT CustomerName FROM Customers;

The following results will be returned:

CustomerName
------------
John
Mary
Peter

By using SELECT DISTINCT, we only return unique CustomerName values.

bannerAds