sqlcmd.net validated sql reference
advanced joining MySQL MariaDB SQL Server PostgreSQL SQLite

Generate All Row Combinations With CROSS JOIN

Produce every possible pairing of rows from two tables using `CROSS JOIN`.

Docker-validated Not currently validation-green

Generate every color and size combination for a product grid

2 colors × 3 sizes = 6 rows. Every color is paired with every size. Unlike INNER JOIN, no matching key is needed — CROSS JOIN intentionally produces all combinations. Adding a WHERE clause after a CROSS JOIN filters the Cartesian product and can simulate an INNER JOIN.

Rows loaded before the example query runs.
Setup
CREATE TABLE colors (color VARCHAR(20));

CREATE TABLE sizes (size VARCHAR(10));

INSERT INTO
  colors (color)
VALUES
  ('Red'),
  ('Blue');

INSERT INTO
  sizes (size)
VALUES
  ('S'),
  ('M'),
  ('L');
Shared across supported engines.
SQL
SELECT
  color,
  size
FROM
  colors
  CROSS JOIN sizes
ORDER BY
  color,
  size;
Returned rows for the shared example.
colorsize
BlueL
BlueM
BlueS
RedL
RedM
RedS

Output is identical across all engines.

Where this command helps.

  • generating every combination of two dimensions such as colors and sizes
  • pairing all items from one list with all items from another for comparison

What the command is doing.

A CROSS JOIN returns the Cartesian product of two tables — every row from the left table paired with every row from the right. A table with M rows crossed with a table with N rows produces M × N result rows. There is no ON condition because every combination is intentional. Common uses include generating test data, building grids, and pairing items like sizes with colors.