Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to SQL Notes
Topic #202

SQL SELECT DISTINCT

SELECT DISTINCT removes duplicate rows from the result — it keeps only unique combinations of the selected columns.

Syntax

SELECT DISTINCT column1, column2
FROM table_name;

Sample Table: employees

idnamedepartment
1AmanEngineering
2RiyaMarketing
3KaranEngineering
4NehaMarketing

Example

SELECT DISTINCT department
FROM employees;

Output:

department
Engineering
Marketing

Four rows collapse to two — one per unique department.

DISTINCT on Multiple Columns

SELECT DISTINCT department, salary
FROM employees;

This keeps a row only if the combination of department and salary is unique — not just the department alone. Easy to misread if you're used to DISTINCT on a single column.

Practical Use Case

Finding the list of distinct categories, cities, or statuses present in a table — e.g. "which departments actually have employees right now?" before building a filter dropdown in an app.

Common Mistakes

  • Using DISTINCT * on a wide table when only a couple of columns are actually relevant — this can be needlessly slow
  • Assuming DISTINCT removes duplicates per column independently — it deduplicates the whole row of selected columns together
  • Reaching for DISTINCT to "fix" a query that's actually producing duplicates because of an incorrect join — the real fix is the join logic, not papering over it with DISTINCT

Interview Relevance

A common trick question: "Will SELECT DISTINCT name and SELECT DISTINCT name, department return the same number of rows?" — not necessarily; the second can return more rows if the same name appears with different departments.

Practice Question

Write a query to list every distinct (department, job_title) combination that currently exists in an employees table.

Related SQL Notes

Want to go beyond the notes?

Join CodingNow's SQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →