BBampaa

Lessons › SQL › Chapter 1: Reading data

SQL lesson

DISTINCT for unique values

DISTINCT removes duplicates so each value appears exactly once in your results, no matter how many times it shows up in the original data.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Finding the unique values

SELECT DISTINCT category FROM products

Result:

category
Electronics
Clothing
Home

Step by step

  1. Plain SELECT returns every row - duplicates and all.
    SELECT category
    FROM products;
  2. DISTINCT goes right after SELECT and collapses duplicate values into one.
    SELECT DISTINCT category
    FROM products;
  3. Swap in another column and DISTINCT applies the same way - only unique values come back.
    SELECT DISTINCT category
    FROM products;

Practice

Try each one in your head first, then open the answer.

1. On the orders table, write a query to list each unique category value (no repeats).
SELECT DISTINCT category FROM orders
2. On the products table, write a query to list each unique brand value (no repeats).
SELECT DISTINCT brand FROM products
3. On the customers table, write a query to list each unique full_name value (no repeats).
SELECT DISTINCT full_name FROM customers
4. On the inventory table, write a query to list each unique warehouse value (no repeats).
SELECT DISTINCT warehouse FROM inventory
5. On the returns table, write a query to list each unique return_id value (no repeats).
SELECT DISTINCT return_id FROM returns

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed