JOIN USING Versus ON Output Columns

JOIN USING coalesces the shared column into one output column while ON keeps both sides, and NATURAL joins implicitly on every shared name.

SqlData Integrity

Code

snippet
-- ON:两边的 customer_id 都会出现在 SELECT * 里
SELECT *
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
LIMIT 5;

-- USING:同名列合并成一列,SELECT * 只有一个 customer_id
-- 注意:USING 之后不能用表前缀引用它,只能写裸列名 customer_id
SELECT *
FROM orders o
JOIN customers c USING (customer_id)
LIMIT 5;

-- NATURAL:隐式按所有同名列连接;列结构一变结果就静默改变,生产慎用
SELECT COUNT(*)
FROM orders o
NATURAL JOIN customers c;

What the code does

SELECT statements share the script, touching orders and customers.

The SQL constructs in play: 3 JOINs combining rows across tables; COUNT aggregation; a row cap (LIMIT) bounding the result size.

What to watch for

  • Both demo tables are small and the two SELECT * probes carry a LIMIT, so each attempt stays cheap; what transfers to a real schema is the ON/USING/NATURAL output difference, not the row counts.

Applying it

Run it in the orders database context; the LIMIT keeps each attempt cheap.

Statements that matter

SELECT *
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
LIMIT 5;
SELECT *
FROM orders o
JOIN customers c USING (customer_id)
LIMIT 5;
SELECT COUNT(*)
FROM orders o
NATURAL JOIN customers c;

Interactive Tools for This Topic

FAQ

Name the tables involved in this file.
`orders`, `customers`, combined through 3 joins.