×

sql · level 2 · unit 2 · lesson 3

  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

A table alias keeps joins short and clear.

Typing owners.name and pets.owner_id gets long. Give each table an alias straight after its name in FROM: pets p and owners o. Then the p. means pets and o. means owners.

Prefixes matter because name on its own is here: both tables have one. SQLite will not guess. It stops with ambiguous column name: name.

Clashing columns would also share a heading in the result, so give each one an alias with AS, like o.name AS owner.

SELECT p.name AS pet, o.name AS owner, o.town
FROM pets p
JOIN owners o ON p.owner_id = o.id
WHERE o.town = 'Leeds';

FROM pets AS p works too. Once a table has an alias, use the alias everywhere in that query.