DISTINCT

SQL · Limiting Rows

Theory

DISTINCT removes duplicate rows from the result. It looks at the whole selected row, not at one column: SELECT DISTINCT dept returns each department once, while SELECT DISTINCT dept, city returns each unique (dept, city) pair. Two rows are duplicates only when every selected value matches.

For DISTINCT, NULLs count as equal to each other, so several NULLs collapse into one NULL row. DISTINCT works by sorting or hashing the rows, so it costs time on large data. It also appears inside aggregates: COUNT(DISTINCT city) counts unique cities. If you find yourself adding DISTINCT to hide duplicates caused by a join, the join is usually wrong and is worth fixing instead.

Why we use it

Data often repeats values: many employees share a department, many orders share a customer. DISTINCT gives the list of unique values or combinations, which is what you need for dropdowns, checks and counts.

When to use it

Use DISTINCT to list unique values or count them with COUNT(DISTINCT col). If you also need a count or total per value, use GROUP BY instead. Do not use it to patch duplicate rows from a bad join.

How it works

  1. Rows come out of SELECT
  2. Build a key from all selected columns of the row
  3. Key seen already? Yes: Drop the row as a duplicate. No: Keep the row and remember its key.
  4. Repeat for every row
  5. Return the unique rows

Syntax

SELECT DISTINCT column
FROM table;

Example

SELECT DISTINCT department FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic