SQL de duplication distinct syntax

Time:2020-9-30

SQL select distinct statement

Duplicate values may be included in the table. This is not a problem, but sometimes you may want to list only distinct values.

The keyword distinct is used to return a unique different value

grammar

Select distinct column name from table name

 


 

Use the distinct keyword

To select all the values from the company column, we need to use the select statement:

SELECT Company FROM Orders

Orders table

 

Company OrderNumber
IBM 3532
W3School 2356
Apple 4698
W3School 6953

result:

Company
IBM
W3School
Apple
W3School

Take a closer look. In the result set, w3school is listed twice.

To select only the only different value from the company column, we need to use the select distinct statement:

 SELECT DISTINCT Company FROM Orders

result:

Company
IBM
W3School
Apple

 

Now, “w3school” is listed only once in the result set