Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 46

SELECT DISTINCT in MySQL

What DISTINCT Changes

DISTINCT removes duplicate rows from the displayed result after selected expressions are evaluated. It does not modify the table and it does not promise an order.

Common learning data: students contains Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) and Sana (X-B, 88.50, Active).
SELECT DISTINCT class_name
FROM students
ORDER BY class_name;
X-A X-B

One Column: Unique Values

Without DISTINCT the class appears once per student. With DISTINCT, identical class_name results collapse. The comparison follows the column collation, so case or accent variants may compare equal under a case-insensitive collation.

SELECT DISTINCT status
FROM students
ORDER BY status;
Active Inactive

DISTINCT on Multiple Columns

SELECT DISTINCT class_name, status
FROM students
ORDER BY class_name, status;
X-A | Active X-B | Active X-B | Inactive

DISTINCT applies to the pair, not separately to each column. Adding student_id, a unique key, makes every selected row unique and therefore makes DISTINCT unnecessary.

NULL and COUNT(DISTINCT)

SELECT DISTINCT marks FROM students;

SELECT COUNT(DISTINCT class_name) AS class_count
FROM students;

SELECT DISTINCT can show one NULL among repeated NULL results. COUNT(DISTINCT expression) counts distinct non-NULL values. For multiple expressions, read the current MySQL manual because NULL and tuple behavior must match your exact version and intention.

Displayed Duplicates vs Stored Duplicates

GoalCorrect tool
Show each class onceSELECT DISTINCT
Count students per classGROUP BY + COUNT
Prevent duplicate email/roll numberUNIQUE constraint
Find duplicate stored valuesGROUP BY value HAVING COUNT(*) > 1
Do not add DISTINCT merely to hide a faulty JOIN. Diagnose why the join multiplies rows and fix its relationship or condition.

Mistakes and Practice

  • Expecting DISTINCT to sort.
  • Selecting a unique ID and wondering why nothing collapses.
  • Using DISTINCT to hide bad joins.
  • Confusing result uniqueness with database constraints.
  1. List each status once.
  2. List unique class/status combinations and predict three rows.
  3. Count distinct classes.
  4. Design a UNIQUE constraint for a school roll number.

Quick Summary

  • DISTINCT removes duplicate selected rows only.
  • Multiple columns form one comparison combination.
  • ORDER BY is still required for predictable display order.
  • Constraints, not DISTINCT, prevent invalid stored duplicates.

Official References

References reviewed 14 August 2026.

Frequently Asked Questions

What does SELECT DISTINCT do?
It removes duplicate rows from the query result based on the complete selected expression list. It does not delete data from the table.
How does DISTINCT work with multiple columns?
A row is duplicate only when the complete combination of selected values matches another result row.
Does DISTINCT include NULL?
SELECT DISTINCT can return one NULL representative among duplicate NULL values. COUNT(DISTINCT column) does not count NULL.
Is DISTINCT the same as GROUP BY?
They may produce similar unique lists, but DISTINCT expresses duplicate elimination while GROUP BY forms groups for aggregation. Use the form that states the real intent.
Can DISTINCT fix duplicate data?
No. It only hides duplicate result rows. Prevent invalid stored duplicates with a PRIMARY KEY or UNIQUE constraint and clean existing data deliberately.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.