lesson 06 Filtering & sorting · beginner
Lists & text patterns
Match lists of values and patterns in text.
the idea
IN tests membership in a list and replaces a chain of ORs. LIKE matches text patterns: % stands for any run of characters and _ for exactly one. In SQLite, LIKE ignores letter case for plain ASCII letters.
Dataset: Retail
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
products6 columns · 5,304 rows
| stock_codetext · unique id | descriptiontext | category_idnumber · links to categories | list_pricedecimal | first_sold_atdate & time | is_merchandisenumber |
|---|---|---|---|---|---|
| 10002 | INFLATABLE POLITICAL GLOBE | 17 | 0.85 | 2009-12-01 09:08:00 | 1 |
| 10002R | ROBOT PENCIL SHARPNER | 8 | 5.17 | 2009-12-02 14:43:00 | 1 |
| 10080 | GROOVY CACTUS INFLATABLE | 17 | 0.39 | 2009-12-02 16:02:00 | 1 |
| 10109 | BENDY COLOUR PENCILS | 8 | 0.42 | 2009-12-03 12:31:00 | 1 |
| 10120 | DOGGY RUBBER | 17 | 0.21 | 2009-12-01 16:17:00 | 1 |
| 10123C | HEARTS WRAPPING TAPE | 8 | 0.65 | 2009-12-01 14:28:00 | 1 |
| 10123G | ARMY CAMO WRAPPING TAPE | 8 | 0.65 | 2009-12-01 14:28:00 | 1 |
| 10124A | SPOTS ON RED BOOKCOVER TAPE | 8 | 0.42 | 2010-02-11 18:13:00 | 1 |
| 10124C | NULL | NULL | NULL | 2010-05-11 12:51:00 | 1 |
| 10124G | ARMY CAMO BOOKCOVER TAPE | 8 | 0.42 | 2010-05-13 16:49:00 | 1 |
- stock_codetext · unique id
- 10002
- descriptiontext
- INFLATABLE POLITICAL GLOBE
- category_idnumber · links to categories
- 17
- list_pricedecimal
- 0.85
- first_sold_atdate & time
- 2009-12-01 09:08:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10002R
- descriptiontext
- ROBOT PENCIL SHARPNER
- category_idnumber · links to categories
- 8
- list_pricedecimal
- 5.17
- first_sold_atdate & time
- 2009-12-02 14:43:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10080
- descriptiontext
- GROOVY CACTUS INFLATABLE
- category_idnumber · links to categories
- 17
- list_pricedecimal
- 0.39
- first_sold_atdate & time
- 2009-12-02 16:02:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10109
- descriptiontext
- BENDY COLOUR PENCILS
- category_idnumber · links to categories
- 8
- list_pricedecimal
- 0.42
- first_sold_atdate & time
- 2009-12-03 12:31:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10120
- descriptiontext
- DOGGY RUBBER
- category_idnumber · links to categories
- 17
- list_pricedecimal
- 0.21
- first_sold_atdate & time
- 2009-12-01 16:17:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10123C
- descriptiontext
- HEARTS WRAPPING TAPE
- category_idnumber · links to categories
- 8
- list_pricedecimal
- 0.65
- first_sold_atdate & time
- 2009-12-01 14:28:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10123G
- descriptiontext
- ARMY CAMO WRAPPING TAPE
- category_idnumber · links to categories
- 8
- list_pricedecimal
- 0.65
- first_sold_atdate & time
- 2009-12-01 14:28:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10124A
- descriptiontext
- SPOTS ON RED BOOKCOVER TAPE
- category_idnumber · links to categories
- 8
- list_pricedecimal
- 0.42
- first_sold_atdate & time
- 2010-02-11 18:13:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10124C
- descriptiontext
- NULL
- category_idnumber · links to categories
- NULL
- list_pricedecimal
- NULL
- first_sold_atdate & time
- 2010-05-11 12:51:00
- is_merchandisenumber
- 1
- stock_codetext · unique id
- 10124G
- descriptiontext
- ARMY CAMO BOOKCOVER TAPE
- category_idnumber · links to categories
- 8
- list_pricedecimal
- 0.42
- first_sold_atdate & time
- 2010-05-13 16:49:00
- is_merchandisenumber
- 1
first 10 of 5,304 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.
Match descriptions that start a certain way. Run this. LIKE with VINTAGE% keeps rows whose description begins with VINTAGE.the pattern
example.sqlSELECT stock_code, description FROM products WHERE description LIKE 'VINTAGE%';
Test many values with IN. Run this. IN is a compact way to filter to customers in Sweden, Norway, or Denmark.
step-2.sqlSELECT customer_id, country FROM customers WHERE country IN ('Sweden', 'Norway', 'Denmark');
common mistake
LIKE 'CAKESTAND' without % matches only that exact text. Use '%CAKESTAND%' to find it anywhere in a description.