Reading history

SQL CROSS JOIN and Calendar Tables

4 min read 0 views

Learn how CROSS JOIN builds every combination of rows, how to make quick lists with VALUES, and how a calendar grid shows zero for months with no data.

A CROSS JOIN pairs every row of one table with every row of another. Used by accident it can produce millions of useless rows. Used on purpose it is very handy: it builds every size–colour combination for a shop, or every customer–month pair for a report so that months with no orders still show up as zero.

In this lesson you will learn

  • What a Cartesian product is and how many rows it makes
  • How to write a CROSS JOIN
  • How to build small lists with VALUES
  • How to fill in zero rows with a calendar and LEFT JOIN
  • How to avoid accidental cross joins

You should know joins and CTEs.

Every Row With Every Row

If one table has 3 rows and the other has 2, a cross join returns 3 × 2 = 6 rows. This is called the Cartesian product. There is no ON clause, because every combination is wanted.

sizescoloursSMLRedBlue×CROSS JOIN result: 3 × 2 = 6 rowsS · RedS · BlueM · RedM · BlueL · RedL · Blue
-- Every size with every colour: 3 x 2 = 6 rows
WITH sizes(size)   AS (VALUES ('S'), ('M'), ('L')),
     colours(colour) AS (VALUES ('Red'), ('Blue'))
SELECT size, colour
FROM sizes
CROSS JOIN colours
ORDER BY colour, size;

Output

+------+--------+
| size | colour |
+------+--------+
| L    | Blue   |
| M    | Blue   |
| S    | Blue   |
| L    | Red    |
| M    | Red    |
| S    | Red    |
+------+--------+
(6 rows)

The VALUES lists inside the CTEs act as tiny tables, so you do not need to create real ones for a quick list.

Filling Gaps With a Calendar

A normal GROUP BY only shows months that have orders. A report usually needs every month for every customer, with 0 where nothing happened. The trick has three steps:

  1. Build the full grid: all customers CROSS JOIN all months.
  2. LEFT JOIN the real orders onto that grid.
  3. Count with COUNT(o.id), which counts only matched orders and gives 0 for empty cells.
-- Monthly order count for every customer, including months with no orders
WITH months(m) AS (VALUES ('2026-01'), ('2026-02'), ('2026-03')),
     customers AS (SELECT DISTINCT customer FROM orders)
SELECT c.customer,
       mo.m                 AS month,
       COUNT(o.id)          AS orders_placed
FROM customers AS c
CROSS JOIN months AS mo
LEFT JOIN orders AS o
       ON o.customer = c.customer
      AND substr(o.order_date, 1, 7) = mo.m
GROUP BY c.customer, mo.m
ORDER BY c.customer, mo.m;

Output

+----------+---------+---------------+
| customer | month   | orders_placed |
+----------+---------+---------------+
| Joya     | 2026-01 | 0             |
| Joya     | 2026-02 | 1             |
| Joya     | 2026-03 | 1             |
| Karim    | 2026-01 | 1             |
| Karim    | 2026-02 | 1             |
| Karim    | 2026-03 | 1             |
| Rina     | 2026-01 | 1             |
| Rina     | 2026-02 | 1             |
| Rina     | 2026-03 | 0             |
| Tuhin    | 2026-01 | 0             |
| Tuhin    | 2026-02 | 0             |
| Tuhin    | 2026-03 | 1             |
+----------+---------+---------------+
(12 rows)

Tuhin placed no orders in January or February, yet both months appear with 0. COUNT(*) would have shown 1 for those rows, because the grid row itself exists. Count a column from the right-hand table instead.

For long date ranges, generate the calendar with a recursive CTE instead of typing every month. Our lesson on filling missing dates shows how.

Accidental Cross Joins

-- Old comma style with the join condition forgotten
SELECT s.name, c.title
FROM students AS s, courses AS c;          -- 8 x 4 = 32 rows, all mixed up

Listing tables with commas and forgetting the WHERE condition gives a cross join without warning. Two tables of 10,000 rows each would return 100 million rows. Always write joins with JOIN … ON, and use the words CROSS JOIN only when you really mean it, so readers know it was deliberate.

Common Mistakes

  1. Using COUNT(*) after the calendar join. It counts grid rows, so empty months show 1. Use COUNT(o.id).
  2. Putting the month filter in WHERE. A condition on the right table in WHERE turns the LEFT JOIN back into an inner join. Keep it in ON.
  3. Cross joining large tables. Check the row count you expect before running it.
  4. Forgetting DISTINCT when building the list of customers from an orders table.

Practice Exercises

Exercise 1. How many rows does students CROSS JOIN departments return with the sample data (8 students, 4 departments)?

Show answer

8 × 4 = 32 rows.

Exercise 2. Show every department with every course credit value (3 and 4), using VALUES.

Show answer
WITH credits(c) AS (VALUES (3), (4))
SELECT d.code, credits.c
FROM departments AS d
CROSS JOIN credits
ORDER BY d.code, credits.c;

4 departments × 2 values = 8 rows.

Frequently Asked Questions

What does CROSS JOIN do in SQL?

It returns every combination of rows from two tables, called the Cartesian product. If one table has m rows and the other has n rows, the result has m times n rows.

How do I show zero for months with no data in SQL?

Build a full grid of the groups and months with a CROSS JOIN, then LEFT JOIN the real data onto it and count a column from the data table. Months with no matching rows then show a count of zero.

Summary

  • CROSS JOIN returns m × n rows: every combination.
  • VALUES in a CTE makes a quick list without a real table.
  • Grid + LEFT JOIN + COUNT(column) fills in zero rows.
  • Avoid accidental cross joins by always writing JOIN … ON.

Next: SQL Auto-Increment IDs and Sequences.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *