The SQL DISTINCT keyword is used to return only unique values from a table. When a column contains duplicate data, DISTINCT helps remove repeated values and displays only one occurrence of each value in the result set. It is one of the most commonly used SQL keywords when analyzing data and generating reports.
In real-world databases, duplicate values often exist because multiple records may share the same department, city, category, course, or product type. If all records are displayed, the result may become difficult to analyze. The DISTINCT keyword simplifies the output by showing only unique entries.
Understanding DISTINCT is important for database developers, analysts, students, and professionals because it improves data readability and helps generate meaningful reports.
The DISTINCT keyword removes duplicate rows from the result of a SELECT statement. It ensures that only unique values are displayed in the query output.
Without DISTINCT, SQL returns all matching rows, including repeated values. By using DISTINCT, duplicate values are automatically filtered out.
SELECT DISTINCT column_name FROM table_name;
The query returns only unique values from the specified column.
| StudentID | StudentName | Course |
|---|---|---|
| 101 | Rahul | BCA |
| 102 | Priya | BCA |
| 103 | Amit | MCA |
| 104 | Neha | B.Tech |
| 105 | Rohan | MCA |
The most common use of DISTINCT is with a single column.
SELECT DISTINCT Course FROM Student;
| Course |
|---|
| BCA |
| MCA |
| B.Tech |
Although multiple students belong to BCA and MCA, DISTINCT displays each course only once.
Let's compare the result without using DISTINCT.
SELECT Course FROM Student;
| Course |
|---|
| BCA |
| BCA |
| MCA |
| B.Tech |
| MCA |
Duplicate values are visible because DISTINCT was not used.
DISTINCT can also work with multiple columns. In this case, SQL checks the uniqueness of the combined column values.
SELECT DISTINCT Course, StudentName FROM Student;
The combination of Course and StudentName must be unique for a row to appear only once.
When multiple columns are specified, SQL does not check uniqueness individually. Instead, it checks the complete combination of values.
| Course | StudentName |
|---|---|
| BCA | Rahul |
| BCA | Priya |
These rows are considered unique because the combinations are different.
DISTINCT works with numbers just as effectively as it works with text values.
| Marks |
|---|
| 80 |
| 90 |
| 80 |
| 95 |
SELECT DISTINCT Marks FROM StudentMarks;
| Marks |
|---|
| 80 |
| 90 |
| 95 |
Text-based columns often contain repeated values. DISTINCT helps identify all unique text entries.
SELECT DISTINCT City FROM Customers;
The query displays all cities where customers are located without repeating city names.
A common interview question involves DISTINCT and NULL values.
When a column contains multiple NULL values, DISTINCT treats them as a single unique value.
| City |
|---|
| Delhi |
| NULL |
| NULL |
| Mumbai |
SELECT DISTINCT City FROM Customers;
Only one NULL value appears in the result set.
Suppose an organization wants to know all departments present in the company.
SELECT DISTINCT Department FROM Employee;
The result displays each department only once, regardless of how many employees belong to it.
An online store may need a list of unique product categories.
SELECT DISTINCT Category FROM Products;
This helps create category filters for customers.
A college administrator may want to know all available courses.
SELECT DISTINCT Course FROM Student;
This query quickly generates a list of unique courses.
| DISTINCT | GROUP BY |
|---|---|
| Removes duplicates. | Groups records. |
| Simple unique value retrieval. | Often used with aggregate functions. |
| Easier for basic duplicate removal. | More powerful for data analysis. |
The DISTINCT keyword can be combined with the WHERE clause to return unique values that satisfy a specific condition. This combination is frequently used in real-world applications where only selected records need to be analyzed.
SELECT DISTINCT Department FROM Employee WHERE Salary > 50000;
This query returns only the unique departments where employees have salaries greater than 50,000.
After removing duplicate values, SQL can sort the remaining results using the ORDER BY clause.
SELECT DISTINCT City FROM Customers ORDER BY City ASC;
The result displays unique city names in alphabetical order.
SELECT DISTINCT Category FROM Products ORDER BY Category DESC;
The categories are displayed in reverse alphabetical order.
Many beginners ask whether DISTINCT and GROUP BY perform similar tasks. While both can return unique values, GROUP BY is mainly designed for grouping records and performing calculations.
SELECT DISTINCT Department FROM Employee;
SELECT Department FROM Employee GROUP BY Department;
Both queries may produce similar results, but GROUP BY becomes more useful when aggregate functions are involved.
One of the most popular uses of DISTINCT is with the COUNT() function. It helps count only unique values instead of all values.
SELECT COUNT(DISTINCT Department) AS TotalDepartments FROM Employee;
This query counts only unique departments.
SELECT COUNT(Department) FROM Employee;
This counts every department value, including duplicates.
| Department |
|---|
| IT |
| IT |
| HR |
| Finance |
COUNT(Department) returns 4 while COUNT(DISTINCT Department) returns 3.
DISTINCT can remove duplicate combinations when more than one column is selected.
SELECT DISTINCT Department, Designation FROM Employee;
SQL checks uniqueness based on the complete Department-Designation combination.
DISTINCT can be used inside aggregate functions to eliminate duplicate values before calculations are performed.
SELECT SUM(DISTINCT Salary) FROM Employee;
Duplicate salary values are counted only once before calculating the total.
SELECT AVG(DISTINCT Salary) FROM Employee;
Only unique salary values contribute to the average calculation.
When multiple tables are joined, duplicate rows often appear in query results. DISTINCT helps remove unnecessary duplicates.
SELECT DISTINCT Department.DepartmentName FROM Department INNER JOIN Employee ON Department.DepartmentID = Employee.DepartmentID;
The query returns each department name only once even if multiple employees belong to the same department.
SELECT DISTINCT DepartmentName FROM Department LEFT JOIN Employee ON Department.DepartmentID = Employee.DepartmentID;
This query returns a unique list of departments regardless of whether employees exist in those departments.
DISTINCT is especially useful in relational databases where data is distributed across several tables.
SELECT DISTINCT Customer.CustomerName FROM Customer INNER JOIN Orders ON Customer.CustomerID = Orders.CustomerID;
The query shows customers who have placed orders without displaying duplicate customer names.
Suppose an e-commerce platform wants a list of unique product categories purchased by customers.
SELECT DISTINCT Category FROM Orders;
This helps generate category-based analytics reports.
SELECT DISTINCT Course FROM Student;
The administration can quickly identify all courses currently offered.
SELECT DISTINCT BranchName FROM Accounts;
The query provides a unique list of bank branches.
SELECT DISTINCT Department FROM Patients;
Hospital administrators can identify all departments handling patients.
Although DISTINCT is simple to use, it may require additional processing on large datasets because SQL must identify and eliminate duplicate values.
The performance impact becomes more noticeable when:
DISTINCT returns only unique values from a query result.
It removes duplicate values from output.
No, it only affects query results.
Yes.
Yes.
Yes.
Yes.
It counts only unique values.
No, but multiple NULL values appear only once.
Yes.
Yes.
SELECT DISTINCT column_name FROM table_name;
Yes.
Yes.
Not always.
The output remains unchanged.
Yes, especially on large tables.
Yes.
Removing duplicate results.
Yes.
It sums only unique values.
It averages only unique values.
Yes.
Yes.
It is essential for creating clean and meaningful reports.
The SQL DISTINCT keyword is an important tool for eliminating duplicate values and generating cleaner query results. It helps developers, analysts, and database administrators work with unique information efficiently. DISTINCT supports multiple columns, aggregate functions, JOIN operations, filtering conditions, and sorting operations.
By understanding how DISTINCT works and when to use it, database professionals can improve report quality, enhance data analysis, and create more effective SQL queries. Mastering DISTINCT is an essential step toward becoming proficient in SQL database management and reporting.