Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What does a CROSS JOIN produce, and give one legitimate use. How does an accidental cartesian product usually appear in a query?

A CROSS JOIN returns every combination of a row from the left input with a row from the right input, so m rows times n rows gives m x n rows, with no join condition at all. A legitimate use is building a grid of every store and every date, or every combination of a few parameters, which we then left join to real data to fill gaps. Accidental cartesian products usually come from a missing join condition, most often in the old comma syntax FROM a, b WHERE ... where one predicate was forgotten.

Why this question is asked

Most joins in our code match rows by a key, so the CROSS JOIN is easy to forget about, until a report shows every order a thousand times or the database fills its disk. Interviewers ask about it because the same operation is both a useful tool and the most common cause of runaway queries, and a good answer shows we can tell the two apart. In this answer, we will recap what a CROSS JOIN does with a tiny example, look at a legitimate use, see how an accidental one sneaks into a query, and learn how to spot one in a plan.

1A Quick Recap of What CROSS JOIN Produces

A join combines rows from two tables. An inner join keeps the pairs that satisfy the ON condition. A CROSS JOIN has no condition, so it keeps every pair. The result is often called a cartesian product, after the mathematical product of two sets. If the left side has 3 rows and the right side has 2, we get 6 rows.

SQL
CREATE TABLE sizes (size text); INSERT INTO sizes VALUES ('S'), ('M'), ('L');
CREATE TABLE colours(colour text); INSERT INTO colours VALUES ('red'), ('blue');
SELECT s.size, c.colour FROM sizes s CROSS JOIN colours c ORDER BY 1, 2;
-- L | blue
-- L | red
-- M | blue
-- M | red
-- S | blue
-- S | red -- 3 x 2 = 6 rows

Notice that the count multiplies, it does not add. That is harmless for 3 and 2, but 50,000 rows crossed with 4,000 rows is 200 million rows, and the database does not warn us; it simply produces them. Writing FROM sizes, colours with no WHERE clause, or an INNER JOIN with ON true, gives exactly the same result.

2A Legitimate Use: Building the Full Grid

The classic good use is a report that must show a row for every combination, even when there is no data for some of them. Suppose a sales report must show every store for every day of the week, with zero where nothing was sold. If we start from the sales table, days without sales simply do not exist, so they cannot appear. We first build the grid of all store and date pairs with a CROSS JOIN, then LEFT JOIN the real data onto it.

SQL
SELECT st.id AS store_id, d.day::date AS day, COALESCE(SUM(sa.amount), 0) AS total
FROM stores st
CROSS JOIN generate_series(DATE '2026-03-02', DATE '2026-03-08', INTERVAL '1 day') AS d(day)
LEFT JOIN sales sa ON sa.store_id = st.id AND sa.sold_on = d.day::date
GROUP BY st.id, d.day
ORDER BY st.id, d.day; -- 40 stores x 7 days = 280 rows, zeros included
Step throughFilling gaps with a CROSS JOIN
  1. Take all stores40 rows
  2. CROSS JOIN all days7 rows -> 280 store-day pairs
  3. LEFT JOIN sales on store and daymissing days keep NULL
  4. COALESCE the sum to 0every pair appears once

4 steps in order. Play them, or step through them one at a time.

Other good uses follow the same shape: combining a small set of parameters, such as every currency with every pricing tier to produce a price list, or every test case with every configuration. What they have in common is that both sides are small and known, and the multiplication is the point.

3How an Accidental Cartesian Product Appears

The accidental kind almost always comes from the old comma syntax, where the tables are listed in FROM and the join conditions sit in WHERE alongside the filters. Because conditions and filters are mixed, it is easy to add a table and forget its condition, and the query still runs.

SQL
-- Intended: orders with their customer and their region
SELECT o.id, c.name, r.name
FROM orders o, customers c, regions r
WHERE o.customer_id = c.id
AND o.created_at > DATE '2026-03-01'; -- forgot: AND c.region_id = r.id
-- every order now appears once per region: 120,000 orders x 300 regions = 36,000,000 rows

The query looks almost right when we read it, which is why the mistake survives review. The explicit syntax makes it much harder to make: with FROM orders o JOIN customers c ON ... JOIN regions r ON ..., every joined table has its condition right next to it, and a JOIN without ON is a syntax error in PostgreSQL and in standard SQL. So a simple team rule, use explicit JOIN ... ON and write CROSS JOIN only on purpose, removes most of these bugs. Another common source is a join on a column that is not unique on either side, such as joining two tables only on a status code, which behaves like a partial cartesian product.

4Spotting It in a Query Plan

When we suspect a cartesian product, EXPLAIN tells us quickly. In PostgreSQL, a cross product shows as a Nested Loop node with no Join Filter and no Hash Cond or Merge Cond, often with a Materialize node on the inner side so that the inner rows can be replayed for every outer row. The row estimate on that node is the product of its inputs, and it is usually huge.

Java
EXPLAIN SELECT o.id, r.name FROM orders o, regions r WHERE o.created_at > DATE '2026-03-01';
-- Nested Loop (rows=36000000) <- no join condition listed under it
-- -> Seq Scan on orders o (rows=120000)
-- Filter: (created_at > '2026-03-01'::date)
-- -> Materialize (rows=300)
-- -> Seq Scan on regions r (rows=300)

So two signals together, a join node without any condition and an estimate that equals the product of the two inputs, mean a cartesian product. If the query was meant to have one, fine. If not, the missing predicate is the bug. In MySQL, EXPLAIN shows a similar picture, with the join buffer note on the second table and rows multiplying across the plan.

5Follow-up questions

Is CROSS JOIN the same as an INNER JOIN with ON 1 = 1?

Yes, the results are identical, and so are the comma syntax without a condition and INNER JOIN ... ON true. The difference is intent. Writing CROSS JOIN tells the reader that the multiplication is deliberate, so a reviewer does not go looking for a missing condition. That is why we prefer it over the other spellings.

Can a CROSS JOIN be fast?

When one side is tiny, it is perfectly fast: crossing a million rows with a one-row table of parameters, such as SELECT ... FROM orders CROSS JOIN (SELECT now() - interval '30 days' AS cutoff) p, costs nothing extra and is a common way to compute a value once and use it in many places. The cost is only ever the size of the product, so we just need to know both sizes.

How do you test for accidental row multiplication?

A cheap check is to compare the row count of the result with the row count of the main table the report is about. If a report about orders returns more rows than there are orders in the period, some join multiplied them. In tests, we can assert that the number of distinct order ids equals the number of rows, and in reviews we can ask for the expected row count of each join.

6Conclusion

In this answer, we saw that a CROSS JOIN pairs every row with every other row, so the counts multiply. We used it on purpose to build a grid of stores and days and then left joined real sales to fill gaps with zeros. We saw how the comma syntax hides a forgotten join condition and turns a normal report into millions of rows, why explicit JOIN ... ON prevents that, and how a nested loop without a join condition and a multiplied row estimate reveal the problem in a plan.

What to say in the interviewAbout 90 seconds aloud

Open with

A CROSS JOIN returns every combination of rows from the two inputs, with no condition, so 3 rows and 2 rows give 6, and 50,000 by 4,000 gives 200 million.

Then, if they want more

It is the same as a comma join without a WHERE predicate, or an inner join on true; writing CROSS JOIN just makes the intent clear. A legitimate use is building a complete grid before adding data.

For example, a daily sales report that must show zero for days without sales: I cross join stores with a generate_series of dates to get every store-day pair, then left join the sales onto that grid and coalesce the sum to zero. Other uses are small parameter combinations, like every currency with every price tier.

Accidental cartesian products almost always come from a missing join condition, typically with the old comma syntax, FROM a, b, c WHERE ..., where someone adds a table and forgets its predicate. The query runs, the result just has every row repeated. Explicit JOIN ...

ON syntax prevents most of these, because the condition sits next to the table and a JOIN without ON does not parse. In a plan I would recognise it as a nested loop with no join filter or hash condition, often over a Materialize node, with a row estimate equal to the product of both sides.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does a CROSS JOIN produce, and give one legitimate use. How does an accidental...": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/5f98789012/