Select Distinct
Written By Bear
The DISTINCT keyword can be used to return only distinct (unique) values. It is often used to remove duplicates from query results.
SQL Select DISTINCT Syntax:
SELECT DISTINCT
Column1,
Column2,
Column3
FROM Table_Name
Example:
Lets get the distinct ReasonTypes from the following table.
Name |
ReasonType |
---|---|
Price |
Other |
On Promotion |
Promotion |
Magazine Advertisement |
Marketing |
Television Advertisement |
Marketing |
Manufacturer |
Other |
Review |
Other |
Demo Event |
Marketing |
Sponsorship |
Marketing |
Quality |
Other |
Other |
Other |
SELECT Distinct ReasonType
FROM SalesReason
Results:
ReasonType |
---|
Marketing |
Other |
Promotion |
Distinct can be used with aggregates COUNT, AVG, MAX, etc.
SELECT COUNT
(DISTINCT Column1)
FROM Table_Name
Ex:
SELECT Count
(Distinct ReasonType)
FROM SalesReason
Results:
(No column name) |
---|
3 |
SQL Tutorial