SQL DISTINCT Keyword

The DISTINCT keyword is used in a SELECT statement to return only unique (non-duplicate) values from a column. When a column contains repeated values, DISTINCT removes those duplicates and shows each unique value only once.

Think of it like getting a list of all the different cities in a school's student database — even if 50 students are from Delhi, the city "Delhi" should appear only once in the list.

The Reference Table

StudentIDStudentNameAgeCityCourse
1Ravi Sharma20DelhiScience
2Priya Mehta22MumbaiCommerce
3Arjun Nair19ChennaiScience
4Sneha Kapoor21DelhiArts
5Rohit Das23KolkataCommerce
6Nisha Verma20MumbaiScience

Syntax

SELECT DISTINCT column_name
FROM table_name;

Without DISTINCT vs With DISTINCT

Without DISTINCT (All Values, Including Duplicates)

SELECT City FROM Students;

Result:

City
Delhi
Mumbai
Chennai
Delhi
Kolkata
Mumbai

Delhi appears twice and Mumbai appears twice.

With DISTINCT (Only Unique Values)

SELECT DISTINCT City FROM Students;

Result:

City
Delhi
Mumbai
Chennai
Kolkata

Each city appears only once, even though Delhi and Mumbai had multiple students.

DISTINCT on Multiple Columns

When DISTINCT is applied to multiple columns, it returns rows where the combination of those column values is unique — not each column individually.

SELECT DISTINCT City, Course FROM Students;

Result:

CityCourse
DelhiScience
MumbaiCommerce
ChennaiScience
DelhiArts
KolkataCommerce
MumbaiScience

Delhi appears twice here because the combinations "Delhi + Science" and "Delhi + Arts" are different. Both rows are unique combinations, so both are returned.

DISTINCT With COUNT

DISTINCT can be used inside COUNT() to count the number of unique values in a column.

SELECT COUNT(DISTINCT City) AS UniqueCities FROM Students;

Result:

UniqueCities
4

There are 4 unique cities: Delhi, Mumbai, Chennai, and Kolkata.

DISTINCT With WHERE

SELECT DISTINCT Course FROM Students
WHERE City = 'Delhi';

Result: Science, Arts — the unique courses taken by students from Delhi.

DISTINCT vs GROUP BY

Both DISTINCT and GROUP BY can be used to find unique values, but they serve different purposes:

FeatureDISTINCTGROUP BY
Primary purposeRemove duplicates from resultGroup rows for aggregation
Used with aggregate functions?Only with COUNT(DISTINCT)Yes — COUNT, SUM, AVG, etc.
Best forGetting a unique list of valuesSummarizing data by groups

Key Points to Remember

  • DISTINCT is placed immediately after SELECT — before the column names.
  • When applied to multiple columns, it removes rows where the entire combination of those columns is duplicated.
  • COUNT(DISTINCT column) counts only the unique values in that column.
  • DISTINCT does not change the data in the table — it only affects the query output.

Summary

The DISTINCT keyword filters out duplicate values from query results, returning only unique entries. It is particularly useful for discovering the range of values in a column — such as all available cities, courses, or categories in a dataset. When combined with COUNT(), it helps measure how many unique options exist within a column.

Leave a Comment

Your email address will not be published. Required fields are marked *