SQL DISTINCT Keyword - Eliminating Duplicates in Queries

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

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

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

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

2. Without DISTINCT

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

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

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

Best Practices

  • 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).

Summary

The DISTINCT keyword helps you cut through the noise — showing only what's different. Whether you're counting unique classes, listing hometowns, or generating reports with uniqueness in mind, DISTINCT keeps your data clean and purposeful.

QUIZ

Question 1:What is the main purpose of the DISTINCT keyword in SQL?

Question 2:The DISTINCT keyword affects only the first column in the SELECT statement.

Question 3:Which of the following SQL queries correctly use the DISTINCT keyword?

Question 4:What does this SQL query return?
SELECT DISTINCT class FROM students;

Question 5:DISTINCT can be used with aggregate functions like COUNT().

Question 6:Which of the following outputs will be affected by using DISTINCT?


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...