Docs
Docs/Aggregations & Grouping/DISTINCT

DISTINCT

Remove duplicate rows from results

DISTINCT removes duplicate rows from your result set, returning only unique values.

Basic DISTINCT

-- Get unique countries
SELECT DISTINCT country FROM users;

-- Without DISTINCT (may have duplicates)
SELECT country FROM users;

Multiple Columns

-- Unique country + city combinations
SELECT DISTINCT country, city FROM users;

-- This considers the COMBINATION as unique
-- So USA, NYC and USA, LA are both returned

COUNT DISTINCT

-- Count unique customers who ordered
SELECT COUNT(DISTINCT user_id) AS unique_customers
FROM orders;

-- Compare: total orders vs unique customers
SELECT 
    COUNT(*) AS total_orders,
    COUNT(DISTINCT user_id) AS unique_customers
FROM orders;

DISTINCT vs GROUP BY

-- These produce the same result:
SELECT DISTINCT country FROM users;
SELECT country FROM users GROUP BY country;

-- But GROUP BY allows aggregation:
SELECT country, COUNT(*) FROM users GROUP BY country;

Performance: DISTINCT can be slow on large result sets as it must compare all rows. Consider if you really need unique values.

Need help?

Join our Discord community for support and discussions.

Join Discord