SELECT DISTINCT in SQL

đŸŽ¯ 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

DISTINCT affects only the result set returned by the query. It does not remove duplicate records from the table itself.

📝 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:

StudentIDNameDepartmentCity
101AliceComputer ScienceChennai
102BobMathematicsCoimbatore
103CharlieComputer ScienceMadurai
104DavidPhysicsChennai
105EvaMathematicsCoimbatore

đŸŽ¯ 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

FeatureSELECTSELECT DISTINCT
Duplicate ValuesReturns all matching rows.Removes duplicate values.
Result SizeMay contain duplicates.Contains only unique values.
PerformanceUsually faster.May require additional processing to remove duplicates.
Common UsageGeneral 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

Use DISTINCT only when duplicate values need to be removed. Avoid using it unnecessarily, as it may reduce query performance on very large tables. When possible, retrieve only the required columns to minimize processing overhead.

🚀 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.
>>"Use SELECT DISTINCT when you need unique information, not repeated data."

Summary

✅ SELECT DISTINCT is a powerful SQL feature for eliminating duplicate values from query results. It is commonly used to generate unique lists, count distinct values, and simplify reports by displaying only meaningful, non-repeating data.