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

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

categories3 columns · 17 rows
category_idnumber · unique idcategory_nametextdepartmenttext
1Christmas & SeasonalSeasonal
2Candles & LightingHome
3Kitchen & DiningHome
4Home DecorHome
5Storage & OrganisationHome
6Bags & AccessoriesFashion
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

first 6 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
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

first 6 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
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

first 6 of 6,722 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not 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 LEFT JOIN. Run this. Each order’s customer_id looks up customers; anonymous orders stay with NULL customer fields.

    step-3.sql
    SELECT o.order_id, o.customer_id, c.country, c.region
    FROM orders o
    LEFT 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

Return name and type for every table in the Retail database from sqlite_schema (type = 'table', excluding names that start with sqlite_).

what to return

Columns
nametype in this order
Rows
any order
challenge-1.sql
 
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