Sub topic 8
CROSS JOIN versus the Comma Syntax
This chapter assumes you know SELECT, INNER JOIN, LEFT JOIN, primary keys, and NULL handling. You should also understand that a join combines rows from two or more tables.
What a `CROSS JOIN` does
A CROSS JOIN returns every possible pair of rows from two tables. This result is called a Cartesian product.
If table A has 3 rows and table B has 2 rows, the result has:
3 × 2 = 6 rows
The query has no ON condition because it does not match related rows. It deliberately combines every row from one input with every row from the other input.
CREATE TABLE sizes (
size VARCHAR(2)
);
CREATE TABLE colors (
color VARCHAR(10)
);
INSERT INTO sizes (size) VALUES
('S'),
('M'),
('L');
INSERT INTO colors (color) VALUES
('red'),
('blue');
SELECT s.size, c.color
FROM sizes AS s
CROSS JOIN colors AS c
ORDER BY s.size, c.color;The result contains these six combinations:
| size | color |
|---|---|
| L | blue |
| L | red |
| M | blue |
| M | red |
| S | blue |
| S | red |
Aliases such as s and c are not required here, but they make the selected columns clear.
`CROSS JOIN` versus comma syntax
Older SQL often writes a Cartesian product by listing tables in the FROM clause separated by commas:
SELECT s.size, c.color
FROM sizes AS s, colors AS c;With no WHERE condition connecting the tables, this has the same meaning as:
SELECT s.size, c.color
FROM sizes AS s
CROSS JOIN colors AS c;The explicit CROSS JOIN is usually better because it states the intent. A reader can immediately see that all combinations are required.
The comma syntax becomes dangerous when a condition is accidentally omitted:
SELECT e.name, d.department_name
FROM employees AS e, departments AS d;This query pairs every employee with every department. It does not find each employee's actual department.
Some older queries use a WHERE clause to turn comma syntax into an inner join:
SELECT e.name, d.department_name
FROM employees AS e, departments AS d
WHERE e.department_id = d.id;This can return the expected matching rows, but explicit JOIN syntax is easier to review:
SELECT e.name, d.department_name
FROM employees AS e
INNER JOIN departments AS d
ON e.department_id = d.id;How to recognize an accidental Cartesian product
Check for these warning signs:
- The result has far more rows than expected.
- The
FROMclause lists multiple tables, but no join condition connects them. - A comma separates tables where an explicit
JOINwould be clearer. - The output repeats the same value from one table beside many unrelated values from another.
- The row count equals the product of the input row counts.
A Cartesian product is not always an error. It is useful when you need every combination, such as all store-product pairs or every date and category pair for a report.
Choosing the syntax
| Situation | Recommended syntax | Reason |
|---|---|---|
| Every possible combination is required | `CROSS JOIN` | Makes the Cartesian product explicit |
| Rows match through related keys | `INNER JOIN ... ON` | Shows the matching rule |
| Unmatched rows must remain | `LEFT JOIN ... ON` | Preserves rows from the left table |
| Legacy comma query needs maintenance | Replace it with explicit joins | Reduces the chance of a missing condition |
When you see comma syntax in existing SQL, do not assume it is wrong. First check whether its WHERE clause contains all intended relationships. Then consider rewriting it with explicit joins for clarity.
Exercise
Tables A and B contain 4 and 7 rows. How many rows does A CROSS JOIN B return?
Show answer
It returns 28 rows because every row in A pairs with every row in B: 4 × 7.