The following SQL uses DISTINCT to find a list of unique first names from all people in the ‘Persons’ table.
/* Answer */ SELECT DISTINCT FirstName FROM Person.Person ORDER BY FirstName
Check out the results, each first name is listed once.
Here is an example using DISTINCT with two columns.
/* Answer */ SELECT DISTINCT FirstName, LastName FROM Person.Person ORDER BY FirstName
In summary, use the DISTINCT Statement to eliminate duplicate value from a result of one or more columns.
DISTINCT Statement Resources
To learn more about the DISTINCT keyword, check out these useful resources:
- An in-depth article on SQL DISTINCT and TOP in the Same Query
- The recommended book SQL Practice Problems: 57 beginning, intermediate, and advanced challenges for you to solve using a “learn-by-doing” approach