lesson 09 Calculations & data quality · beginner
Unique values with DISTINCT
Return one copy of each distinct value.
the idea
DISTINCT removes duplicate result rows. It applies to the whole selected row: with two columns, a row is a duplicate only when both values repeat. Use it to answer “which values occur?”, not “how many rows are there?”.
Dataset: Retail
orders6 columns · 6,722 rows
| order_idnumber · unique id | invoice_notext | customer_idnumber · links to customers | ordered_atdate & time | countrytext | order_typetext |
|---|---|---|---|---|---|
| 1 | 489438 | 18102 | 2009-12-01 09:24:00 | United Kingdom | sale |
| 2 | 489517 | 16329 | 2009-12-01 11:34:00 | United Kingdom | sale |
| 3 | C489528 | 16714 | 2009-12-01 11:50:00 | United Kingdom | cancellation |
| 4 | 489529 | 17984 | 2009-12-01 11:51:00 | United Kingdom | sale |
| 5 | 489532 | 13394 | 2009-12-01 11:58:00 | United Kingdom | sale |
| 6 | 489540 | 12913 | 2009-12-01 12:18:00 | United Kingdom | sale |
| 7 | C489541 | 15091 | 2009-12-01 12:18:00 | United Kingdom | cancellation |
| 8 | C489549 | 13768 | 2009-12-01 12:33:00 | United Kingdom | cancellation |
| 9 | C489555 | 16955 | 2009-12-01 12:44:00 | United Kingdom | cancellation |
| 10 | 489559 | 17056 | 2009-12-01 12:55:00 | United Kingdom | sale |
- order_idnumber · unique id
- 1
- invoice_notext
- 489438
- customer_idnumber · links to customers
- 18102
- ordered_atdate & time
- 2009-12-01 09:24:00
- countrytext
- United Kingdom
- order_typetext
- sale
- order_idnumber · unique id
- 2
- invoice_notext
- 489517
- customer_idnumber · links to customers
- 16329
- ordered_atdate & time
- 2009-12-01 11:34:00
- countrytext
- United Kingdom
- order_typetext
- sale
- order_idnumber · unique id
- 3
- invoice_notext
- C489528
- customer_idnumber · links to customers
- 16714
- ordered_atdate & time
- 2009-12-01 11:50:00
- countrytext
- United Kingdom
- order_typetext
- cancellation
- order_idnumber · unique id
- 4
- invoice_notext
- 489529
- customer_idnumber · links to customers
- 17984
- ordered_atdate & time
- 2009-12-01 11:51:00
- countrytext
- United Kingdom
- order_typetext
- sale
- order_idnumber · unique id
- 5
- invoice_notext
- 489532
- customer_idnumber · links to customers
- 13394
- ordered_atdate & time
- 2009-12-01 11:58:00
- countrytext
- United Kingdom
- order_typetext
- sale
- order_idnumber · unique id
- 6
- invoice_notext
- 489540
- customer_idnumber · links to customers
- 12913
- ordered_atdate & time
- 2009-12-01 12:18:00
- countrytext
- United Kingdom
- order_typetext
- sale
- order_idnumber · unique id
- 7
- invoice_notext
- C489541
- customer_idnumber · links to customers
- 15091
- ordered_atdate & time
- 2009-12-01 12:18:00
- countrytext
- United Kingdom
- order_typetext
- cancellation
- order_idnumber · unique id
- 8
- invoice_notext
- C489549
- customer_idnumber · links to customers
- 13768
- ordered_atdate & time
- 2009-12-01 12:33:00
- countrytext
- United Kingdom
- order_typetext
- cancellation
- order_idnumber · unique id
- 9
- invoice_notext
- C489555
- customer_idnumber · links to customers
- 16955
- ordered_atdate & time
- 2009-12-01 12:44:00
- countrytext
- United Kingdom
- order_typetext
- cancellation
- order_idnumber · unique id
- 10
- invoice_notext
- 489559
- customer_idnumber · links to customers
- 17056
- ordered_atdate & time
- 2009-12-01 12:55:00
- countrytext
- United Kingdom
- order_typetext
- sale
first 10 of 6,722 rows · your queries use all of them
customers4 columns · 848 rows
| customer_idnumber · unique id | countrytext | regiontext | first_order_atdate & time |
|---|---|---|---|
| 12347 | Iceland | Europe | 2010-10-31 14:20:00 |
| 12353 | Bahrain | Rest of World | 2010-10-27 12:44:00 |
| 12359 | Cyprus | Europe | 2009-12-05 13:32:00 |
| 12373 | Austria | Europe | 2010-02-04 12:08:00 |
| 12374 | Austria | Europe | 2010-04-30 14:01:00 |
| 12384 | Switzerland | Europe | 2011-08-10 11:23:00 |
| 12389 | Australia | Rest of World | 2010-02-22 10:15:00 |
| 12392 | Australia | Rest of World | 2010-04-27 14:04:00 |
| 12404 | Finland | Europe | 2010-01-26 16:25:00 |
| 12416 | Australia | Rest of World | 2010-02-21 10:19:00 |
- customer_idnumber · unique id
- 12347
- countrytext
- Iceland
- regiontext
- Europe
- first_order_atdate & time
- 2010-10-31 14:20:00
- customer_idnumber · unique id
- 12353
- countrytext
- Bahrain
- regiontext
- Rest of World
- first_order_atdate & time
- 2010-10-27 12:44:00
- customer_idnumber · unique id
- 12359
- countrytext
- Cyprus
- regiontext
- Europe
- first_order_atdate & time
- 2009-12-05 13:32:00
- customer_idnumber · unique id
- 12373
- countrytext
- Austria
- regiontext
- Europe
- first_order_atdate & time
- 2010-02-04 12:08:00
- customer_idnumber · unique id
- 12374
- countrytext
- Austria
- regiontext
- Europe
- first_order_atdate & time
- 2010-04-30 14:01:00
- customer_idnumber · unique id
- 12384
- countrytext
- Switzerland
- regiontext
- Europe
- first_order_atdate & time
- 2011-08-10 11:23:00
- customer_idnumber · unique id
- 12389
- countrytext
- Australia
- regiontext
- Rest of World
- first_order_atdate & time
- 2010-02-22 10:15:00
- customer_idnumber · unique id
- 12392
- countrytext
- Australia
- regiontext
- Rest of World
- first_order_atdate & time
- 2010-04-27 14:04:00
- customer_idnumber · unique id
- 12404
- countrytext
- Finland
- regiontext
- Europe
- first_order_atdate & time
- 2010-01-26 16:25:00
- customer_idnumber · unique id
- 12416
- countrytext
- Australia
- regiontext
- Rest of World
- first_order_atdate & time
- 2010-02-21 10:19:00
first 10 of 848 rows · your queries use all of them
worked example
The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.
See the same country many times. Run this. Country repeats across orders—those duplicates are what DISTINCT will remove.
step-1.sqlSELECT order_id, country FROM orders LIMIT 15;
Keep each order type once. Run this. DISTINCT collapses the result to one row per different order_type value.the pattern
example.sqlSELECT DISTINCT order_type FROM orders;
common mistake
SELECT DISTINCT country, region keeps one row per pair. It does not return each country only once if a country appears with two regions.