CROSS JOIN
A join that produces all combinations (Cartesian product) of every row from two tables. Because no join condition is specified, it returns m × n rows — where m is the number of rows in the left table and n is the number of rows in the right table.
Syntax
-- Use CROSS JOIN to generate all combinations. SELECT column_name, ... FROM table1 CROSS JOIN table2; -- A comma-separated FROM clause produces the same result (older style). SELECT column_name, ... FROM table1, table2;
Syntax list
| Syntax | Description |
|---|---|
| CROSS JOIN | Returns all combinations (Cartesian product) of every row from two tables. |
| FROM t1, t2 | Listing multiple tables separated by commas produces the same result as CROSS JOIN when no WHERE clause is used. |
Sample code
-- Generate all combinations of the products table and the colors table.
SELECT
s.product_name,
c.color
FROM products AS s
CROSS JOIN colors AS c;
-- Combine a days-of-week table and a time-slot table to build a shift schedule grid.
SELECT
d.day_of_week,
t.time_slot
FROM days AS d
CROSS JOIN time_slots AS t
ORDER BY d.day_id, t.slot_id;
Result
-- products (3 rows) × colors (4 rows) = 12 rows returned. product_name | color -------------+------- T-Shirt | Red T-Shirt | Blue T-Shirt | Green T-Shirt | White Hoodie | Red Hoodie | Blue Hoodie | Green Hoodie | White Jacket | Red Jacket | Blue Jacket | Green Jacket | White
Syntax by database
The CROSS JOIN syntax is supported across all major databases.
-- Common to MySQL, PostgreSQL, SQL Server, Oracle, and SQLite SELECT s.product_name, c.color FROM products AS s CROSS JOIN colors AS c;
In Oracle, the older comma-separated syntax (FROM table1, table2) is also widely used. It behaves identically to CROSS JOIN, but using CROSS JOIN explicitly is recommended for clarity.
Overview
CROSS JOIN is a join with no join condition — it pairs every row in the left table with every row in the right table. The number of result rows equals the left table row count multiplied by the right table row count.
Common use cases include generating calendars, building shift schedule grids, and listing all size-and-color combinations — any situation where you intentionally need every possible pairing. Using CROSS JOIN on large tables can cause the row count to explode, so check the expected number of rows before running the query.
For the basics of joins, see INNER JOIN. To join a table to itself, see SELF JOIN.
If you find any errors or copyright issues, please contact us.