|
|
The SELECT DISTINCT Statement |
| |
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: |
| |
Syntax |
| |
SELECT DISTINCT column_name(s)
FROM table_name |
|
| |
Using the DISTINCT keyword |
| |
To select ALL values from the column named "Company" we use a SELECT statement like this: |
| |
| SELECT Company FROM Orders |
|
| |
"Orders" table |
| |
| Company |
OrderNumber |
| Sheela |
3412 |
| 365edu |
2312 |
| CIQ |
4678 |
| 365edu |
6798 |
|
| |
| Result |
| |
| Company |
| Sheela |
| 365edu |
| CIQ |
| 365edu |
|
| |
Note that "W3Schools" is listed twice in the result-set. |
| |
| SELECT DISTINCT Company FROM Orders |
|
| |
Result: |
| |
| Company |
| Sheela |
| 365edu |
| CIQ |
|
| |
Now "365edu" is listed only once in the result-set. |
| |
| |
| |
| |
| |
| |