January 21st, 2012
SQL Distinct
The DISTINCT statement is used when you do not want to extract duplicate data from database table(s). The basic syntax for DISTINCT is as follows
SELECT DISTINCT column1 FROM table;
SELECT DISTINCT Example Usage
The Persons Table
| ID | First_Name | Last_Name | Gender | Occupation |
|---|---|---|---|---|
| 123 | Fabian | Salvador | Male | Fireman |
| 345 | Abigail | Grant | Female | Fireman |
| 223 | Chloe | Strange | Female | Secretary |
If we want to extract Occupations we will use the SELECT statement. This will result in getting duplicate rows with the same value.
SELECT Occupation FROM Persons
The result would look like this:
| Occupation |
|---|
| Fireman |
| Fireman |
| Secretary |
Using the DISTINCT statement along with SELECT to get the Occupation, we will do the following:
SELECT DISTINCT Occupation FROM Persons
The result will have no duplicates.
| Occupation |
|---|
| Fireman |
| Secretary |