Course contentsShow
← Data Science
Lesson 874 of 2,14522. SQL FoundationsFree lesson

DISTINCT with Multiple Columns

Explore how DISTINCT works with multiple columns to identify unique combinations of values across fields.

DISTINCT with Multiple Columns

What you'll learn: How to use DISTINCT with multiple columns to find unique combinations of values across several fields at once.

When you apply DISTINCT to multiple columns, SQL doesn't just remove duplicates from each column individually—it identifies rows where the combination of all specified columns is unique.

How It Works

Think of it like identifying unique people at a conference. If you only check first names, you might find ten "Alex"s. But if you check the combination of first name and last name, you'll find far fewer duplicates. Each unique pairing counts as one distinct row.

When you write:

SELECT DISTINCT country, city
FROM customers;

SQL returns one row for each unique pair of country and city. You might have multiple customers in "USA, New York" but you'll only see that combination once in the results.

Multiple Columns in Action

SELECT DISTINCT product_category, supplier_id
FROM products;

This query shows every unique combination of category and supplier. If "Electronics" appears with supplier IDs 101, 102, and 103, you'll get three rows—one for each distinct pairing.

Order Matters for Readability

While the combination is what matters, column order affects how results display. DISTINCT country, city and DISTINCT city, country return the same unique combinations but in different column arrangements.

Common Use Cases

  • Finding all unique product-location pairs in inventory
  • Identifying unique customer segments (age group + region)
  • Listing distinct campaign-channel combinations in marketing data

Key Takeaway: DISTINCT with multiple columns returns rows where the entire combination of values is unique, not each column separately—perfect for finding unique pairings or groupings in your data.