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 →Example
Finding the unique values
SELECT DISTINCT category FROM products| category |
|---|
| Electronics |
| Clothing |
| Home |
Step by step
- Plain SELECT returns every row - duplicates and all.
SELECT category FROM products; - DISTINCT goes right after SELECT and collapses duplicate values into one.
SELECT DISTINCT category FROM products; - Swap in another column and DISTINCT applies the same way - only unique values come back.
SELECT DISTINCT category FROM products;
Practice
1. On the orders table, write a query to list each unique category value (no repeats).
SELECT DISTINCT category FROM orders2. On the products table, write a query to list each unique brand value (no repeats).
SELECT DISTINCT brand FROM products3. On the customers table, write a query to list each unique full_name value (no repeats).
SELECT DISTINCT full_name FROM customers4. On the inventory table, write a query to list each unique warehouse value (no repeats).
SELECT DISTINCT warehouse FROM inventory5. On the returns table, write a query to list each unique return_id value (no repeats).
SELECT DISTINCT return_id FROM returnsLearn 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 →