×

sql · level 2 · unit 2 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

pairs each row with its match in another table.

Looking up ids by hand gets old fast. JOIN does it for you. Write pets JOIN owners in FROM, then says which columns must match: the pet's owner_id and the owner's id.

Each pet is paired with its owner, and the pair becomes one wide row. Name each column as table.column, like owners.name, because both tables have a name.

A plain JOIN is short for : it keeps only rows that found a match. Nibbles has no owner, so Nibbles vanishes from the result.

SELECT pets.name, owners.name AS owner
FROM pets
JOIN owners ON pets.owner_id = owners.id;

Without ORDER BY, joined rows come back in whatever order is quickest for the database.