đ¯ The SELECT DISTINCT statement is used to retrieve unique (non-duplicate) values from one or more columns in a database table. When a column contains repeated values, DISTINCT removes the duplicates and returns only one occurrence of each unique value.
đ What is SELECT DISTINCT?
In many databases, multiple rows may contain the same value in a particular column. The DISTINCT keyword helps eliminate duplicate values from the query result, making it easier to view unique entries.
Information
đ Basic Syntax
General Syntax of SELECT DISTINCT
SELECT DISTINCT column_name
FROM table_name;You can also retrieve unique combinations of multiple columns.
Multiple Columns
SELECT DISTINCT column1, column2
FROM table_name;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | City |
|---|---|---|---|
| 101 | Alice | Computer Science | Chennai |
| 102 | Bob | Mathematics | Coimbatore |
| 103 | Charlie | Computer Science | Madurai |
| 104 | David | Physics | Chennai |
| 105 | Eva | Mathematics | Coimbatore |
đ¯ Retrieve Unique Values
To display each department only once, use the DISTINCT keyword.
Unique Departments
SELECT DISTINCT Department
FROM Students;Result:
| Department |
|---|
| Computer Science |
| Mathematics |
| Physics |
đ§Š DISTINCT with Multiple Columns
When multiple columns are specified, SQL returns only unique combinations of those column values.
Unique Department and City Combinations
SELECT DISTINCT Department, City
FROM Students;If two rows have the same department but different cities, both rows will be included because the combination is different.
đ DISTINCT with WHERE
You can combine DISTINCT with the WHERE clause to retrieve unique values that satisfy a specific condition.
DISTINCT with WHERE
SELECT DISTINCT City
FROM Students
WHERE Department = 'Mathematics';đ DISTINCT with ORDER BY
The result returned by DISTINCT can be sorted using the ORDER BY clause.
DISTINCT with ORDER BY
SELECT DISTINCT Department
FROM Students
ORDER BY Department ASC;đ DISTINCT with Aggregate Functions
Aggregate functions such as COUNT() can use DISTINCT to calculate results based only on unique values.
Count Unique Departments
SELECT COUNT(DISTINCT Department) AS TotalDepartments
FROM Students;This query counts the number of different departments instead of the total number of rows.
đ Difference Between SELECT and SELECT DISTINCT
| Feature | SELECT | SELECT DISTINCT |
|---|---|---|
| Duplicate Values | Returns all matching rows. | Removes duplicate values. |
| Result Size | May contain duplicates. | Contains only unique values. |
| Performance | Usually faster. | May require additional processing to remove duplicates. |
| Common Usage | General data retrieval. | Finding unique values or combinations. |
đŧ Real-World Example
Suppose an online shopping website wants to display a list of all available product categories without repeating any category names.
Retrieve Unique Product Categories
SELECT DISTINCT Category
FROM Products
ORDER BY Category;This query returns each product category only once, making it ideal for navigation menus and filter options.
â ī¸ Important Notes
- DISTINCT compares the values of the selected columns only.
- When multiple columns are selected, the entire row combination must be unique.
- Using DISTINCT on large datasets may increase query execution time because the database must identify and remove duplicate results.
â Best Practices
Best Practice
đ Key Points to Remember
- đ SELECT DISTINCT returns only unique values.
- đ It removes duplicate values from the query result, not from the table.
- đ It can be used with one or multiple columns.
- đ It works with WHERE, ORDER BY, and aggregate functions like COUNT().
- đ Multiple columns return unique combinations of values.