Lesson 16 of 60 – DISTINCT
27%

DISTINCT in SQL

The DISTINCT keyword is used with the SELECT statement to remove duplicate values from the result. It returns only unique values.

Note: DISTINCT removes duplicate rows from the selected result. It does not delete any data from the original table.

1. What is DISTINCT?

Suppose a Students table contains the following courses:

ID Name Course
1 Rahul Python
2 Priya Java
3 Amit Python
4 Neha PHP
5 Ravi Python

If we execute:

SELECT course
FROM Students;

The result may contain:

Python
Java
Python
PHP
Python

To display each course only once, use DISTINCT.

2. Basic DISTINCT Syntax

The basic syntax is:

SELECT DISTINCT column_name
FROM table_name;

3. DISTINCT with One Column

SELECT DISTINCT course
FROM Students;

This returns each unique course.

Example result:

Python
Java
PHP

4. DISTINCT with City

Suppose many students are from the same city.

SELECT DISTINCT city
FROM Students;

This displays each city only once.

5. DISTINCT with Department

SELECT DISTINCT department
FROM Employees;

This query returns a list of unique departments.

For example:

IT
HR
Sales
Accounts

6. DISTINCT with Multiple Columns

DISTINCT can be applied to multiple columns.

SELECT DISTINCT course, city
FROM Students;

Here, SQL considers the combination of course and city.

7. Understanding Multiple Columns

Consider this data:

Course City
Python Patna
Python Patna
Python Delhi
Java Patna

Query:

SELECT DISTINCT course, city
FROM Students;

The duplicate Python + Patna combination appears only once.

8. DISTINCT Does Not Delete Data

DISTINCT only changes the query result. It does not remove duplicate records from the table.

SELECT DISTINCT course
FROM Students;

The original duplicate course values remain stored in the table.

9. DISTINCT vs DELETE

Command Purpose
DISTINCT Removes duplicate values from the query result.
DELETE Removes records from a table.

DISTINCT is a query operation, while DELETE changes the stored data.

10. DISTINCT with WHERE

DISTINCT can be combined with the WHERE clause.

SELECT DISTINCT course
FROM Students
WHERE age > 18;

This returns unique courses only from students whose age is greater than 18.

11. DISTINCT with ORDER BY

You can sort unique values using ORDER BY.

SELECT DISTINCT course
FROM Students
ORDER BY course;

The unique courses will be displayed in ascending order.

12. DISTINCT with ORDER BY DESC

SELECT DISTINCT course
FROM Students
ORDER BY course DESC;

This displays unique courses in descending order.

13. DISTINCT with LIMIT

In MySQL, DISTINCT can be combined with LIMIT.

SELECT DISTINCT course
FROM Students
LIMIT 5;

This returns up to five unique course values.

14. DISTINCT with COUNT()

One of the most useful combinations is COUNT(DISTINCT column).

SELECT COUNT(DISTINCT course)
FROM Students;

This returns the number of different courses in the table.

15. Count Unique Cities

SELECT COUNT(DISTINCT city)
FROM Students;

This returns the number of unique cities represented in the table.

16. Count Unique Departments

SELECT COUNT(DISTINCT department)
FROM Employees;

This can be used to determine how many different departments exist.

17. DISTINCT with WHERE and COUNT

SELECT COUNT(DISTINCT course)
FROM Students
WHERE age > 18;

This counts the number of unique courses among students older than 18.

18. DISTINCT with JOIN

DISTINCT can also be used when retrieving data from multiple tables.

SELECT DISTINCT Students.name, Courses.course_name
FROM Students
INNER JOIN Courses
ON Students.course_id = Courses.course_id;

This can help avoid duplicate result rows when the same combination appears more than once.

19. DISTINCT with NULL Values

If a column contains multiple NULL values, DISTINCT treats the NULL values as one distinct result value.

SELECT DISTINCT city
FROM Students;

If several records have NULL as their city, the result contains a single NULL entry for that distinct result.

20. DISTINCT with Names

SELECT DISTINCT name
FROM Students;

This returns each different student name only once.

Keep in mind that if two different people happen to have the same name, DISTINCT treats those identical name values as duplicates in this result.

21. DISTINCT with Fees

SELECT DISTINCT fee
FROM Students;

This displays each different fee amount only once.

For example:

4000.00
10000.00
15000.00
35000.00

22. DISTINCT with Course and Fee

SELECT DISTINCT course, fee
FROM Students;

DISTINCT considers the complete combination of course and fee.

23. DISTINCT and Aggregate Functions

DISTINCT can be used inside aggregate functions.

SELECT COUNT(DISTINCT course)
FROM Students;

The query counts unique courses instead of counting every student row.

24. DISTINCT with GROUP BY

DISTINCT and GROUP BY can sometimes produce similar-looking results, but they are used for different purposes.

SELECT DISTINCT course
FROM Students;

This simply returns unique courses.

SELECT course
FROM Students
GROUP BY course;

GROUP BY is mainly used when grouping rows for aggregate calculations.

25. DISTINCT with a Practical Example

Suppose a training institute has students enrolled in several courses:

Python
Python
ADCA
Tally
Python
ADCA
Java
Tally

To find the available unique courses:

SELECT DISTINCT course
FROM Students;

Possible result:

Python
ADCA
Tally
Java

26. Finding Unique Cities

SELECT DISTINCT city
FROM Students
ORDER BY city;

This is useful for creating reports or understanding the different locations represented in a database.

27. Finding Unique Courses for a Report

SELECT DISTINCT course
FROM Students
WHERE fee > 5000
ORDER BY course;

This query returns unique courses where the fee is greater than 5000, sorted alphabetically.

28. Common Mistake

DISTINCT should be placed immediately after SELECT.

Correct:

SELECT DISTINCT course
FROM Students;

Incorrect:

SELECT course DISTINCT
FROM Students;

29. Important Difference with SELECT *

Consider:

SELECT *
FROM Students;

This returns all columns and all matching rows.

Whereas:

SELECT DISTINCT course
FROM Students;

This returns only unique course values.

30. Complete DISTINCT Example

SELECT DISTINCT course
FROM Students
WHERE age >= 18
ORDER BY course ASC
LIMIT 10;

This query:

  • Selects only the course column.
  • Removes duplicate course values.
  • Includes students aged 18 or above.
  • Sorts courses in ascending order.
  • Returns up to 10 unique courses.

📌 Key Points

  • DISTINCT is used with SELECT to return unique values.
  • DISTINCT does not delete data from the original table.
  • Use SELECT DISTINCT column_name for unique values.
  • DISTINCT can be used with multiple columns.
  • With multiple columns, DISTINCT considers the complete combination.
  • COUNT(DISTINCT column) counts unique values.
  • DISTINCT can be combined with WHERE, ORDER BY, and LIMIT.
  • DISTINCT can also be used with JOIN queries.
  • DISTINCT is different from DELETE because it does not modify stored data.
  • DISTINCT is useful for reports, lists, filters, and unique-value searches.

🧠 Quick Quiz

Question: Which SQL keyword is used to return only unique values?