Lesson 6 of 15 · 15 min

CROSS JOIN: every combination

By the end of this lesson you can use CROSS JOIN to build a complete grid (every warehouse × every carrier, every warehouse × every month), fill it with LEFT JOIN so missing combinations show 0, and spot an accidental cross join.

Every join you have written so far had a condition that decided which pairs to keep. A CROSS JOIN has no condition: it keeps every pair. Each row of the first table is combined with each row of the second, so the result has

rows(A) × rows(B) rows.

This result is called the Cartesian product of the two tables. With 6 warehouses and 8 carriers you get 48 rows: every warehouse paired with every carrier. That sounds useless until you need exactly that list.

Included with a Formula Quest membership

This lesson, its 4 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.

Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons