SQL SELECT DISTINCT makes sure that the rows retrieved are distinct by one column or multiple columns.
In this SQL Tutorial, we will see syntax and usage of the DISTINCT keyword with various examples.
There are a lot of duplicate values may reside in a column in any database tables. Sometimes we just want to fetch all the different possible values in that column or unique column value. In such cases, we use the DISTINCT keyword before the column name.
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column
SQL SELECT DISTINCT practical examples
SQL SELECT DISTINCT on one column
SELECT DISTINCT job_title
This query fetches all the unique job_titles in the whole jobs table.
SQL SELECT DISTINCT on multiple columns
SELECT DISTINCT job_id, job_title
This query will list out all the unique combination of job_id and job_title.
SQL SELECT DISTINCT count
SELECT COUNT(DISTINCT job_title)
Basically, we put the DISTINCT column as input to the COUNT function. Hence, it calculates the number of unique jobs available.
SQL SELECT DISTINCT values and COUNT of each
This query will SELECT all unique jobs with number of occurance of them.
This example will not work in MS Access database. However, you can use SQL Subquery to achieve the same.