SQL DISTINCT Keyword — Eliminating Duplicates in Queries

Remove Duplicate Results

The office needs one occurrence of each class name even when several students belong to that class.

DISTINCT removes duplicate rows from the projected result. Equality is evaluated over the selected values, rather than over every stored field.

  1. Explain and apply

    Remove duplicate result combinations without confusing them with duplicate stored rows.

  2. Work it out

    Return each class once and explain why roll number should not be included in that DISTINCT list.

  3. Avoid mistakes

    Use a key to identify individual students and an appropriate selected tuple to define duplicates.

Remove Duplicate Results: the request and its result

  1. School need

    The office needs one occurrence of each class name even when several students belong to that class.

  2. Database operation

    DISTINCT removes duplicate rows from the projected result. Equality is evaluated over the selected values, rather than over every stored field.

  3. Check the outcome

    Including a unique roll number would make every selected pair different. The required result is about classes, so only the class value defines the duplicate.

A conceptual model. The query examples below show the actual syntax, input data and results.

Understand what the database does

SELECT DISTINCT class compares the class values returned by the query. SELECT DISTINCT class, city compares the pair, so one class can appear more than once when its cities differ. DISTINCT reads a deduplicated result; it does not remove duplicate source records.

Related concepts: Group and Summarize and INNER JOIN.

SQL DISTINCT

Imagine you have a students table, and you're asked: "How many different classes are there?" Simply selecting the class column will list all entries — including duplicates. This is where DISTINCT shines. It helps you remove duplicates and see only unique values.

Syntax of DISTINCT

SQL · portable concepts; preserved examples identify their engine

SELECT DISTINCT column1, column2, ...
FROM table_name;

The DISTINCT keyword in SQL is used to remove duplicate rows from the result set. When you apply SELECT DISTINCT to one or more columns, it ensures that only unique combinations of those column values appear in the final output.

For example, in the syntax:

SELECT DISTINCT column1, column2, ...
FROM table_name;

SQL returns only one instance of each unique combination of column1, column2, etc., from table_name. If there are multiple rows with the same values in all the specified columns, only one of those rows will appear in the result.

Sample Table – students - For Examples

SQL · portable concepts; preserved examples identify their engine

Let’s work with this school dataset:

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  city VARCHAR(30)
);

INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 'Delhi'),
(2, 'Diya Iyer', '9B', 'Chennai'),
(3, 'Rohit Menon', '10A', 'Kochi'),
(4, 'Sneha Patil', '8C', 'Pune'),
(5, 'Mehul Agarwal', '9B', 'Delhi'),
(6, 'Aisha Khan', '8C', 'Hyderabad');
students
roll_nonameclasscity
1Aarav Sharma10ADelhi
2Diya Iyer9BChennai
3Rohit Menon10AKochi
4Sneha Patil8CPune
5Mehul Agarwal9BDelhi
6Aisha Khan8CHyderabad

1. DISTINCT on One Column

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  city VARCHAR(30)
);

INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 'Delhi'),
(2, 'Diya Iyer', '9B', 'Chennai'),
(3, 'Rohit Menon', '10A', 'Kochi'),
(4, 'Sneha Patil', '8C', 'Pune'),
(5, 'Mehul Agarwal', '9B', 'Delhi'),
(6, 'Aisha Khan', '8C', 'Hyderabad');

SQL · portable concepts; preserved examples identify their engine

Let’s list all the unique classes:

Use case: A school administrator wants to identify all the unique classes available in the school to create a summary of class sections.

students
roll_nonameclasscity
1Aarav Sharma10ADelhi
2Diya Iyer9BChennai
3Rohit Menon10AKochi
4Sneha Patil8CPune
5Mehul Agarwal9BDelhi
6Aisha Khan8CHyderabad

SQL query description: SQL query to select the distinct values of the class column from the students table, eliminating any duplicate entries.

SELECT DISTINCT class
FROM students;

Result description: Result contains a list of unique class names that exist in the students table.

Query Result
class
10A
9B
8C
FocusSelected or comparedAdded or changedRemoved or failing

The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.

2. Without DISTINCT

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  city VARCHAR(30)
);

INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 'Delhi'),
(2, 'Diya Iyer', '9B', 'Chennai'),
(3, 'Rohit Menon', '10A', 'Kochi'),
(4, 'Sneha Patil', '8C', 'Pune'),
(5, 'Mehul Agarwal', '9B', 'Delhi'),
(6, 'Aisha Khan', '8C', 'Hyderabad');

SQL · portable concepts; preserved examples identify their engine

If we don’t use DISTINCT:

SELECT class
FROM students;
Query Result
class
10A
9B
10A
8C
9B
8C

You can see how duplicate values clutter the output — DISTINCT solves that instantly.

3. DISTINCT on Multiple Columns

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  city VARCHAR(30)
);

INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 'Delhi'),
(2, 'Diya Iyer', '9B', 'Chennai'),
(3, 'Rohit Menon', '10A', 'Kochi'),
(4, 'Sneha Patil', '8C', 'Pune'),
(5, 'Mehul Agarwal', '9B', 'Delhi'),
(6, 'Aisha Khan', '8C', 'Hyderabad');

SQL · portable concepts; preserved examples identify their engine

You can also find unique combinations — like each unique (class, city) pair:

Use case: A school administrator wants to identify all unique combinations of classes and cities represented in the student database for planning region-wise class distribution.

SQL query description: SQL query to select distinct pairs of class and city from the students table, eliminating duplicate combinations.

SELECT DISTINCT class, city
FROM students;

Result description: Result contains a list of unique class and city combinations present in the student records.

Query Result
classcity
10ADelhi
9BChennai
10AKochi
8CPune
9BDelhi
8CHyderabad

4. Using COUNT with DISTINCT

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  city VARCHAR(30)
);

INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 'Delhi'),
(2, 'Diya Iyer', '9B', 'Chennai'),
(3, 'Rohit Menon', '10A', 'Kochi'),
(4, 'Sneha Patil', '8C', 'Pune'),
(5, 'Mehul Agarwal', '9B', 'Delhi'),
(6, 'Aisha Khan', '8C', 'Hyderabad');

SQL · portable concepts; preserved examples identify their engine

How many unique cities are students from?

Use case: A school administrator wants to find out how many unique cities students come from to analyze geographical diversity.

students
roll_nonameclasscity
1Aarav Sharma10ADelhi
2Diya Iyer9BChennai
3Rohit Menon10AKochi
4Sneha Patil8CPune
5Mehul Agarwal9BDelhi
6Aisha Khan8CHyderabad

SQL query description: SQL query to count the number of distinct values in the city column from the students table, and label the result as unique_city_count.

SELECT COUNT(DISTINCT city) AS unique_city_count
FROM students;

Result description: Result contains a single number representing the total count of unique cities that students in the database belong to.

Query Result
unique_city_count
5
FocusSelected or comparedAdded or changedRemoved or failing

The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.

Best Practices

Query discussed in this section:

SELECT COUNT(DISTINCT city) AS unique_city_count
FROM students;
  • Use DISTINCT only when needed — it may slow down performance on large datasets.
  • Understand column combinations — DISTINCT class and city is not the same as DISTINCT city alone.
  • Prefer GROUP BY if you're also using aggregate functions (we’ll explore this in advanced topics).

Important differences and mistakes

Use a key to identify individual students and an appropriate selected tuple to define duplicates. DISTINCT can conceal an unintended join multiplication without fixing it. Add ORDER BY if the deduplicated list needs a stated display order.

Try it with the school data

Return each class once and explain why roll number should not be included in that DISTINCT list.

This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

SQL · create only these practice tables in a disposable database:

CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');

Worked answer and reasoning

This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

SQL · create only these practice tables in a disposable database:

CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');

SQL · this practice variation runs with SQLite 3; the preserved examples retain their stated dialect

SELECT DISTINCT class FROM practice_students ORDER BY class;
Expected query result
class
10A
9B

Including a unique roll number would make every selected pair different. The required result is about classes, so only the class value defines the duplicate.

Check your understanding

Does DISTINCT class delete repeated class values from students?
Choose one answer. You can try again.

Answer and explanation

No. It removes repeated class values only from this query result.

Including a unique roll number would make every selected pair different. The required result is about classes, so only the class value defines the duplicate.

Use the idea independently

DISTINCT removes duplicate rows from the projected result. Equality is evaluated over the selected values, rather than over every stored field.

Including a unique roll number would make every selected pair different. The required result is about classes, so only the class value defines the duplicate.

Continue your understanding with Group and Summarize and INNER JOIN.

Source for the engine-specific behavior: MySQL 8.4 reference documentation.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: SQL Operators — AND, OR, NOT, IN, BETWEEN, LIKE →