lesson 17 Joins & sets · beginner
Databases, schemas, tables & keys
Understand tables, keys and how related data is organized.
the idea
A database holds tables; sqlite_schema lists them with their definitions. A primary key identifies each row of a table; a foreign key stores another table’s key, so orders.customer_id points at customers.customer_id. Joining along that relationship (FROM orders JOIN customers ON customers.customer_id = orders.customer_id) attaches customer attributes to each order.
Dataset: Retail
categories3 columns · 17 rows
| category_idnumber · unique id | category_nametext | departmenttext |
|---|---|---|
| 1 | Christmas & Seasonal | Seasonal |
| 2 | Candles & Lighting | Home |
| 3 | Kitchen & Dining | Home |
| 4 | Home Decor | Home |
| 5 | Storage & Organisation | Home |
| 6 | Bags & Accessories | Fashion |
| 7 | Jewellery | Fashion |
| 8 | Stationery & Gift Wrap | Paper & Party |
| 9 | Party & Celebrations | Paper & Party |
| 10 | Toys & Games | Kids |
- category_idnumber · unique id
- 1
- category_nametext
- Christmas & Seasonal
- departmenttext
- Seasonal
- category_idnumber · unique id
- 2
- category_nametext
- Candles & Lighting
- departmenttext
- Home
- category_idnumber · unique id
- 3
- category_nametext
- Kitchen & Dining
- departmenttext
- Home
- category_idnumber · unique id
- 4
- category_nametext
- Home Decor
- departmenttext
- Home
- category_idnumber · unique id
- 5
- category_nametext
- Storage & Organisation
- departmenttext
- Home
- category_idnumber · unique id
- 6
- category_nametext
- Bags & Accessories
- departmenttext
- Fashion
- category_idnumber · unique id
- 7
- category_nametext
- Jewellery
- departmenttext
- Fashion
- category_idnumber · unique id
- 8
- category_nametext
- Stationery & Gift Wrap
- departmenttext
- Paper & Party
- category_idnumber · unique id
- 9
- category_nametext
- Party & Celebrations
- departmenttext
- Paper & Party
- category_idnumber · unique id
- 10
- category_nametext
- Toys & Games
- departmenttext
- Kids
first 10 of 17 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
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
worked example
The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.
List tables in the database. Run this. sqlite_schema names every table and view the database defines.
step-1.sqlSELECT name, type FROM sqlite_schema WHERE type IN ('table', 'view') ORDER BY name;
Read a table’s CREATE statement. Run this. The sql column shows columns, primary keys, and foreign keys for orders.the pattern
example.sqlSELECT name, sql FROM sqlite_schema WHERE type = 'table' AND name = 'orders';
Attach customer columns with JOIN. Run this. Each order’s customer_id looks up customers. An order with no customer_id does not appear.
step-3.sqlSELECT o.order_id, o.customer_id, c.country, c.region FROM orders o JOIN customers c ON c.customer_id = o.customer_id ORDER BY o.order_id LIMIT 10;
common mistake
A NULL foreign key (an anonymous order) matches no customer, so an inner join drops it.