Skip to content
/ Chapter 3 · Filtering in Depth
Lesson 3.1 · garden_shop
Lesson 3.1

Choosing Columns and SQL DISTINCT

Name the columns you need, and add DISTINCT when you want each value once.

The SELECT line controls which columns appear in your result. The shortcut * means "show every column", which is useful for exploring a table.

Once you know what you need, name the columns directly. That keeps results focused and makes your query easier for another person to understand.

Naming columns can leave repeats behind. Ask for customers' states and you get one row per customer, so PA appears six times. DISTINCT, written straight after SELECT, keeps each distinct row once.

It compares whole rows, not one column. With two columns selected, a state appears once for every different city it has, because each state and city pair is its own row.

Pattern
select distinct column_one, column_two
from table_name
Schema · Garden Shop Table · customers 9 columns · 20 rows
Table · customers

One row per customer. Some customers have no phone on file.

9 columns · 20 rows
customer_id int first_name text last_name text email text phone text city text state text signup_date date is_active bool
Your task

List every place the shop has customers: each state and city pair once, sorted by state, then city.

·

Run your query to inspect the result.

Expected answer
  • Columns: state, city.
  • Rows: 15 state and city pairs, sorted by state, then city.
  • 20 customers live in 15 places, so five repeated rows disappear.