CodeBlanks Logo Icon CodeBlanks

Choosing the join column

Learn the difference between USING and ON.

Matching column choice

When joining equally named columns, USING is a shortcut. For different names use ON.

SELECT * FROM orders
INNER JOIN customers
  USING (customer_id);

USING works when customer_id is in both tables.

All columns are selected from orders. customers USING is joined, keeping only matching rows.

Matching column choice — details

USING is only for same-named columns. When names differ, ON must be written explicitly.

For example, if both orders and customers have customer_id, you write USING (customer_id); the column then appears only once in the result.

Practice Matching column choice

Join using the customer_id column with USING.

Starter code

query.sql
-- join with using
SELECT *
FROM orders
INNER JOIN customers

Sample solution

query.sql
SELECT *
FROM orders
INNER JOIN customers
  USING (customer_id);

Expected output

order_id  customer_id
1         10
2         11

Questions and answers

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

1 Add USING

Question
SELECT * FROM orders
INNER JOIN customers
____ (customer_id);
Answer
SELECT * FROM orders
INNER JOIN customers
USING (customer_id);

Explanation: When both tables share the column name you can write USING (customer_id) instead of ON.

2 When is USING used?

  • When the column names are the same Correct answer
  • When the column names are different
  • Always
  • Never

Explanation: USING is only for same-named columns.

3 Choosing the join column: match the terms

Term Meaning
USING same name
ON different name
WHERE filter
AS alias

All lessons in Intermediate