The SQL SELECT DISTINCT statement is used to select distinct(different, unique) value of centain database table column(s), and remove the duplicates from the result. The usage of this statement is same among MS SQL Server, Oracle, MySQL.
SQL SELECT DISTINCT Syntax
SELECT DISTINCT Column1, Column2, ... FROM Table
SQL SELECT DISTINCT Example
Table: Employees
EmployeeId | FirstName | LastName | Department | Salary |
---|---|---|---|---|
203 | Mary | Fox | Finance | 78000 |
204 | Joe | Lanyon | Finance | 45800 |
205 | Sally | Daff | Finance | 57000 |
206 | Grace | Salter | Finance | 62000 |
302 | James | Fox | Development | 75000 |
303 | Dona | Lanyon | Development | 55000 |
304 | Tony | Oakes | Development | 49000 |
Select all the unique last names without duplicates:
SELECT DISTINCT LastName FROM Employees
The result of above statement will look like:
LastName |
---|
Fox |
Lanyon |
Daff |
Salter |
Oakes |
SELECT DISTINCT FirstName, LastName FROM Employees
The above statement will return each unique FirstName and LastName combination. The result of this statement will look like:
FirstName | LastName |
---|---|
Mary | Fox |
Joe | Lanyon |
Sally | Daff |
Grace | Salter |
James | Fox |
Dona | Lanyon |
Tony | Oakes |