← Back to SQL, page 3

sql-0060 · Medium mid

How do JOIN ... USING, JOIN ... ON and NATURAL JOIN behave? Why do many teams avoid NATURAL JOIN?

ON takes any condition. USING (col) is shorthand for equality on columns with the same name in both tables, and it outputs that column once. NATURAL JOIN joins on every column name the two tables share, chosen automatically. That is risky: adding an unrelated column with a common name, such as created_at, silently changes the join condition.

Full answer

Three ways to say "match on the key":

sql
CREATE TABLE dept (dept_id INTEGER, dept_name TEXT);
CREATE TABLE staff (staff_name TEXT, dept_id INTEGER);
INSERT INTO dept VALUES (1,'Library'),(2,'Canteen');
INSERT INTO staff VALUES ('Ira',1),('Jon',2),('Kim',1);

ON is the general form. Any condition works, and both key columns stay available with their table prefixes:

sql
SELECT s.staff_name, d.dept_name
FROM staff s JOIN dept d ON d.dept_id = s.dept_id
ORDER BY s.staff_name;
staff_namedept_name
IraLibrary
JonCanteen
KimLibrary

USING (id) requires a column with the same name in both tables, compares them with =, and returns it once without a prefix. It reads nicely when keys share names, which these tables do not, so here is a tiny pair that does:

sql
SELECT dept_id, staff_name, dept_name
FROM staff JOIN dept USING (dept_id)
ORDER BY staff_name;
dept_idstaff_namedept_name
1IraLibrary
2JonCanteen
1KimLibrary

NATURAL JOIN goes further and joins on every column name the tables have in common:

sql
SELECT staff_name, dept_name FROM staff NATURAL JOIN dept ORDER BY staff_name;
staff_namedept_name
IraLibrary
JonCanteen
KimLibrary

It works today. Then someone adds an updated_on column to both tables for auditing:

sql
ALTER TABLE dept  ADD COLUMN updated_on TEXT DEFAULT '2026-01-01';
ALTER TABLE staff ADD COLUMN updated_on TEXT DEFAULT '2026-02-15';
SELECT staff_name, dept_name FROM staff NATURAL JOIN dept ORDER BY staff_name;
staff_namedept_name

(0 rows)

The same NATURAL JOIN now also requires updated_on to match, and the rows vanish without any error. The query text never changed. That is why style guides often ban NATURAL JOIN in application code. USING is generally considered safe, because the join columns are written down.

sql
CREATE TABLE dept (dept_id INTEGER, dept_name TEXT);
CREATE TABLE staff (staff_name TEXT, dept_id INTEGER);
INSERT INTO dept VALUES (1,'Library'),(2,'Canteen');
INSERT INTO staff VALUES ('Ira',1),('Jon',2),('Kim',1);

SELECT s.staff_name, d.dept_name
FROM staff s JOIN dept d ON d.dept_id = s.dept_id
ORDER BY s.staff_name;

SELECT dept_id, staff_name, dept_name
FROM staff JOIN dept USING (dept_id)
ORDER BY staff_name;

SELECT staff_name, dept_name FROM staff NATURAL JOIN dept ORDER BY staff_name;

ALTER TABLE dept  ADD COLUMN updated_on TEXT DEFAULT '2026-01-01';
ALTER TABLE staff ADD COLUMN updated_on TEXT DEFAULT '2026-02-15';
SELECT staff_name, dept_name FROM staff NATURAL JOIN dept ORDER BY staff_name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • ON accepts any condition
  • USING (col) is equality on same-named columns, output once
  • NATURAL JOIN picks join columns automatically from shared names
  • A new common column name can silently change a NATURAL JOIN

Common mistakes

  • Using NATURAL JOIN in code that lives for years
  • Expecting USING to work when key columns have different names

Follow-ups

  • Can you mix USING with extra conditions?
  • How do you refer to the USING column in SELECT on PostgreSQL?

Tips

  • Tell the audit-column story; it is the concrete reason behind the rule