DELAPARRA
Index
Color
NL
Category: SQL

DISTINCT

Explained by Updated

What is DISTINCT?

DISTINCT: Removes duplicate rows from the result, or counts unique values with COUNT(DISTINCT col). DISTINCT removes duplicate rows from a query result. SELECT DISTINCT country FROM customers lists each country once. COUNT(DISTINCT customer_id) counts unique customers instead of orders. It is handy for questions like how many different customers bought something this month, or which cities you deliver to.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about DISTINCT

What should you keep in mind with DISTINCT?

If you need DISTINCT to fix duplicates caused by a join, look at the join first. Duplicates often mean a one-to-many relationship you did not account for, and DISTINCT hides the symptom while the query still does extra work. On large tables, DISTINCT can be slow because the database must sort or hash all rows. An index on the column, or a GROUP BY with the information you actually need, is often faster.

What does an example of DISTINCT look like?

SELECT COUNT(DISTINCT country) FROM users;

At a glance

CategorySQL
Explained by
Updated

More SQL terms

All terms starting with D

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.