What is the Purpose of the DISTINCT Keyword in SQL?
The DISTINCT keyword in SQL is used to remove duplicate values from query results.
In simple words:
DISTINCT returns only unique records from a table.
Why DISTINCT is Important
Enterprise databases often contain:
- Duplicate values
- Repeated records
- Redundant data entries
DISTINCT helps:
- Retrieve unique values
- Improve report clarity
- Reduce duplicate results
- Support analytics and filtering
Simple Real-Life Example
Think about:
- A student database with repeated department names
Without DISTINCT
IT IT HR HR Finance
With DISTINCT
IT HR Finance
DISTINCT Internal Architecture
Read Rows
|
v
Compare Values
|
v
Remove Duplicate Entries
|
v
Return Unique Records
DISTINCT Syntax
SELECT DISTINCT column_name FROM table_name;
Students Table Example
| Student | Department |
|---|---|
| Naresh | IT |
| Rahul | IT |
| Arjun | HR |
| David | Finance |
| Kiran | HR |
Query Without DISTINCT
SELECT department FROM students;
Result
IT IT HR Finance HR
Query With DISTINCT
SELECT DISTINCT department FROM students;
Result
IT HR Finance
What Happens Internally?
Step 1
Rows selected normally.
Step 2
Duplicate department values identified.
Step 3
Only unique values returned.
DISTINCT Query Flow
Fetch Rows
|
v
Identify Duplicate Values
|
v
Remove Repeated Entries
|
v
Return Unique Rows
DISTINCT with Multiple Columns
DISTINCT can work on:
- Single column
- Multiple columns
Example
SELECT DISTINCT department,
city
FROM employees;
Important Point
DISTINCT checks uniqueness based on:
- Combination of selected columns
Example Data
| Department | City |
|---|---|
| IT | Hyderabad |
| IT | Hyderabad |
| IT | Bangalore |
Result
IT | Hyderabad IT | Bangalore
DISTINCT with COUNT()
DISTINCT is commonly used with:
- COUNT()
Example
SELECT COUNT(DISTINCT department) FROM employees;
Purpose
Count:
- Unique departments only
Difference Between DISTINCT and GROUP BY
| Feature | DISTINCT | GROUP BY |
|---|---|---|
| Purpose | Remove duplicates | Group rows |
| Aggregate Functions | Optional | Usually required |
| Complex Analytics | Limited | Advanced |
Example Using GROUP BY
SELECT department FROM employees GROUP BY department;
Result
Similar to DISTINCT.
But GROUP BY Supports Aggregates
SELECT department,
COUNT(*)
FROM employees
GROUP BY department;
DISTINCT vs UNIQUE Constraint
| Feature | DISTINCT | UNIQUE Constraint |
|---|---|---|
| Type | Query keyword | Database constraint |
| Purpose | Remove duplicates in result | Prevent duplicate storage |
Important Point
DISTINCT:
- Does not change table data
It only:
- Changes query output
DISTINCT with ORDER BY
DISTINCT can be combined with:
- ORDER BY
Example
SELECT DISTINCT department FROM employees ORDER BY department ASC;
What Happens?
Step 1
Duplicate departments removed.
Step 2
Unique values sorted alphabetically.
DISTINCT with NULL Values
DISTINCT treats:
- Multiple NULLs as one unique value
Example
NULL NULL IT HR
Result with DISTINCT
NULL IT HR
Performance Consideration
DISTINCT may require:
- Sorting
- Hashing
which can:
- Increase execution time on large datasets
How Databases Implement DISTINCT
Databases internally use:
- Sorting algorithms
- Hash aggregation
to identify duplicates.
DISTINCT Internal Processing
Read Data
|
v
Sort or Hash Values
|
v
Identify Unique Entries
|
v
Return Final Result
Real-Time Banking Example
Banking systems use DISTINCT for:
- Unique customer identification
- Branch reporting
- Fraud analysis
Example
Find distinct account types.
Real-Time E-Commerce Example
E-commerce platforms use DISTINCT for:
- Unique product categories
- Customer location analytics
- Vendor reporting
Example
Find distinct product brands.
Real-Time Learning Platform Example
Learning platforms use DISTINCT for:
- Unique course categories
- Student department reports
- Certification analytics
Example
Find distinct course technologies.
DISTINCT in Microservices
Microservices use DISTINCT for:
- Analytics APIs
- Search filtering
- Dropdown value generation
Advantages of DISTINCT
- Removes duplicate results
- Improves report clarity
- Useful for analytics
- Simple query syntax
Disadvantages of DISTINCT
- May impact performance on large tables
- Requires additional processing
Best Practices
- Use DISTINCT only when required
- Use indexes on filtered columns
- Avoid unnecessary DISTINCT usage
- Analyze execution plans for optimization
Common Interview Mistake
Many developers think:
- DISTINCT removes duplicate rows permanently
Reality
DISTINCT affects:
- Only query output
not:
- Stored table data
Related Learning Topics
- What is GROUP BY in SQL?
- What are Aggregate Functions in SQL?
- What is ORDER BY in SQL?
- What is a UNIQUE Key in SQL?
- MySQL Performance Optimization
Professional Interview Answer
The DISTINCT keyword in SQL is used to retrieve only unique values from query results by eliminating duplicate records. It can be applied to single or multiple columns and is commonly used in reporting, analytics, filtering, and search functionalities. DISTINCT works by comparing selected column values and returning only unique combinations. It is frequently used with aggregate functions such as COUNT(DISTINCT column_name) to calculate unique counts. Although DISTINCT improves result clarity, it may impact performance on large datasets because the database may need additional sorting or hashing operations to identify duplicates.
Why Interviewers Like This Answer
- Clearly explains duplicate removal
- Includes internal processing understanding
- Shows DISTINCT vs GROUP BY knowledge
- Provides enterprise-level examples
- Explains performance considerations
Frequently Asked Questions
What is DISTINCT in SQL?
DISTINCT removes duplicate values from query results.
Does DISTINCT modify table data?
No, DISTINCT affects only query output.
Can DISTINCT work on multiple columns?
Yes, DISTINCT checks unique combinations of selected columns.
Can DISTINCT be used with COUNT?
Yes, COUNT(DISTINCT column_name) counts unique values.
Does DISTINCT affect performance?
Yes, it may increase execution cost due to sorting or hashing operations.