Tier 2 · Core SQL

CROSS JOIN in SQL

By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17

Before this INNER JOIN

What is CROSS JOIN in SQL?

CROSS JOIN returns every possible combination of rows from two tables.

You're building a billing report template. You need one row for every user paired with every billing period — even if the user has no activity in that period yet. There's no shared ID to join on. You don't want matches; you want all combinations. That's CROSS JOIN.

With INNER JOIN and LEFT JOIN, the ON clause defines which rows belong together. CROSS JOIN has no ON clause. Every row in the left table pairs with every row in the right table — the full cartesian product.

How do you write a CROSS JOIN in SQL?

Here's the user-period grid:

SELECT u.id AS user_id, p.name AS period_name
FROM users u
CROSS JOIN periods p

If users has 50 rows and periods has 12 rows, this returns 600 rows — every user paired with every period. That's the complete grid.

Can you filter a CROSS JOIN with WHERE?

You can filter the cross product with WHERE:

SELECT u.id AS user_id, u.name AS user_name, p.name AS period_name
FROM users u
CROSS JOIN periods p
WHERE u.country = 'US'

Only US users are included, but each US user still gets paired with every period. The filter narrows which rows enter the cross product, not how the pairing works.

You can also combine CROSS JOIN with a regular JOIN in the same query:

SELECT e.name AS employee_name, d.name AS department_name
FROM employees e
CROSS JOIN departments d

Every employee paired with every department — not just their own. Useful for generating comparison matrices, testing coverage, or building template rows for reporting.

Why is an unfiltered CROSS JOIN dangerous?

The one thing that trips people up: accidentally running an unfiltered CROSS JOIN on large tables.

CROSS JOIN scales multiplicatively. Two tables with 10,000 rows each produce 100,000,000 rows. That's not a slow query — it's a query that may never finish, or one that takes down a database. Always know the row counts before running a CROSS JOIN without a filter, and add WHERE conditions to limit the output to what you actually need.

Practice CROSS JOIN in SQL

Practice · easy analytics · Streamhub

Streamhub's analytics team is building a quarterly reporting template and needs a complete grid of every user across every reporting period — one row per user-period combination, regardless of whether any data appears for that pairing.

Write a query to return the user ID and period name for every possible combination.

Assumptions:

  • The users table contains every account on the platform.
  • The periods table contains the calendar windows used to bucket metrics (Q1, Q2, etc.).
  • The output's row count is the product of the two tables' row counts (every user paired with every period).

Output:

  • One row per user-period combination, with columns user_id and period_name.
Schema · analytics5 tables? = nullable
users
idinteger
nametext
emailtext
countrytext
plantext
signed_up_attimestamptz
is_activeboolean
conversions
idinteger
user_id?integer
converted_attimestamptz
plantext
amountnumeric
sessions
idinteger
user_id?integer
started_attimestamptz
ended_at?timestamptz
event_countinteger
events
idinteger
user_id?integer
session_id?integer
event_typetext
occurred_attimestamptz
properties?jsonb
periods
idinteger
nametext
start_monthinteger
end_monthinteger

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution

The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.

See the full worked solution

9 CROSS JOIN practice problems

Start learning to practice all 9 CROSS JOIN problems, with instant grading and mastery tracking.

Common questions about CROSS JOIN

How many rows does a CROSS JOIN produce?

The two row counts multiplied together. Seventy customers crossed with sixty-three products gives four thousand four hundred and ten rows. That growth is why an unfiltered cross join on two large tables is something to check before you run it, not after.

Is writing two tables separated by a comma the same as CROSS JOIN?

Yes, they produce the same rows. The spelled-out form is worth preferring because a comma looks like a typo, and a reader cannot tell whether you meant to cross the tables or forgot to write a join condition.

Does a CROSS JOIN take an ON clause?

No, and adding one is a syntax error. A cross join pairs every row with every other row, so there is no condition to state. The moment you want a condition you want one of the other join types instead.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.