CROSS JOIN in SQL
CROSS JOIN combines every row from one table with every row from another, producing a result with no join condition. The output contains one row for every possible pairing between the two tables.
Before this INNER JOIN
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.
Here's the user-period grid:
SELECT u.id AS user_id, p.name AS period_name
FROM users u
CROSS JOIN periods pIf users has 50 rows and periods has 12 rows, this returns 600 rows — every user paired with every period. That's the complete grid.
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:
Every employee paired with every department — not just their own. Useful for generating comparison matrices, testing coverage, or building template rows for reporting.
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.
9 CROSS JOIN practice problems
Write a query to return the user ID and period name for every possible combination.
Write a query to return the employee name and department name for every combination.
Write a query to return the product name and category name for every possible combination.
Write a query to return the user ID and period name for every pro-user-quarter combination.
Write a query to return the user ID, user name, and period name for every US-user-quarter combination.
Write a query to return the employee name and department name for every Engineering-employee × department combination.
Write a query to return each premium product paired with every category, returning the product name, price, and category name.
Write a query that produces the cross-product of `products` and `categories` and then narrows it to the actual assignments.
Write a query to return the user ID, session ID, and period name for every (session × period) combination.
These problems are part of the CROSS JOIN lesson in SQLMaxx, with instant grading and a worked solution on each.
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.
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.
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.
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.
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.
Practice, feedback, mastery, review. That's the loop that turns reading into real skill.
Start freeNo account, no credit card. Start solving in under a minute.