Learn From - Interactive SQL



The SQL DISTINCT Statement


The SQL DISTINCT keyword is used along with the SELECT statement to eliminate all the duplicate records and fetch only unique records.

SQL DISTINCT Syntax


SELECT DISTINCT column_name1,column_name2,column_name3 ...
FROM table_name

The SQL DISTINCT Example


Below is shown the "Customers" table:

CustomerID CustomerName EmailID Address City PostalCode Country
C001 Fredrick Johnson FJohnson@yahoo.com Berkeley Gardens 12 Brewery London WX1 6LT UK
C002 Mary Davidson Mary@gmail.com 35 King George Madrid 28023 Spain
C003 George Clooney George@hotmail.com Obere Str. 57 Berlin 12209 Germany
C004 David Louis David@mail.com 2743 Bering St. NewJersy 07019 USA
C005 Johnny Anderson Johnny@gmail.com 55 Grizzly Peak Rd. NewJersy 07019 USA
C006 Paul Henriot PaulHenriot@yahoo.com 59 rue de l'Abbaye Paris 51100 France
C007 Henry King Henry@mail.com 120 Hanover Sq. London WA1 1DP UK
C008 Robert John Robert@gmail.com 87 Polk St. Suite 5 San Francisco 94117 USA

Type or Copy the SQL into the Text Editor, click the "View Result" button, to see the result.

Retrieve all the unique countries from the Customers table.

Select DISTINCT Country from Customers;

Note: After execution, in the output you can see that no country name is repeated. Out of 8 records only 5 records are retrieved.

Retrieve all unique cities and countries from the Customers table

Select DISTINCT Country, City from Customers;

Note: After execution, in the output you can see only 6 records are retrieved.

SQL in Text Editor:









Show Top