The SQL DISTINCT keyword is used to remove duplicate values from the result set. When a table contains repeated data, DISTINCT helps return only unique records.
In real-world databases, duplicate values are common. For example:
- Multiple students may belong to the same city.
- Many employees can work in the same department.
- Several products may have the same category.
Using the DISTINCT keyword ensures that duplicate values appear only once in the query result. SQL DISTINCT Clause practice questions with solutions help to understand the concepts.
The DISTINCT keyword is commonly used in:
- Reports
- Data Analysis
- Business Intelligence Dashboards
- Customer Analytics
- Data Cleaning
What is SQL DISTINCT?
The DISTINCT keyword removes duplicate rows from the query result.
Basic Syntax
SELECT DISTINCT column_name
FROM table_name;
Multiple Columns
SELECT DISTINCT city,
course
FROM students;
In this case, SQL returns only unique combinations of city and course.
Sample Table Used Throughout This Chapter
students
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 106 | Ankit | 22 | Python | Delhi | 82 |
| 107 | Sneha | 21 | SQL | Noida | 89 |
Why Use DISTINCT?
The DISTINCT keyword is useful for:
- Removing duplicate values
- Finding unique cities
- Listing unique courses
- Identifying unique departments
- Data cleaning
- Report generation
- Business analysis
1. SQL Query to Display Unique Cities
Problem Statement
Write an SQL query to display all unique cities from the students table.
SQL Solution
SELECT DISTINCT city
FROM students;
Sample Output
| city |
|---|
| Delhi |
| Noida |
| Gurgaon |
| Faridabad |
Explanation
Although multiple students belong to Delhi and Noida, the DISTINCT keyword returns each city only once.
Without DISTINCT, duplicate city names would also appear.
Concepts Covered
- DISTINCT
- Unique Values
- Duplicate Removal
2. SQL Query to Display Unique Courses
Problem Statement
Write an SQL query to display all unique courses offered.
SQL Solution
SELECT DISTINCT course
FROM students;
Sample Output
| course |
|---|
| Python |
| Java |
| SQL |
Explanation
Even though multiple students are enrolled in the same course, DISTINCT removes duplicate course names.
Concepts Covered
- DISTINCT
- Text Data
- Duplicate Records
3. SQL Query to Display Unique Ages
Problem Statement
Write an SQL query to display all unique student ages.
SQL Solution
SELECT DISTINCT age
FROM students;
Sample Output
| age |
|---|
| 20 |
| 21 |
| 22 |
| 23 |
Explanation
The DISTINCT keyword removes repeated age values and returns only unique ages available in the table.
Concepts Covered
- DISTINCT
- Numeric Data
- Unique Records
4. SQL Query to Display Unique Marks
Problem Statement
Write an SQL query to display all unique marks scored by students.
Sample Table
| id | name | age | course | city | marks |
|---|---|---|---|---|---|
| 101 | Rahul | 21 | Python | Delhi | 88 |
| 102 | Amit | 22 | Java | Noida | 91 |
| 103 | Neha | 20 | SQL | Delhi | 95 |
| 104 | Priya | 23 | Python | Gurgaon | 84 |
| 105 | Rohit | 21 | Java | Faridabad | 90 |
| 106 | Ankit | 22 | Python | Delhi | 82 |
| 107 | Sneha | 21 | SQL | Noida | 89 |
SQL Solution
SELECT DISTINCT marks
FROM students;
Sample Output
| marks |
|---|
| 82 |
| 84 |
| 88 |
| 89 |
| 90 |
| 91 |
| 95 |
Explanation
The DISTINCT keyword removes duplicate marks (if any exist) and displays each unique mark only once.
This is useful for:
- Exam analysis
- Score distribution
- Report generation
Concepts Covered
- DISTINCT
- Numeric Data
- Duplicate Removal
5. SQL Query Using DISTINCT with Multiple Columns
Problem Statement
Write an SQL query to display all unique combinations of City and Course.
Sample Table
| id | name | course | city |
|---|---|---|---|
| 101 | Rahul | Python | Delhi |
| 102 | Amit | Java | Noida |
| 103 | Neha | SQL | Delhi |
| 104 | Priya | Python | Gurgaon |
| 105 | Rohit | Java | Faridabad |
| 106 | Ankit | Python | Delhi |
| 107 | Sneha | SQL | Noida |
SQL Solution
SELECT DISTINCT city,
course
FROM students;
Sample Output
| city | course |
|---|---|
| Delhi | Python |
| Noida | Java |
| Delhi | SQL |
| Gurgaon | Python |
| Faridabad | Java |
| Noida | SQL |
Explanation
When multiple columns are used with DISTINCT, SQL checks the entire row combination.
Notice that:
- Delhi + Python appears only once, even though multiple students belong to this combination.
- Delhi + SQL is considered a different combination.
- Noida + Java and Noida + SQL are also different.
This feature is commonly used in reporting and analytics to identify unique combinations of data.
Concepts Covered
- DISTINCT
- Multiple Columns
- Unique Combinations
- Duplicate Row Removal
6. SQL DISTINCT with WHERE Clause
Problem Statement
Write an SQL query to display all unique cities where students scored more than 85 marks.
Sample Table
| id | name | course | city | marks |
|---|---|---|---|---|
| 101 | Rahul | Python | Delhi | 88 |
| 102 | Amit | Java | Noida | 91 |
| 103 | Neha | SQL | Delhi | 95 |
| 104 | Priya | Python | Gurgaon | 84 |
| 105 | Rohit | Java | Faridabad | 90 |
| 106 | Ankit | Python | Delhi | 82 |
| 107 | Sneha | SQL | Noida | 89 |
SQL Solution
SELECT DISTINCT city
FROM students
WHERE marks > 85;
Sample Output
| city |
|---|
| Delhi |
| Noida |
| Faridabad |
Explanation
SQL performs the operations in this order:
- Apply the
WHEREcondition (marks > 85). - Remove duplicate city names using
DISTINCT.
Although Delhi appears more than once in the filtered data, it is displayed only once.
Concepts Covered
- DISTINCT
- WHERE Clause
- Duplicate Removal
- Filtering Records
7. SQL DISTINCT with ORDER BY Clause
Problem Statement
Write an SQL query to display all unique cities sorted alphabetically.
SQL Solution
SELECT DISTINCT city
FROM students
ORDER BY city ASC;
Sample Output
| city |
|---|
| Delhi |
| Faridabad |
| Gurgaon |
| Noida |
Explanation
The query first removes duplicate cities using DISTINCT.
Then, the ORDER BY clause sorts the remaining unique city names alphabetically.
Concepts Covered
- DISTINCT
- ORDER BY
- ASC
- Sorting Unique Values
8. SQL Query to Display Unique Cities of Python Students
Problem Statement
Write an SQL query to display unique cities where students are enrolled in the Python course.
SQL Solution
SELECT DISTINCT city
FROM students
WHERE course = 'Python';
Sample Output
| city |
|---|
| Delhi |
| Gurgaon |
Explanation
The query first filters only students studying Python.
After filtering, DISTINCT removes duplicate city names.
Although two students from Delhi study Python, Delhi appears only once.
Concepts Covered
- DISTINCT
- WHERE
- Text Filtering
9. SQL Query to Display Unique Courses for Students Scoring More Than 85 Marks
Problem Statement
Write an SQL query to display all unique courses where students scored more than 85 marks.
SQL Solution
SELECT DISTINCT course
FROM students
WHERE marks > 85;
Sample Output
| course |
|---|
| Python |
| Java |
| SQL |
Explanation
The query filters students whose marks are greater than 85.
Then, DISTINCT returns only unique course names.
This type of query is useful for finding which courses have high-performing students.
Concepts Covered
- DISTINCT
- WHERE
- Unique Records
10. SQL Query to Display Unique Ages Greater Than 20
Problem Statement
Write an SQL query to display all unique ages greater than 20.
SQL Solution
SELECT DISTINCT age
FROM students
WHERE age > 20;
Sample Output
| age |
|---|
| 21 |
| 22 |
| 23 |
Explanation
The WHERE clause filters ages greater than 20, and the DISTINCT keyword removes duplicate age values.
Only unique ages satisfying the condition are displayed.
Concepts Covered
- DISTINCT
- WHERE
- Numeric Filtering
- Duplicate Removal
11. SQL DISTINCT with Multiple Columns and ORDER BY
Problem Statement
Write an SQL query to display all unique combinations of city and course, sorted alphabetically by city.
Sample Table
| id | name | course | city |
|---|---|---|---|
| 101 | Rahul | Python | Delhi |
| 102 | Amit | Java | Noida |
| 103 | Neha | SQL | Delhi |
| 104 | Priya | Python | Gurgaon |
| 105 | Rohit | Java | Faridabad |
| 106 | Ankit | Python | Delhi |
| 107 | Sneha | SQL | Noida |
SQL Solution
SELECT DISTINCT city,
course
FROM students
ORDER BY city ASC;
Sample Output
| city | course |
|---|---|
| Delhi | Python |
| Delhi | SQL |
| Faridabad | Java |
| Gurgaon | Python |
| Noida | Java |
| Noida | SQL |
Explanation
The query first removes duplicate city-course combinations and then sorts the remaining records alphabetically by city.
This is useful for generating location-wise course reports.
Concepts Covered
- DISTINCT
- Multiple Columns
- ORDER BY
- ASC
12. SQL DISTINCT with DESC Sorting
Problem Statement
Write an SQL query to display all unique cities sorted in descending alphabetical order.
SQL Solution
SELECT DISTINCT city
FROM students
ORDER BY city DESC;
Sample Output
| city |
|---|
| Noida |
| Gurgaon |
| Faridabad |
| Delhi |
Explanation
The DISTINCT keyword removes duplicate city names.
The ORDER BY DESC clause arranges the remaining cities from Z to A.
Concepts Covered
- DISTINCT
- ORDER BY
- DESC
- Sorting Text Data
13. SQL Query to Display Unique Courses in Delhi
Problem Statement
Write an SQL query to display all unique courses available for students living in Delhi.
SQL Solution
SELECT DISTINCT course
FROM students
WHERE city = 'Delhi';
Sample Output
| course |
|---|
| Python |
| SQL |
Explanation
The query first filters records where the city is Delhi.
Then, DISTINCT removes duplicate course names.
This helps identify all courses available in a particular location.
Concepts Covered
- DISTINCT
- WHERE
- Text Filtering
14. SQL Query to Display Unique Marks in Descending Order
Problem Statement
Write an SQL query to display all unique marks, sorted from highest to lowest.
SQL Solution
SELECT DISTINCT marks
FROM students
ORDER BY marks DESC;
Sample Output
| marks |
|---|
| 95 |
| 91 |
| 90 |
| 89 |
| 88 |
| 84 |
| 82 |
Explanation
The query removes duplicate marks (if any) and sorts the remaining unique values in descending order.
This type of query is useful for:
- Score analysis
- Ranking systems
- Statistical reporting
Concepts Covered
- DISTINCT
- ORDER BY
- DESC
- Numeric Sorting
15. SQL DISTINCT Using Multiple Conditions
Problem Statement
Write an SQL query to display all unique cities where students:
- Study Python
- Scored more than 80 marks
SQL Solution
SELECT DISTINCT city
FROM students
WHERE course = 'Python'
AND marks > 80;
Sample Output
| city |
|---|
| Delhi |
| Gurgaon |
Explanation
This query combines:
WHEREANDDISTINCT
SQL first filters students who satisfy both conditions:
- Course = Python
- Marks > 80
Finally, duplicate city names are removed using DISTINCT.
This type of query is commonly used in business analytics to identify unique locations based on multiple criteria.
Concepts Covered
- DISTINCT
- WHERE
- AND
- Multiple Conditions
- SQL Filtering
Chapter Summary
In this chapter, you learned how to use the SQL DISTINCT keyword to remove duplicate records from query results. The DISTINCT keyword is especially useful when working with large databases that contain repeated values.
You explored how to retrieve unique values from a single column, unique combinations from multiple columns, and how to combine DISTINCT with the WHERE and ORDER BY clauses to create more meaningful queries.
These techniques are widely used in data analysis, reporting, business intelligence, and database management.
Throughout this chapter, you covered:
- Introduction to the
DISTINCTKeyword - Removing Duplicate Values
- DISTINCT on a Single Column
- DISTINCT on Multiple Columns
- DISTINCT with
WHERE - DISTINCT with
ORDER BY - DISTINCT with Multiple Conditions
- Real-world SQL Examples
By mastering DISTINCT, you can generate cleaner reports and avoid duplicate information in your SQL query results.
Key Takeaways
DISTINCTremoves duplicate values from the result set.- It works with both numeric and text data.
- It can be used on one or multiple columns.
DISTINCTis applied after filtering with theWHEREclause.ORDER BYcan sort the unique values returned byDISTINCT.- Multiple columns with
DISTINCTreturn unique combinations, not unique values for each column separately. DISTINCTimproves report readability and data quality.- It is commonly used in dashboards, analytics, and business reports.
DISTINCTdoes not modify the original data in the table.- It is one of the most frequently asked SQL interview topics.
Frequently Asked Questions (FAQs)
1. What is the SQL DISTINCT keyword?
The DISTINCT keyword removes duplicate values from the query result.
Example:
SELECT DISTINCT city
FROM students;
2. Does DISTINCT change the original table?
No.
DISTINCT only removes duplicate values from the query result. The actual data stored in the database remains unchanged.
3. Can DISTINCT be used with multiple columns?
Yes.
Example:
SELECT DISTINCT city,
course
FROM students;
SQL returns only unique combinations of city and course.
4. Can DISTINCT be used with WHERE?
Yes.
Example:
SELECT DISTINCT city
FROM students
WHERE marks > 90;
The WHERE clause filters the data first, and then DISTINCT removes duplicate values.
5. Can DISTINCT be used with ORDER BY?
Yes.
Example:
SELECT DISTINCT city
FROM students
ORDER BY city ASC;
The query first removes duplicate cities and then sorts the remaining values alphabetically.
6. When should I use DISTINCT?
Use DISTINCT when you need:
- Unique cities
- Unique courses
- Unique departments
- Unique product categories
- Unique customer names
- Clean reports without duplicates
7. What is the difference between DISTINCT and GROUP BY?
- DISTINCT removes duplicate values from the result set.
- GROUP BY groups rows for aggregate calculations such as
COUNT(),SUM(),AVG(),MIN(), andMAX().
Example:
SELECT DISTINCT course
FROM students;
Example using GROUP BY:
SELECT course,
COUNT(*)
FROM students
GROUP BY course;
8. Where is DISTINCT used in real-world applications?
The DISTINCT keyword is commonly used in:
- Banking Applications
- Hospital Management Systems
- School and College Databases
- Customer Relationship Management (CRM)
- E-commerce Websites
- HR Management Systems
- Sales Dashboards
- Business Intelligence Reports
- Inventory Management Systems
- Data Analytics Projects
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
