A DISTINCT clause is used in the select statement to return only the distinct values from a data set.
Consider the employee table as shown below:
| id | first_name | last_name | department | salary |
| 101 | bryan | walker | sales | 50,000 |
| 102 | alex | carpenter | accounts | 55,000 |
| 103 | sam | white | sales | 60,000 |
| 104 | john | barry | sales | 54,000 |
| 105 | bill | wood | accounts | 65,000 |
| 106 | robert | hopkins | support | 70,000 |
| 107 | david | lee | support | 72,000 |
| 108 | jack | sears | support | 65,000 |
| 109 | tom | young | sales | 55,000 |
I would like to query all the departments from the employee table.
SELECT department FROM employee; department ---------- sales accounts sales sales accounts support support support sales
But what if I would like to return each department name only once ? DISTINCT clause does just that. It removes duplicates from the result set.
SELECT DISTINCT department FROM employee; department ---------- sales accounts support
Also check out DISTINCT ON clause and COUNT DISTINCT.