This chapter explains the SELECT DISTINCT statement.
SQL SELECT DISTINCT Statement
In a table, you might include duplicate values. This is not a problem, but sometimes you may want to list only the different values (distinct).
Keyword DISTINCT is used to return only a different value.
Grammar:
SELECT DISTINCT column name from table name
Using DISTINCT Keywords
If you want 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 |
Sqlkey |
6953 |
Results:
Company |
Ibm |
W3school |
Apple |
Sqlkey |
Note that in the result set, the Sqlkey is listed two times.
To select only a different value from the company column, we need to use the Select DISTINCT statement:
DISTINCT
Company from Orders
Results:
Now, in the result set, "Sqlkey" is listed only once.