What Is SQL DISTINCT?

SQL DISTINCT removes duplicate rows from a result set, returning only unique combinations of the selected columns. Place the DISTINCT keyword right after SELECT to deduplicate data for reporting, aggregation, or sub-queries. Use cautiously on large tables because it forces a sort or hash step that may slow queries.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
SQL DISTINCT eliminates duplicate rows in a query result, delivering only unique values for the specified column list. Add DISTINCT immediately after SELECT to deduplicate data for cleaner reports and accurate aggregates.

What Does SQL DISTINCT Do?

SQL DISTINCT removes duplicate rows from the result set, guaranteeing each returned row is unique for the selected columns. This deduplication happens after filtering in the logical query order.

When Should I Use SQL DISTINCT?

Use DISTINCT when you need a list of unique values, deduplicated reports, or inputs for IN clauses. Avoid it if the table already enforces uniqueness with constraints.

How Does SQL DISTINCT Work Internally?

Most databases implement DISTINCT by sorting or hashing the result set on the requested columns, then discarding duplicates. Both operations add CPU, memory, and possible disk I/O.

What Is the Basic Syntax of SQL DISTINCT?

Place DISTINCT immediately after SELECT. Example:

SELECT DISTINCT city FROM customers;

Can SQL DISTINCT Handle Multiple Columns?

DISTINCT can span several columns; uniqueness applies to their combined values.

SELECT DISTINCT first_name, last_name FROM users;

How Do I Count Distinct Values?

Wrap the column in COUNT(DISTINCT column). Example:

SELECT COUNT(DISTINCT status) AS status_count FROM orders;

How Do I Use DISTINCT with Aggregate Functions?

DISTINCT can appear inside SUM, AVG, or other aggregates to eliminate duplicate inputs.

SELECT SUM(DISTINCT amount) AS unique_sales FROM invoices;

How Do I Remove Duplicate Rows Altogether?

Combine DISTINCT with INSERT INTO … SELECT or use CTEs plus ROW_NUMBER() to permanently delete duplicates.

Does SQL DISTINCT Affect Performance?

DISTINCT can slow queries because it forces a sort or hash step. Indexes on the DISTINCT columns mitigate cost by avoiding full table scans.

How Can I Optimize Queries Using DISTINCT?

Project only needed columns, filter early, index DISTINCT columns, and avoid DISTINCT inside subqueries that feed GROUP BY aggregates.

What Are Alternatives to SQL DISTINCT?

GROUP BY without aggregates, EXISTS subqueries, or window functions like ROW_NUMBER() offer deduplication without DISTINCT.

Key Takeaways on SQL DISTINCT

DISTINCT returns unique rows, is simple to use, but may harm performance. Indexes, minimal column selection, and aggregate awareness keep queries fast.

Frequently Asked Questions (FAQs)

Does SQL DISTINCT work with TEXT or JSON columns?

Yes, as long as the data type supports equality comparisons. Large objects may slow sorting.

Can I alias a column when using DISTINCT?

Absolutely. Place the alias after the column list: SELECT DISTINCT country AS unique_country FROM customers;.

Is COUNT(DISTINCT *) valid SQL?

No. COUNT(DISTINCT) requires a specific column or expression.

How can I test performance impact of DISTINCT?

Use EXPLAIN plans before and after adding DISTINCT, and watch for Sort or HashAggregate operations.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo