Lessons › SQL › Chapter 1: Reading data
SQL lesson
DISTINCT for unique values
DISTINCT removes repeats, so each value shows up once. Electronics might be in the table ten times, but it's listed once. It goes straight after SELECT. With two columns, a row is a repeat only if both match.
Try this lesson free →Example
Each value once
SELECT DISTINCT category
FROM products| category |
|---|
| Electronics |
| Clothing |
| Home |
Step by step
- Removes repeats, so each value is listed once.
SELECT DISTINCT category FROM products - It goes straight after SELECT, before the columns.
SELECT DISTINCT category FROM products - With two columns, a row goes only if both repeat.
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. Using store_sales, list each unique combination of store_id and channel (no repeats).
SELECT DISTINCT store_id, channel FROM store_salesLearn 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 →