typestar

CROSS JOIN in SQL

Every row on the left paired with every row on the right — on purpose, this time.

SELECT
    c.name,
    p.sku,
    p.price
FROM customers AS c
CROSS JOIN products AS p
ORDER BY c.name, p.sku;

How it works

  1. A CROSS JOIN has no ON clause: the result is n * m rows.
  2. It is the honest way to build a grid of every combination.
  3. A JOIN whose ON you forgot is the same thing by accident.

Keywords and builtins used here

The run, in numbers

Lines
7
Characters to type
98
Tokens
31
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 22 seconds.

Type this snippet

Step 5 of 9 in Joins, step 13 of 22 in Joins & aggregation.

← Previous Next →