LearnContact
Lesson 2615 min read

DISTINCT

Learn how to remove duplicate rows from a query result using DISTINCT, and how it applies across multiple selected columns at once.

Introduction

Sometimes a column contains repeated values, and you only care about which distinct values exist — not how many rows have each one. For example, you might want a list of every grade that appears in a students table, without seeing the same grade repeated once per student.

The DISTINCT keyword removes duplicate rows from a query result, keeping only unique ones.

What You Will Learn
  • The basic syntax of SELECT DISTINCT.
  • A worked example listing every unique grade in the students table.
  • How DISTINCT applies to the entire combination of selected columns, not just one.

Basic DISTINCT Syntax

DISTINCT is placed directly after SELECT, before the column list.

SELECT DISTINCT column1
FROM table_name;

Here is the students table we will use for the examples in this lesson.

idnameagegrade
1Alex16A
2Sam17B
3Priya16A
4Jordan18C
5Maya17B
6Ravi16B

Worked Example: Unique Grades

Without DISTINCT, selecting the grade column returns one row per student — grades repeat every time more than one student shares a grade.

SELECT grade
FROM students;
Result (without DISTINCT)
+-------+
| grade |
+-------+
| A     |
| B     |
| A     |
| C     |
| B     |
| B     |
+-------+

Adding DISTINCT collapses repeated values down to one row each.

SELECT DISTINCT grade
FROM students;
Result (with DISTINCT)
+-------+
| grade |
+-------+
| A     |
| B     |
| C     |
+-------+

Even though grade B appears in three separate student rows, it now appears only once in the result — exactly the "every unique grade" list we wanted.

DISTINCT with Multiple Columns

DISTINCT can be applied to more than one column at once — but it is important to understand what it actually does in that case. It does not make each column individually unique; instead, it treats the combination of all selected columns as a single unit, and removes rows only where every selected column matches another row exactly.

SELECT DISTINCT age, grade
FROM students;
Result
+-----+-------+
| age | grade |
+-----+-------+
| 16  | A     |
| 17  | B     |
| 18  | C     |
| 16  | B     |
+-----+-------+

Notice age 16 appears twice here — once paired with grade A, once paired with grade B. These are different combinations, so DISTINCT keeps both. It only removes a row when the entire (age, grade) pair is an exact repeat of another row, such as Alex and Priya both being (16, A).

A Frequent Misunderstanding

DISTINCT does not mean "make column X unique while ignoring column Y." It always considers the full row formed by every column in the SELECT list. If you truly only want unique values from one column, select only that one column, as in the grade example above.

Common Mistakes

Avoid These Mistakes
  • Assuming DISTINCT applies only to the first column when multiple columns are selected.
  • Using DISTINCT * expecting it to behave differently from listing the actual columns — it does not.
  • Forgetting that DISTINCT can be slower on very large tables, since MySQL must compare rows to find duplicates.
  • Using DISTINCT to "fix" a query that actually has a WHERE or JOIN condition producing accidental duplicate rows, instead of fixing the root cause.

Best Practices

  • Use DISTINCT only on the exact columns whose unique combination you actually care about.
  • Select as few columns as needed when your goal is a list of unique values.
  • Investigate why duplicates exist before reaching for DISTINCT as a quick fix.
  • Remember DISTINCT applies to the whole row of selected columns, not one column in isolation.

Frequently Asked Questions

Does DISTINCT work with ORDER BY and LIMIT?

Yes, they can all be combined — MySQL first determines the distinct rows, then applies ordering and limiting as usual.

Is DISTINCT the same as GROUP BY?

They can produce similar results for simple cases, but GROUP BY (covered in a later lesson) is built for calculating aggregates per group, while DISTINCT simply removes duplicate rows.

Can DISTINCT be used with just one column?

Yes — that is the most common use case, such as listing every unique grade or every unique city.

Does DISTINCT change the underlying table?

No. Like all SELECT queries, it only affects the returned result, not the stored data.

Key Takeaways

  • SELECT DISTINCT removes duplicate rows from a query result.
  • With one column, DISTINCT returns each unique value only once.
  • With multiple columns, DISTINCT treats the full combination of columns as the unit of uniqueness.
  • DISTINCT does not modify the underlying table, only the returned result.

Summary

DISTINCT is a simple but powerful tool for eliminating duplicate rows, whether you are listing unique values from a single column or unique combinations across several columns.

Next, you will learn how to give columns and tables temporary names using aliases, making queries easier to read and write.

Lesson 26 Completed
  • You can remove duplicate rows using SELECT DISTINCT.
  • You can list unique values from a single column.
  • You understand that DISTINCT applies to the full combination of selected columns.
Next Lesson →

Aliases