BBampaa

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 →
About 5–10 minutes · no sign-up needed

Example

Each value once

SELECT DISTINCT category
FROM products

Result:

category
Electronics
Clothing
Home

Step by step

  1. Removes repeats, so each value is listed once.
    SELECT DISTINCT category FROM products
  2. It goes straight after SELECT, before the columns.
    SELECT DISTINCT category FROM products
  3. With two columns, a row goes only if both repeat.
    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. Using store_sales, list each unique combination of store_id and channel (no repeats).
SELECT DISTINCT store_id, channel FROM store_sales

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