PostgreSQL SELECT DISTINCT

Remove duplicate rows from query results

🎯 What is SELECT DISTINCT?

SELECT DISTINCT removes duplicate rows from query results, returning only unique values. It's useful when you want to see all different values in a column without repetition.


-- Get unique cities from customers table
SELECT DISTINCT city FROM customers;
                                    

Key Concepts

🔍

Single Column

Get unique values from one column

SELECT DISTINCT country FROM customers;
📊

Multiple Columns

Get unique combinations of columns

SELECT DISTINCT city, country FROM customers;
🗑️

Remove Duplicates

Eliminate repeated rows automatically

SELECT DISTINCT email FROM users;

Clean Results

Get organized, unique data sets

SELECT DISTINCT category FROM products;

🔹 Basic SELECT DISTINCT

The SELECT DISTINCT statement retrieves unique values from a column, automatically removing any duplicate entries. This is particularly helpful when analyzing data categories, customer locations, or product types where you only need to see each unique value once.

-- Get all unique countries
SELECT DISTINCT country FROM customers;

-- Result: Each country appears only once
-- USA
-- Canada
-- Mexico
-- UK

Output:

country
USA
Canada
Mexico
UK

🔹 DISTINCT with Multiple Columns

When using DISTINCT with multiple columns, PostgreSQL returns unique combinations of all specified columns together. Each row in the result will have a unique combination of values across all selected columns, not just individual column uniqueness.

-- Get unique city and country combinations
SELECT DISTINCT city, country 
FROM customers;

-- Result: Unique pairs of city and country
-- New York, USA
-- Toronto, Canada
-- New York, Canada (different from New York, USA)

Output:

city country
New York USA
Toronto Canada
New York Canada

🔹 DISTINCT with WHERE Clause

You can combine DISTINCT with WHERE to filter data before removing duplicates. This allows you to get unique values from a specific subset of your data, making your queries more targeted and efficient for specific analysis needs.

-- Get unique cities only from USA
SELECT DISTINCT city 
FROM customers 
WHERE country = 'USA';

-- Get unique product categories with price > 100
SELECT DISTINCT category 
FROM products 
WHERE price > 100;

Output:

city
New York
Los Angeles
Chicago

🔹 DISTINCT ON Expression

PostgreSQL offers DISTINCT ON to get the first row for each unique value in specified columns. This powerful feature lets you retrieve one representative row per group, which is useful for getting the latest order per customer or first entry per category.

-- Get first order for each customer
SELECT DISTINCT ON (customer_id) 
    customer_id, order_date, total 
FROM orders 
ORDER BY customer_id, order_date;

-- Get highest priced product per category
SELECT DISTINCT ON (category) 
    category, product_name, price 
FROM products 
ORDER BY category, price DESC;

Output:

customer_id order_date total
1 2024-01-15 $150.00
2 2024-01-20 $200.00

🔹 Common Use Cases

SELECT DISTINCT is commonly used for data analysis, reporting, and creating dropdown lists in applications. It helps identify unique categories, locations, or values in your database, making it essential for understanding data distribution and creating user-friendly interfaces.

Practical Examples:

  • Find all product categories: SELECT DISTINCT category FROM products;
  • List all customer cities: SELECT DISTINCT city FROM customers;
  • Get unique order statuses: SELECT DISTINCT status FROM orders;
  • Find all employee departments: SELECT DISTINCT department FROM employees;

🧠 Test Your Knowledge

What does SELECT DISTINCT do?