CROSS JOIN
CROSS JOIN pairs every row of one table with every row of another, with no matching condition at all. Three students and six courses make 18 rows. That's called the Cartesian product. On its own it sounds useless, but it's exactly what you need for a grid of every combination: every student in every month, every course for every student. A left join onto that grid then shows what's missing, like a month in which a student paid nothing. This lesson builds such a calendar, and then shows the cross join's dark side: the accidental one that turns 7 rows into 21.
In One Minute
FROM a CROSS JOIN breturns every combination: rows ofatimes rows ofb.- There's no
ON: nothing is matched, everything is paired. - Cross join a list (such as months from
generate_series) to make a grid, thenLEFT JOINthe facts onto it. - A forgotten join condition makes an accidental cross join, multiplying rows.
FROM a, bwithout aWHEREcondition is also a cross join.
The Basics
SELECT columns
FROM table_a
CROSS JOIN table_b;Every course, paired with Navika:
SELECT s.first_name, c.title
FROM students AS s
CROSS JOIN courses AS c
WHERE s.student_id = 103
ORDER BY c.course_id; first_name | title
------------+-------------------
Navika | Java Basics
Navika | Java OOP
Navika | Java Collections
Navika | SQL Basics
Navika | JavaScript Basics
Navika | Git Basics
(6 rows)All six courses, whether she takes them or not. Without the WHERE, every student would be paired with every course:
SELECT count(*) AS combinations
FROM students
CROSS JOIN courses; combinations
--------------
18
(1 row)3 students × 6 courses = 18. The size is always the product of the two row counts, which is why cross joins on big tables are dangerous: 10,000 × 10,000 is 100 million rows.
Building a Grid, Then Finding Gaps
A cross join builds the full grid; a left join then fills it with facts, and the empty cells are what's missing. Which students paid what in each month from October to December? PostgreSQL's generate_series makes the list of months (PostgreSQL only):
SELECT s.first_name,
m.month::date AS month,
coalesce(sum(p.amount_usd), 0.00) AS paid
FROM students AS s
CROSS JOIN generate_series(date '2025-10-01', date '2025-12-01', interval '1 month') AS m(month)
LEFT JOIN payments AS p
ON p.student_id = s.student_id
AND date_trunc('month', p.paid_on) = m.month
GROUP BY s.student_id, s.first_name, m.month
ORDER BY s.student_id, m.month; first_name | month | paid
------------+------------+--------
Srihitha | 2025-10-01 | 250.00
Srihitha | 2025-11-01 | 0.00
Srihitha | 2025-12-01 | 800.00
Advitha | 2025-10-01 | 0.00
Advitha | 2025-11-01 | 0.00
Advitha | 2025-12-01 | 0.00
Navika | 2025-10-01 | 0.00
Navika | 2025-11-01 | 225.00
Navika | 2025-12-01 | 225.00
(9 rows)Nine rows: 3 students × 3 months, including every month with nothing paid. A plain GROUP BY on payments would only show months that had payments, so Advitha wouldn't appear at all and Srihitha's empty November would be invisible. The grid makes the zeros visible, which is what a real report needs.
The same trick shows each student's enrollment status in every course: cross join students with courses, then left join enrollments and check whether a match was found (e.student_id IS NOT NULL).
The Accidental Cross Join
Forget the join condition, and you get a cross join without asking for one. The older comma style in FROM makes this easy:
SELECT count(*) AS rows
FROM students, payments; rows
------
21
(1 row)FROM students, payments with no WHERE linking them pairs every student with every payment: 3 × 7 = 21 rows, each payment repeated once per student. With the condition, the comma style is an inner join:
SELECT s.first_name, p.payment_id
FROM students AS s, payments AS p
WHERE p.student_id = s.student_id;That works, but the join condition is mixed in with the filters, and forgetting it is silent. With explicit JOIN ... ON, a missing ON is a syntax error instead. Joining Many Tables says more about this older style.
Common Mistakes
Cross joining large tables. Row counts multiply. Filter first, or make sure you really want every combination.
Comma joins without a condition. FROM a, b is a cross join until a WHERE connects them. Prefer JOIN ... ON.
Reporting from facts alone when gaps matter. Months with no payments don't appear unless you build the grid first.
Interview Corner
What is a CROSS JOIN?
A join with no condition that returns every combination of rows from the two tables, the Cartesian product. The row count is the product of the two tables' row counts.
When is a CROSS JOIN useful?
To build a grid of all combinations, such as every product for every store or every student for every month, which a LEFT JOIN then fills with real data so missing combinations show up as zeros or NULLs.
What happens if you forget the join condition with comma-separated tables?
You get an accidental cross join: every row of one table paired with every row of the other.
Practice
Challenge: Show every course for Navika (student 103) with a column enrolled that is true if she's enrolled and false if not, sorted by course_id.
Show solution
SELECT s.first_name, c.title, (e.student_id IS NOT NULL) AS enrolled
FROM students AS s
CROSS JOIN courses AS c
LEFT JOIN enrollments AS e
ON e.student_id = s.student_id
AND e.course_id = c.course_id
WHERE s.student_id = 103
ORDER BY c.course_id; first_name | title | enrolled
------------+-------------------+----------
Navika | Java Basics | f
Navika | Java OOP | f
Navika | Java Collections | f
Navika | SQL Basics | f
Navika | JavaScript Basics | t
Navika | Git Basics | f
(6 rows)The cross join makes all six pairs; the left join finds an enrollment only for JavaScript Basics. e.student_id IS NOT NULL turns "found a match" into true.
Key Takeaways
| You want to | Write |
|---|---|
| Every combination | FROM students CROSS JOIN courses |
| A list of months (PostgreSQL) | generate_series(date '2025-10-01', date '2025-12-01', interval '1 month') |
| Grid, then facts | FROM a CROSS JOIN b LEFT JOIN facts ON ... |
Was this tutorial helpful?