Introduction

In a table, a column may contain many duplicate values. DISTINCT keyword can be used to return only distinct values. It can be used with aggregates i.e. COUNT, AVG, MAX, etc.

Syntax

SELECT DISTINCT
    column_name
FROM
    table_name;

Above query returns only distinct values in the specified column. To find distinct values from multiple column

SELECT DISTINCT
 column_name1,
 column_name2 ,
 ...
FROM
 table_name;

The query uses the combination of values in all specified columns in the SELECT list to evaluate the uniqueness. Below syntax uses DISTINCT with aggregates.

SELECT
COUNT (DISTINCT column_name)
FROM table_name

Example

Consider below table

Database: DISTINCT Example
Employee Table

Below example filters the distinct result set based on age and city.

SELECT DISTINCT age FROM employleeinfo

/* Query Result */
age
20
17
19
50
40
43
30


SELECT DISTINCT age,city FROM employleeinfo

/* Query Result */
age city
20  Noida
17  Delhi
19  Patna
50  Mumbai
40  Chennai
50  Pune
43  Delhi
30  Lucknow

Reference

SQL SELECT DISTINCT Keyword