SQL SELECT DISTINCT
SQL

SQL SELECT DISTINCT — How to Remove Duplicate Rows from Results

CHAPTER 4 of 30 SQL Tutorial — Free Course on Neotech Navigators
When you query a database, the results often contain duplicate values. The SQL SELECT DISTINCT statement solves this by returning only unique rows. In this chapter, you will learn how DISTINCT works on single columns, multiple columns, and how it differs from a regular SELECT.

The Problem: Duplicate Values

Let’s start by looking at a common situation. Using our Employees table, suppose you want to see which departments exist in the company. If you have not yet learned the basic SELECT command, check Chapter 3: SQL SELECT first.

TABLE: Employees
+----+-----------+------------+--------+-----------+
| ID | Name      | Department | Salary | City      |
+----+-----------+------------+--------+-----------+
| 1  | Priya K.  | Sales      | 55000  | Mumbai    |
| 2  | John D.   | Marketing  | 62000  | London    |
| 3  | Arun M.   | IT         | 78000  | Delhi     |
| 4  | Emma W.   | Sales      | 51000  | Sydney    |
| 5  | Ravi S.   | IT         | 72000  | Mumbai    |
| 6  | Sara L.   | HR         | 58000  | New York  |
+----+-----------+------------+--------+-----------+

If you run a simple SELECT on the Department column:

SQL — Without DISTINCT
SELECT Department
FROM Employees;
✓ OUTPUT — With Duplicates
+------------+
| Department |
+------------+
| Sales      |
| Marketing  |
| IT         |
| Sales      |  ← duplicate
| IT         |  ← duplicate
| HR         |
+------------+
6 rows returned

The result shows Sales twice and IT twice. That is because multiple employees belong to the same department. This is where DISTINCT comes in.

SQL SELECT DISTINCT Syntax

The SELECT DISTINCT statement returns only unique values, automatically removing all duplicate rows from the result:

SQL — DISTINCT Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;

DISTINCT on a Single Column

Now let’s run the same query with DISTINCT:

SQL
SELECT DISTINCT Department
FROM Employees;
✓ OUTPUT — Unique Values Only
+------------+
| Department |
+------------+
| Sales      |
| Marketing  |
| IT         |
| HR         |
+------------+
4 rows returned

The duplicates are gone. Instead of 6 rows, we get only 4 unique departments.

Let’s try another column — unique cities where employees are located:

SQL
SELECT DISTINCT City
FROM Employees;
✓ OUTPUT
+-----------+
| City      |
+-----------+
| Mumbai    |
| London    |
| Delhi     |
| Sydney    |
| New York  |
+-----------+
5 rows returned

Mumbai appeared twice in the table (Priya K. and Ravi S.), but DISTINCT returns it only once.

DISTINCT on Multiple Columns

When you use DISTINCT with multiple columns, SQL removes rows where the combination of all listed columns is duplicated. Each unique pair (or group) of values is treated as one result:

SQL
SELECT DISTINCT Department, City
FROM Employees;
✓ OUTPUT
+------------+-----------+
| Department | City      |
+------------+-----------+
| Sales      | Mumbai    |
| Marketing  | London    |
| IT         | Delhi     |
| Sales      | Sydney    |
| IT         | Mumbai    |
| HR         | New York  |
+------------+-----------+
6 rows returned

Notice that Sales appears twice and IT appears twice — but the combination of Department + City is different each time. “Sales, Mumbai” is not the same as “Sales, Sydney”, so both are kept.

💡 Key Concept

DISTINCT applies to the entire row of selected columns — not just the first column. Two rows are considered duplicates only when every selected column has the same value in both rows.

SELECT DISTINCT vs SELECT — Comparison

Feature SELECT SELECT DISTINCT
Duplicate rows Included in results Removed from results
Number of rows returned All matching rows Only unique rows
Performance Faster (no dedup needed) Slightly slower on large datasets
Use case View all data Find unique values

COUNT with DISTINCT

You can combine DISTINCT with the COUNT() function to count how many unique values exist in a column. This is incredibly useful for quick analysis. You will learn more about COUNT and other aggregate functions in Chapter 14: SQL COUNT, AVG, SUM.

SQL
SELECT COUNT(DISTINCT Department) AS TotalDepartments
FROM Employees;
✓ OUTPUT
+------------------+
| TotalDepartments |
+------------------+
| 4                |
+------------------+

Without DISTINCT, COUNT(Department) would return 6 (counting duplicates). With DISTINCT, it correctly returns 4 unique departments.

SQL — Count Unique Cities
SELECT COUNT(DISTINCT City) AS TotalCities
FROM Employees;
✓ OUTPUT
+-------------+
| TotalCities |
+-------------+
| 5           |
+-------------+

📌 Important Note

DISTINCT treats NULL as a unique value. If a column has multiple NULL entries, DISTINCT will keep only one NULL in the result. You will learn more about NULL values in Chapter 9: SQL NULL Values.

For a complete reference of the DISTINCT syntax, see the official MySQL SELECT documentation. If you want to filter rows based on conditions instead of just removing duplicates, learn about the SQL WHERE clause in the next chapter.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a query that counts how many unique cities the employees work in, and also a separate query that lists those unique cities sorted alphabetically.

ANSWER — Query 1: Count
SELECT COUNT(DISTINCT City) AS UniqueCities
FROM Employees;
ANSWER — Query 2: List
SELECT DISTINCT City
FROM Employees
ORDER BY City ASC;

Expected Output (Query 2):

+-----------+
| City      |
+-----------+
| Delhi     |
| London    |
| Mumbai    |
| New York  |
| Sydney    |
+-----------+

📝 What You Learned in This Chapter

  • SELECT DISTINCT removes duplicate rows from query results
  • On a single column, it returns only unique values from that column
  • On multiple columns, it removes rows where the entire combination of values is duplicated
  • Combine COUNT(DISTINCT column) to count how many unique values exist
  • DISTINCT treats NULL as a unique value
  • It slightly reduces performance on large datasets compared to regular SELECT

Frequently Asked Questions

What does SELECT DISTINCT do in SQL?
SELECT DISTINCT returns only unique rows from the result set. It automatically removes any duplicate rows where all selected column values are identical. It is commonly used to find unique categories, cities, statuses, or any values that may repeat across rows.
Does DISTINCT work on multiple columns?
Yes. When you use DISTINCT with multiple columns, SQL considers a row to be a duplicate only when all listed column values match. For example, SELECT DISTINCT Department, City FROM Employees keeps rows where the Department-City combination is unique — even if the same department or city appears individually in other rows.
Is DISTINCT the same as GROUP BY?
They produce similar results in simple cases, but they serve different purposes. DISTINCT simply removes duplicates. GROUP BY groups rows so you can apply aggregate functions like COUNT, SUM, or AVG. If you just need unique values, use DISTINCT. If you need calculations per group, use GROUP BY. You will learn GROUP BY in Chapter 25.
Does DISTINCT affect query performance?
DISTINCT adds a sorting or hashing step to remove duplicates, which makes queries slightly slower than a plain SELECT — especially on large tables with millions of rows. For small to medium datasets, the difference is negligible. To optimize, make sure the columns used with DISTINCT are indexed.
How does DISTINCT handle NULL values?
DISTINCT treats NULL as a single unique value. If a column contains three rows with NULL, the DISTINCT result will show only one NULL. This behavior is consistent across all major database systems including MySQL, PostgreSQL, SQL Server, and Oracle.

📖 Chapter 4 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com