runtime
17 / 88

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 idcategory_nametextdepartmenttext
1Christmas & SeasonalSeasonal
2Candles & LightingHome
3Kitchen & DiningHome
4Home DecorHome
5Storage & OrganisationHome
6Bags & AccessoriesFashion
7JewelleryFashion
8Stationery & Gift WrapPaper & Party
9Party & CelebrationsPaper & Party
10Toys & GamesKids
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 idcountrytextregiontextfirst_order_atdate & time
12347IcelandEurope2010-10-31 14:20:00
12353BahrainRest of World2010-10-27 12:44:00
12359CyprusEurope2009-12-05 13:32:00
12373AustriaEurope2010-02-04 12:08:00
12374AustriaEurope2010-04-30 14:01:00
12384SwitzerlandEurope2011-08-10 11:23:00
12389AustraliaRest of World2010-02-22 10:15:00
12392AustraliaRest of World2010-04-27 14:04:00
12404FinlandEurope2010-01-26 16:25:00
12416AustraliaRest of World2010-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 idinvoice_notextcustomer_idnumber · links to customersordered_atdate & timecountrytextorder_typetext
1489438181022009-12-01 09:24:00United Kingdomsale
2489517163292009-12-01 11:34:00United Kingdomsale
3C489528167142009-12-01 11:50:00United Kingdomcancellation
4489529179842009-12-01 11:51:00United Kingdomsale
5489532133942009-12-01 11:58:00United Kingdomsale
6489540129132009-12-01 12:18:00United Kingdomsale
7C489541150912009-12-01 12:18:00United Kingdomcancellation
8C489549137682009-12-01 12:33:00United Kingdomcancellation
9C489555169552009-12-01 12:44:00United Kingdomcancellation
10489559170562009-12-01 12:55:00United Kingdomsale
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.

  1. List tables in the database. Run this. sqlite_schema names every table and view the database defines.

    step-1.sql
    SELECT name, type
    FROM sqlite_schema
    WHERE type IN ('table', 'view')
    ORDER BY name;
  2. Read a table’s CREATE statement. Run this. The sql column shows columns, primary keys, and foreign keys for orders.the pattern

    example.sql
    SELECT name, sql
    FROM sqlite_schema
    WHERE type = 'table' AND name = 'orders';
  3. 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.sql
    SELECT 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.

challenge 1

Find every user table in this database.

Skip SQLite’s own internal tables.

Return name and type.

challenge-1.sql
 
Output
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Databases, schemas, tables & keys — Joins & sets · Runtime