CodeBlanks Logo Icon CodeBlanks

The ON clause

Choose which column to match on.

Table aliases

In JOINs you alias table names for brevity. With FROM orders o you give the alias o.

SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id;

This query orders aliased as o, customers as c.

o.id, c.name is selected from orders o. customers c is joined on o.customer_id = c.id, keeping only matching rows.

Table aliases — details

Aliases shorten long table names and make queries readable. ON and SELECT use the alias; the AS keyword is optional.

Practice Table aliases

INNER JOIN orders o and customers c; select o.id and c.name.

Starter code

query.sql
-- join with alias
SELECT o.id, c.name
FROM orders o
JOIN

Sample solution

query.sql
SELECT o.id, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id

Expected output

id  name
1   Ayşe
2   Alex

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 The ON clause: match the terms

Term Meaning
alias short name
JOIN join
ON condition
AS optional

2 Alias

Question
SELECT o.id FROM orders ____ JOIN customers c ON o.customer_id = c.id;
Answer
SELECT o.id FROM orders o JOIN customers c ON o.customer_id = c.id;

Explanation: The o written after the table name is an alias; it lets you write o.id instead of the full name.

3 What does a table alias do?

  • It adds a column
  • Gives the table a short name Correct answer
  • Deletes rows
  • Creates an index

Explanation: An alias shortens a table name.

All lessons in Beginner