MySQL
SQL Joins
Comma Separated Joins
Join On Syntax
Database Management

What's the difference between comma separated joins and join on syntax in MySQL?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In MySQL, comma-separated joins and explicit JOIN ... ON syntax can produce the same result for simple inner joins, but they are not equally clear or equally flexible. The older comma style hides join conditions inside the WHERE clause, while explicit joins state the relationship directly next to each table. In modern SQL, JOIN ... ON is almost always the better choice.

Comma-Separated Join Syntax

The older style lists multiple tables in FROM and then puts the matching condition in WHERE.

sql
SELECT users.id, users.name, orders.total
FROM users, orders
WHERE users.id = orders.user_id;

For a basic inner join, this works. Conceptually, the database forms combinations of rows and then filters them down to matching pairs.

The main problem is not that the result is wrong. The problem is that join logic and filter logic are mixed together in one place.

Explicit JOIN ... ON Syntax

The modern equivalent expresses the relationship directly:

sql
SELECT users.id, users.name, orders.total
FROM users
JOIN orders ON users.id = orders.user_id;

This is easier to read because the join relationship is attached to the joined table itself. Additional filtering can then stay in WHERE, where it belongs.

sql
1SELECT users.id, users.name, orders.total
2FROM users
3JOIN orders ON users.id = orders.user_id
4WHERE orders.total > 100;

This separation of concerns is one of the biggest reasons explicit joins are preferred.

They Are Equivalent Only for Simple Inner Joins

For inner joins, MySQL can usually optimize both forms similarly. That means the difference is usually about clarity and maintainability rather than raw correctness or performance.

However, the equivalence breaks down when outer joins enter the picture. LEFT JOIN, RIGHT JOIN, and other explicit join types cannot be expressed cleanly with comma-separated syntax.

sql
SELECT users.id, users.name, orders.total
FROM users
LEFT JOIN orders ON users.id = orders.user_id;

There is no equally clear comma-based form for that. This is one reason the old style is considered limited and outdated.

WHERE Versus ON Becomes Important

With explicit joins, placing a condition in ON versus WHERE can change the result, especially for outer joins.

sql
1SELECT users.id, orders.total
2FROM users
3LEFT JOIN orders ON users.id = orders.user_id
4    AND orders.total > 100;

This keeps users even when they have no matching order above 100.

But this version:

sql
1SELECT users.id, orders.total
2FROM users
3LEFT JOIN orders ON users.id = orders.user_id
4WHERE orders.total > 100;

filters after the join and effectively turns the outer join into something closer to an inner join for that condition. Comma-separated syntax makes this kind of reasoning harder because all conditions are already crowded into WHERE.

Accidental Cross Joins Are Easier to Miss

In comma-separated syntax, forgetting one join condition silently creates a Cartesian product.

sql
SELECT *
FROM users, orders;

That multiplies every user row by every order row. Explicit joins make missing relationships more obvious because every joined table usually appears with an ON clause.

Even when the database can still execute either style, the risk of human error is higher in the comma form.

Use Explicit Joins for Maintainability

In small queries, the difference can look cosmetic. In multi-table reporting queries, the difference becomes significant. Explicit joins help developers scan the query and answer two separate questions:

  • how are tables related
  • which rows should be filtered afterward

That separation becomes more valuable as the query grows.

Common Pitfalls

  • Treating comma-separated syntax as equivalent to explicit joins in all cases instead of only simple inner joins.
  • Mixing join conditions and filters together until the query becomes hard to reason about.
  • Forgetting a join condition in comma syntax and creating an accidental Cartesian product.
  • Misplacing outer-join conditions in WHERE instead of ON.
  • Sticking to the older style in new code for no real benefit.

Summary

  • Comma-separated joins and JOIN ... ON can match for simple inner joins, but the explicit form is clearer.
  • 'JOIN ... ON keeps relationship logic separate from filtering logic.'
  • Outer joins and more advanced query shapes strongly favor explicit join syntax.
  • Explicit joins reduce the chance of accidental Cartesian products.
  • In modern MySQL code, JOIN ... ON should usually be the default style.

Course illustration
Course illustration

All Rights Reserved.