The DISTINCT keyword is used to return only distinct (different) values.
The SELECT statement returns information from table columns. But what if we only want to select distinct elements?
With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement:
|
SELECT DISTINCT column_name(s) FROM table_name |
To select ALL values from the column named "Company" we use a SELECT statement like this:
|
SELECT Company FROM Orders |
"Orders" table
|
Company |
OrderNumber |
|
Sega |
3412 |
|
Velocityscape |
2312 |
|
Trio |
4678 |
|
Velocityscape |
6798 |
Result
|
Company |
|
Sega |
|
Velocityscape |
|
Trio |
|
Velocityscape |
Note that "Velocityscape" is listed twice in the result-set.
To select only DIFFERENT values from the column named "Company" we use a SELECT DISTINCT statement like this:
|
SELECT DISTINCT Company FROM Orders |
Result:
|
Company |
|
Sega |
|
Velocityscape |
|
Trio |
Now "Velocityscape" is listed only once in the result-set.