Stop 06 · Joining TablesTopics · 6/10

CROSS JOIN

Part of the SQL Roadmap

In short

Sometimes you want every combination: every store with every month, every size with every colour. CROSS JOIN builds that grid. Use it on purpose, because by accident it can create millions of rows.

CROSS JOIN pairs every row of one table with every row of another, producing all combinations. It is rarely what you want by accident but useful for building grids.

The jaadu lays out a neat grid of steel thalis on a long dhaba table, placing one of each bread in every row and one of each curry bowl in every column.

Think of it like this

The thali combination board

A dhaba offers 3 breads (roti, naan, paratha) and 4 sabzis. The owner wants a board showing every possible plate. That's 3 × 4 = 12 combinations, whether or not anyone has ordered them yet. CROSS JOIN pairs every row of one table with every row of the other, exactly like that board.

Watch it happen

3 stores × 2 months = 6 rows

Pune
0
Nagpur
1
Nashik
2

1/4Three stores. The months table has October and November.

The big ideas

What you need to know

Every pair

Rows out = rows in A × rows in B. There's no ON condition at all.

Build a full grid

Cross join stores with months, then LEFT JOIN sales, so missing combinations show as 0.

Watch the size

10,000 customers × 5,000 products is 50 million rows. Do the multiplication before you run it.

Spot accidental ones

FROM a, b with no join condition is a hidden cross join. Sudden huge row counts are the clue.

See it in code

Walk through the code

sql
1SELECT2  s.store_name,3  m.month,4  COALESCE(SUM(o.amount), 0) AS revenue5FROM stores AS s6CROSS JOIN (VALUES (DATE '2026-09-01'),7                   (DATE '2026-10-01'),8                   (DATE '2026-11-01')) AS m(month)9LEFT JOIN orders AS o10  ON  o.store_id   = s.store_id11  AND o.order_date >= m.month12  AND o.order_date <  m.month + INTERVAL '1 month'13GROUP BY s.store_name, m.month14ORDER BY s.store_name, m.month;

1/3Every store paired with each of three months: a complete grid.

Revenue for every store in every month, with ₹0 where nothing sold. In MySQL, build the months with SELECT ... UNION ALL SELECT ... instead of VALUES.

Save yourself the pain

Common mistakes

Oops

FROM orders, customers WHERE o.amount > 500 with no join condition.

Do this instead

That's a cross join of both tables. Always write an explicit JOIN ... ON.

Oops

Running a CROSS JOIN on two big tables to 'see what happens'.

Do this instead

Multiply the row counts first. If it's in the millions, add LIMIT or rethink it.

Oops

Reporting monthly sales without a grid and missing zero months.

Do this instead

Months with no sales have no rows. Cross join a full calendar first, then LEFT JOIN the data.

Your mission

A no-gaps sales grid

~20 min

Quick check

sizes has 5 rows and colours has 8 rows. How many rows does sizes CROSS JOIN colours return?

Go deeper

  1. 1Cross join a list of stores with a list of months to build a full grid.
  2. 2Calculate the expected row count before running it on big tables.
  3. 3Recognise an accidental cross join when a join condition is missing.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge