Interviewers love CROSS JOIN because one tiny keyword can quietly turn two small tables into a huge result set.
Question: What does CROSS JOIN do in SQL?
Answer: A CROSS JOIN returns every possible pair of rows between two tables. If the first table has 3 rows and the second has 4 rows, the result has 12 rows. It does not match rows by a key; it simply combines everything with everything.
Interview-Ready Answer: I use CROSS JOIN when I want a Cartesian product, meaning every row from table A is paired with every row from table B. So if I have 10 rows and 20 rows, I should expect 200 output rows. I would choose it intentionally for things like generating combinations, but I would avoid it in normal joins because it can blow up row counts very quickly.
CROSS JOIN is the SQL way to say Cartesian product. A Cartesian product means every row on the left is combined with every row on the right. There is no matching condition, no ON clause, and no filtering by key.
If table A has m rows and table B has n rows, the output has m × n rows. That is the mental model to remember under pressure.
Because it multiplies rows, use it only when the multiplication is the point. If you are trying to match records by customer id or order id, you want an INNER JOIN or LEFT JOIN, not a cross join.
| Join type | What it does | Output size |
|---|---|---|
| CROSS JOIN | All pairs | m × n |
| INNER JOIN | Matches by condition | Only matching rows |
| LEFT JOIN | All left rows plus matches | At least left size |
| Comma FROM list | Old-style cross join | m × n |
In many databases, FROM a, b is equivalent to FROM a CROSS JOIN b, but the explicit keyword is safer because it makes intent obvious. Interviewers like that distinction because it shows you understand both syntax and risk.
Time complexity is effectively O(m × n) because the engine must produce every pair. The main cost is not just CPU; it is the sheer number of rows that may need memory, sorting, network transfer, or disk spill. A 1,000-row table crossed with another 1,000-row table makes 1,000,000 rows; 10,000 by 10,000 makes 100,000,000 rows, which is often too big for an interactive query.
Memory rule: think pairing engine, not matching engine. If you need to match, cross join is the wrong tool. If you need every combination, it is exactly the right tool.
Imagine a checkout service in an e-commerce app that needs to show every shipping option for every package type in a cart. The product team wants a grid like standard, express, and pickup for each package size. A clean CROSS JOIN can generate those combinations from two small lookup tables.
Now for the bug story: an engineer meant to join orders to customers by customer_id, but accidentally wrote a cross join. The query returned millions of rows instead of thousands, the API timed out, and the database CPU spiked. In logs, you would see huge result sets, slow sorts, and maybe temp-file spill messages; users would see duplicate options, slow page loads, or a spinner that never finishes.
The lesson is simple: CROSS JOIN is powerful when you want combinations, but dangerous when you really wanted matching. In production, that difference can be the gap between a neat pricing grid and an outage.
-- Example 1: intentional CROSS JOIN to build every color-size combination.
WITH colors(color) AS (
VALUES ('Red'), ('Green'), ('Blue')
),
sizes(size) AS (
VALUES ('S'), ('M')
)
SELECT
c.color,
s.size
FROM colors AS c
CROSS JOIN sizes AS s
ORDER BY c.color, s.size;
-- Example 2: edge case. If one input is empty, the result is empty too.
WITH colors(color) AS (
VALUES ('Red'), ('Green')
),
empty_discounts(discount) AS (
SELECT '10%'
WHERE 1 = 0
)
SELECT
c.color,
d.discount
FROM colors AS c
CROSS JOIN empty_discounts AS d;Follow-up & Tricky Questions:
CROSS JOIN on purpose? When you need every combination, such as generating a matrix of dates, colors, sizes, or test cases.FROM a, b the same as CROSS JOIN? In most SQL engines, yes in effect: it produces the Cartesian product. The explicit CROSS JOIN is clearer and less error-prone.CROSS JOIN keep unmatched rows? No. That is what outer joins do. A cross join keeps all combinations, which is a different idea entirely.ON with CROSS JOIN? No in standard SQL; if you need conditions, use a regular join or put the filter in WHERE after the cross product, knowing that it still starts from all pairs.Common Mistakes:
INNER JOIN or LEFT JOIN with the right ON clause.CROSS JOIN on large tables. Correction: estimate the output first; if the product is huge, pre-filter or rethink the query.Memory Hook: Think of a wardrobe: every shirt gets tried with every pair of pants. If you have 3 shirts and 4 pants, you get 12 outfits. That is CROSS JOIN.
Cheat Sheet:
CROSS JOIN = Cartesian product.ON match condition.Practice Tasks:
CROSS JOIN.INNER JOIN using a matching key.