SQL DISTINCT




🎯 Introduction

The SQL DISTINCT keyword is used to return unique values by removing duplicates from query results.

This is very useful when you want to analyze unique categories, departments, or values from a dataset.

1️⃣ Basic DISTINCT Example

SELECT DISTINCT Department
FROM Employees;

👉 Returns a list of unique department names from the Employees table.

2️⃣ DISTINCT with Multiple Columns

SELECT DISTINCT Department, JobTitle
FROM Employees;

👉 Returns unique combinations of Department + Job Title.

3️⃣ COUNT with DISTINCT

SELECT COUNT(DISTINCT Department) AS UniqueDepartments
FROM Employees;

👉 Returns the number of unique departments.

4️⃣ DISTINCT vs GROUP BY

  • DISTINCT → Quickly removes duplicates.
  • GROUP BY → Groups rows for aggregation (works with SUM, COUNT, etc.).
Pro Tip: Use DISTINCT for quick unique values, and GROUP BY when you need aggregation.

✅ Key Takeaways

  • DISTINCT removes duplicate rows.
  • Can be applied to single or multiple columns.
  • Combines well with COUNT() for unique counts.
  • DISTINCT is simpler, while GROUP BY is more powerful.

📚 Continue Learning

💬 Got a question about SQL DISTINCT? Drop it in the comments below!




Leave a Comment