VillageSQL is a drop-in replacement for MySQL with extensions.
All examples on this page work on VillageSQL. Install Now →
CROSS JOIN pairs every row of one table with every row of another. You
write no ON condition, because nothing is being matched. It is the join you
ask for when you want every combination, and the one you get by accident when
you leave an ON clause out.
Open the client with mysql -u root -p sakila before you start.
How CROSS JOIN works
COLUMN_LIST with the columns you want, and FIRST_TABLE and
SECOND_TABLE with the tables to combine. The result holds 1 row
for every possible pairing, so its size is the first table’s row count
multiplied by the second’s. 6 rows crossed with 2 rows give 12.
Examples
1) Every combination
Sakila has 6 languages and 2 stores.2) The comma is the older spelling
FROM is a cross join. You will
meet it in older SQL, often with the matching condition down in WHERE, which
works and hides what kind of join you are reading.
A comma binds more loosely than JOIN, so an ON clause cannot reach a table
on the other side of a comma. The 2 spellings do appear together in real
queries, and this is what goes wrong when they do.
CROSS JOIN when you mean one, and keep to a single spelling per query.
The keyword does not enforce the rule either. MySQL accepts an ON clause on a
CROSS JOIN and then behaves as an INNER JOIN, so the word in the query is
not proof of the join you got.
ON
clause, not the keyword, to know what a join does.
The number gets large quickly
12 rows is harmless. The same operation on the tables you usually query is not.INNER JOIN with no ON clause and treats it as this. So when a
join returns a suspiciously round and suspiciously large number, a missing ON
is the first thing to check. The INNER JOIN lesson
shows that accident.
Summary
- Use
CROSS JOINto pair every row of one table with every row of another. - Write no
ONclause, because nothing is being matched. - Expect MySQL to accept
CROSS JOIN ... ONanyway, and to treat it as an inner join. - Multiply the 2 row counts to know the size before you run it.
- Read a comma between tables in
FROMas a cross join, and keep to 1 spelling per query. - Suspect a missing
ONwhen a join returns a round, large number of rows.
See also
- How joins work — where this sits among the other kinds
- INNER JOIN — the join that matches rows instead
- Table aliases — the short names these queries use
- MySQL JOIN performance and common mistakes — the accident, seen from the other end

