← Back to SQL, page 4

sql-0061 · Easy fresher

Does FROM a, b WHERE a.id = b.a_id behave any differently from FROM a JOIN b ON a.id = b.a_id?

For inner joins the two forms return the same rows; the comma form is the older SQL-89 style. Explicit JOIN ... ON keeps each join condition next to its table, separates join logic from filters, and makes a forgotten condition obvious. With the comma form, a missing condition silently becomes a cross join.

Full answer

Both of these return the same result:

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT e.name, d.name AS dept
FROM employees e, departments d
WHERE d.id = e.dept_id AND e.salary > 90000
ORDER BY e.name;
namedept
AshaEngineering
MeiEngineering
RaviEngineering
sql
SELECT e.name, d.name AS dept
FROM employees e
JOIN departments d ON d.id = e.dept_id
WHERE e.salary > 90000
ORDER BY e.name;
namedept
AshaEngineering
MeiEngineering
RaviEngineering

The optimizer treats them the same way, so this is about readability and safety, not speed.

The explicit form wins in practice for three reasons. Each join condition sits next to the table it belongs to, so a five-table query can be read top to bottom. Join conditions and filters are separated: ON says how tables relate, WHERE says which rows you want. And outer joins need the explicit syntax anyway (the old vendor forms such as Oracle's (+) are obsolete).

The biggest risk with commas is a missing condition. Forget it, and every employee pairs with every department:

sql
SELECT COUNT(*) AS rows_returned
FROM employees e, departments d
WHERE e.salary > 0 OR e.salary IS NULL;
rows_returned
32

Eight employees times four departments gives 32 rows, with no error. With explicit JOIN syntax on PostgreSQL, leaving out ON is a syntax error instead:

sql
SELECT e.name FROM employees e JOIN departments d WHERE e.salary > 90000;  -- error on purpose
text
Error: syntax error at or near "WHERE"

SQLite is more permissive and accepts JOIN without ON as a cross join, so the protection depends on the engine.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT e.name, d.name AS dept
FROM employees e, departments d
WHERE d.id = e.dept_id AND e.salary > 90000
ORDER BY e.name;

SELECT e.name, d.name AS dept
FROM employees e
JOIN departments d ON d.id = e.dept_id
WHERE e.salary > 90000
ORDER BY e.name;

SELECT COUNT(*) AS rows_returned
FROM employees e, departments d
WHERE e.salary > 0 OR e.salary IS NULL;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Comma joins and JOIN ... ON give the same inner join result
  • Explicit JOIN keeps conditions next to tables and apart from filters
  • A forgotten condition in comma style becomes a silent cross join
  • PostgreSQL rejects JOIN without ON; SQLite treats it as a cross join

Common mistakes

  • Mixing comma joins and explicit joins in one FROM clause
  • Forgetting one condition in a long comma-separated FROM list

Follow-ups

  • How do you write a LEFT JOIN in the comma style?
  • Can mixing comma and JOIN syntax change how aliases are resolved?

Tips

  • Mention the silent cross join; it is the practical reason for the rule