Sub topic 14
Reading older comma-style joins
This chapter assumes you can write an INNER JOIN ... ON query, use table aliases, and know that a foreign key links a child column to a parent's primary key. Older code, textbooks and legacy reports often join tables with commas instead. You need to read that style fluently, even if you write the explicit style yourself.
What a comma-style join is
In the implicit join (comma-style) syntax, you list several tables in the FROM clause, separated by commas. You then put the matching condition in WHERE. The sample schema from earlier chapters has customers and orders. This query returns the same rows as the INNER JOIN version:
SELECT c.name, o.id AS order_id
FROM customers AS c, orders AS o
WHERE o.customer_id = c.id;With the sample data, the result has three rows: Ada with orders 101 and 102, and Ben with order 103. Chloe has no orders, and order 104 has no customer, so neither appears.
The explicit equivalent is:
SELECT c.name, o.id AS order_id
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id;For inner joins, the two forms are logically equivalent. The database sees the same request. Its optimizer, the part that chooses how to run a query, normally produces the same execution plan for both.
Why the comma form works
A comma between tables in FROM means a cross join, also called a Cartesian product. Every row of the first table is paired with every row of the second. With 3 customers and 4 orders, that is 12 combined rows.
The WHERE clause then filters those 12 rows. Only the pairs where o.customer_id = c.id survive. Conceptually, an inner join is a Cartesian product followed by a filter. The explicit syntax states this in one place, while the comma syntax splits it across two clauses.
The main danger: a missing condition
Because the join condition lives in WHERE, it is easy to forget. The query still runs without it:
SELECT c.name, o.id AS order_id
FROM customers AS c, orders AS o;This returns all 12 combinations. Ada appears next to order 103, which is not hers. No error is raised. On small test data you may notice. On tables with a million rows each, you get a trillion rows, and the query may run for hours or fill the disk.
With INNER JOIN, leaving out the ON clause is a syntax error in most databases. (MySQL and SQLite are lenient and treat it as a cross join, so do not rely on this.) That early failure is the main reason style guides prefer the explicit form.
Counting conditions
A useful review rule: joining N tables needs at least N − 1 join conditions, forming a connected chain. Here is a four-table comma-style query using the order schema:
SELECT c.name AS customer, o.id AS order_id, p.name AS product, i.quantity
FROM customers AS c, orders AS o, order_items AS i, products AS p
WHERE o.customer_id = c.id
AND i.order_id = o.id
AND p.id = i.product_id;Four tables and three conditions. Each condition connects a new table to one already linked. If you removed the last line, products would be unconnected, and every row would repeat once per product.
Mixing joins and filters in WHERE
In comma style, join conditions and ordinary filters sit together in WHERE, and nothing separates them visually:
SELECT c.name, o.id AS order_id, o.order_date
FROM customers AS c, orders AS o
WHERE o.customer_id = c.id
AND o.total > 30
ORDER BY o.order_date;Here o.customer_id = c.id is the join condition and o.total > 30 is a filter. The database does not care, but a human reader must work out which is which. This is the readability problem the explicit syntax solves: ON holds the relationship and WHERE holds the filters.
For inner joins, moving a condition between ON and WHERE does not change the result. That is why the translation is safe. It stops being safe with outer joins, covered in a later chapter.
Translating comma style to INNER JOIN
Use this procedure when you modernize a query:
- Keep the first table in
FROM. - For each further table, add an
INNER JOINclause. - Move the
WHEREcondition that links that table to a table already listed into itsONclause. - Leave the remaining filters in
WHERE. - Run both versions and compare row counts.
Applied to the four-table query:
SELECT c.name AS customer, o.id AS order_id, p.name AS product, i.quantity
FROM customers AS c
INNER JOIN orders AS o ON o.customer_id = c.id
INNER JOIN order_items AS i ON i.order_id = o.id
INNER JOIN products AS p ON p.id = i.product_id;Order matters in one detail. An ON clause can only refer to tables that appear earlier in the FROM list. If the original FROM order was unusual, reorder the tables so each ON condition refers only to tables already introduced.
Precedence trap when mixing styles
Do not mix commas and explicit joins in one FROM clause. In standard SQL, JOIN binds more tightly than a comma. Consider:
SELECT c.name, o.id
FROM customers AS c, orders AS o
INNER JOIN order_items AS i ON i.order_id = c.id;The ON clause refers to c, but the explicit join happens between o and i first. c is not visible there. PostgreSQL rejects this with an error saying it cannot reference c in the JOIN condition. MySQL gives a similar "unknown column" error. The fix is to use one style throughout.
Why the old style persists
Early SQL standards (SQL-86 and SQL-89) had only the comma form. The JOIN ... ON syntax came with SQL-92. Legacy code, older tutorials, and some generated SQL still use commas. A few legacy Oracle queries also use proprietary outer-join markers such as (+). You may meet those, but they are not standard and are outside this chapter.
Comparing the two styles
| Aspect | Comma style | Explicit INNER JOIN |
|---|---|---|
| Join condition location | `WHERE` | `ON` |
| Missing condition | Silent cross join | Usually a syntax error |
| Separates join from filter | No | Yes |
| Result for inner joins | Same | Same |
| Typical performance | Same plan | Same plan |
| Extends to outer joins | No (standard form) | Yes |
Debugging an inherited comma-style query
When a legacy query returns too many rows, check these in order:
- Count the tables in
FROMand the join conditions inWHERE. A connected chain needs at least one fewer condition than tables. - Look for conditions joined by
OR.WHERE a.id = b.id OR a.x = 5can make the join apply only on one branch and revives a cross join for the rest. - Check that every alias in
WHEREmatches an alias inFROM. A typo likeo.customer_id = o.idcompiles but ties the table to itself. - Run
SELECT COUNT(*)on the query, then compare with the product of the table sizes. If the count equals that product, no join condition took effect.
Exercises
Exercise
A query reads FROM customers AS c, orders AS o WHERE o.total > 30. The tables have 3 and 4 rows, and 2 orders have a total above 30. How many rows does it return, and why?
Show answer
It returns 6 rows. There is no join condition, so each of the 3 customers pairs with each of the 2 orders that pass the filter (3 × 2 = 6). The fix is to add AND o.customer_id = c.id.
Exercise
Rewrite this query with INNER JOIN: SELECT e.name, m.name FROM employees e, employees m WHERE m.id = e.manager_id AND e.name <> 'Root';
Show answer
SELECT e.name, m.name FROM employees AS e INNER JOIN employees AS m ON m.id = e.manager_id WHERE e.name <> 'Root'; The linking condition moves to ON and the filter stays in WHERE.