SELECT DISTINCT

Overview

The SELECT DISTINCT statement is used to return only distinct (unique) values. It eliminates duplicate rows from the result set.

DISTINCT is useful when you want to see unique values in a column or combination of columns, without duplicate entries.

Basic Syntax

The DISTINCT keyword is placed immediately after SELECT:

DISTINCT Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;

Simple Example

Let's see how DISTINCT works with sample data:

Sample Orders Table
Orders Table:
┌──────────┬─────────────┬────────────┐
│ order_id │ customer_id │ city       │
├──────────┼─────────────┼────────────┤
│ 1        │ 101         │ New York   │
│ 2        │ 102         │ Los Angeles│
│ 3        │ 103         │ New York   │
│ 4        │ 104         │ Chicago    │
│ 5        │ 105         │ New York   │
└──────────┴─────────────┴────────────┘

Without DISTINCT

Query: SELECT city FROM Orders
SELECT city FROM Orders;
Result (with duplicates)
┌────────────┐
│ city       │
├────────────┤
│ New York   │
│ Los Angeles│
│ New York   │
│ Chicago    │
│ New York   │
└────────────┘

With DISTINCT

Query: SELECT DISTINCT city FROM Orders
SELECT DISTINCT city FROM Orders;
Result (unique values only)
┌────────────┐
│ city       │
├────────────┤
│ New York   │
│ Los Angeles│
│ Chicago    │
└────────────┘

DISTINCT with Multiple Columns

When using DISTINCT with multiple columns, it returns unique combinations of those columns:

DISTINCT on Multiple Columns
SELECT DISTINCT city, state
FROM Customers;

This returns unique combinations of city and state. For example:

Sample Result
┌────────────┬───────────┐
│ city       │ state     │
├────────────┼───────────┤
│ New York   │ NY        │
│ Los Angeles│ CA        │
│ Chicago    │ IL        │
│ New York   │ NY        │ ← Only shown once (duplicate)
└────────────┴───────────┘

DISTINCT vs GROUP BY

DISTINCT and GROUP BY can produce similar results, but they serve different purposes:

DISTINCT - Just unique values
SELECT DISTINCT category
FROM Products;
GROUP BY - Unique values with aggregation
SELECT category, COUNT(*) AS product_count
FROM Products
GROUP BY category;

Common Examples

Example 1: Unique Customer Cities

Get list of unique cities
SELECT DISTINCT city
FROM Customers
ORDER BY city;

Example 2: Unique Product Categories

Get all product categories
SELECT DISTINCT category
FROM Products
WHERE active = 1;

Example 3: Unique Combinations

Unique customer and product combinations
SELECT DISTINCT customer_id, product_id
FROM Orders;

DISTINCT with WHERE

You can combine DISTINCT with WHERE to filter before finding unique values:

DISTINCT with Filtering
SELECT DISTINCT city
FROM Customers
WHERE country = 'USA'
ORDER BY city;

DISTINCT with COUNT

You can count distinct values using COUNT(DISTINCT column):

Count Distinct Values
-- Count how many unique cities exist
SELECT COUNT(DISTINCT city) AS unique_cities
FROM Customers;

-- Count unique customers per city
SELECT city, COUNT(DISTINCT customer_id) AS unique_customers
FROM Orders
GROUP BY city;

Performance Considerations

When to Use DISTINCT

When NOT to Use DISTINCT

Best Practices

Next Steps

Continue learning with WHERE clause for filtering, or explore GROUP BY for grouping and aggregation.