It is expected that you have visited the 'Select' statement examples and have a basic understanding of T-SQL.
Here the 'DISTINCT' statements is executed on the 'Customers' table.
The Distinct Statement
The 'DISTINCT' statement is used to remove duplicate records or rows from a 'SELECT' statement call.
For example, if you want to see the various cities that your customers are from,
but without repeating the cities, you can use the 'distinct' clause in your
'SELECT' statement. The DISTINCT clause can be used with SELECT statements only.
SQL Distinct Syntax
Select Distinct column_name FROM table_name
The Distinct Statement Example
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 |
The
Select * from Customers displays all the columns and data in them from the
CUSTOMERS table. As you can see some of the city names are repeated as shown
above.
Select Distinct City from Customers
The above
Distinct statement removes the repeating data and displays only the first
occurence of the city name in this case.
Berlin |
London |
Madrid |
NewJersy |
Paris |
San Francisco |