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
| StudentID | StudentName | Age | City | Course |
|---|---|---|---|---|
| 1 | Ravi Sharma | 20 | Delhi | Science |
| 2 | Priya Mehta | 22 | Mumbai | Commerce |
| 3 | Arjun Nair | 19 | Chennai | Science |
| 4 | Sneha Kapoor | 21 | Delhi | Arts |
| 5 | Rohit Das | 23 | Kolkata | Commerce |
| 6 | Nisha Verma | 20 | Mumbai | Science |
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:
| City | Course |
|---|---|
| Delhi | Science |
| Mumbai | Commerce |
| Chennai | Science |
| Delhi | Arts |
| Kolkata | Commerce |
| Mumbai | Science |
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:
| Feature | DISTINCT | GROUP BY |
|---|---|---|
| Primary purpose | Remove duplicates from result | Group rows for aggregation |
| Used with aggregate functions? | Only with COUNT(DISTINCT) | Yes — COUNT, SUM, AVG, etc. |
| Best for | Getting a unique list of values | Summarizing data by groups |
Key Points to Remember
DISTINCTis placed immediately afterSELECT— 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.DISTINCTdoes 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.
