Understanding SQL CROSS JOIN with Examples
Estimated study time: 7 minutes. Every row from one table paired with every row from another.
A CROSS JOIN returns the Cartesian product of two tables — every row from the first table is combined with every row from the second table. Unlike other joins, it doesn't need a join condition.
Basic Syntax
SELECT * FROM TableA CROSS JOIN TableB;
Example
SELECT Colors.ColorName, Sizes.SizeLabel FROM Colors CROSS JOIN Sizes;
If Colors has 3 rows and Sizes has 4 rows, this query returns 12 rows — one row for every possible color-size combination. That kind of combination generator is exactly what CROSS JOIN is built for, such as producing every variant of a product.
Old-Style Syntax
A comma-separated FROM clause with no WHERE condition produces the same result as an explicit CROSS JOIN, though the explicit syntax is preferred because it makes the intent clear.
SELECT * FROM Colors, Sizes;
CROSS JOIN with a WHERE Clause
Adding a WHERE clause after a CROSS JOIN effectively turns it into an INNER JOIN, since the filter removes the unmatched combinations.
SELECT * FROM Employees CROSS JOIN Departments WHERE Employees.DeptID = Departments.DeptID;
When to Use CROSS JOIN
- Generating all possible combinations, such as product variants or a calendar of dates crossed with store locations.
- Building test data sets that need every combination of a few small lookup tables.
- Pairing a table with a single-row table of constants to broadcast a value across every row.
How the Result Is Built, Row by Row
A CROSS JOIN works like two nested loops: for each row of the left table, the database walks through every row of the right table and outputs the pair. With two small tables the process is easy to follow.
| sizes.size | crusts.crust |
|---|---|
| Small | Thin |
| Small | Cheese burst |
| Medium | Thin |
| Medium | Cheese burst |
| Large | Thin |
| Large | Cheese burst |
Three sizes multiplied by two crusts give six rows. Adding a third table with four toppings would give 24 rows, because the multiplication continues with every extra table.
In MySQL specifically, CROSS JOIN, JOIN and INNER JOIN are treated as the same operation. Writing CROSS JOIN ... ON is therefore accepted and behaves like an inner join, although most style guides keep CROSS JOIN for joins without any condition so readers know the product is intentional.
Worked Example: Finding Days With No Sales
A common reporting problem is that missing data never appears: if a store sold nothing on a day, the orders table simply has no row for it. Crossing every store with every date creates the full grid, and a LEFT JOIN then fills in actual sales.
WITH RECURSIVE days AS (
SELECT CURDATE() - INTERVAL 6 DAY AS d
UNION ALL
SELECT d + INTERVAL 1 DAY FROM days WHERE d < CURDATE()
)
SELECT s.store_name,
days.d AS sale_day,
COALESCE(SUM(o.amount), 0) AS revenue
FROM stores s
CROSS JOIN days
LEFT JOIN orders o
ON o.store_id = s.store_id
AND o.order_date = days.d
GROUP BY s.store_name, days.d
ORDER BY s.store_name, sale_day;
The recursive CTE (available in MySQL 8 and later) generates the last seven dates. Every store now has seven rows, and days without orders show a revenue of 0 instead of disappearing, which makes charts and averages correct.
Other Practical Patterns
- Broadcasting a single value: crossing a table with a one-row subquery attaches the same total to every row, which helps calculate percentages.
SELECT e.emp_name, e.salary,
ROUND(100 * e.salary / t.total, 2) AS pct_of_payroll
FROM employees e
CROSS JOIN (SELECT SUM(salary) AS total FROM employees) t;
- Seeding test data: crossing a list of user types with a list of plans quickly creates one test account per combination.
- Building a schedule template: crossing employees with shift slots gives a blank roster that a manager fills in later.
Staying Safe With Large Tables
Because the output grows by multiplication, always estimate the size before running a cross join on real data:
SELECT (SELECT COUNT(*) FROM stores) * (SELECT COUNT(*) FROM days_table) AS expected_rows;
- Filter each side first, using subqueries or CTEs, so only the rows you need are multiplied.
- Add
LIMITwhile exploring the result in a client tool. - If a query using the old comma syntax suddenly returns millions of rows, check for a missing WHERE condition; that is the classic accidental Cartesian product.
- Prefer an explicit
INNER JOIN ... ONwhenever you actually have a matching condition. It states intent better than a cross join followed by a filter.