Save Big on Cyber Monday! Up to 40% Off
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

What is CROSS JOIN in SQL?

What is a CROSS JOIN in SQL?

A CROSS JOIN is a type of database join that combines every row from one table with every row from another table. The result is a Cartesian product, containing all possible combinations of rows from both tables.

When to use the CROSS JOIN?

SQL CROSS JOIN is used to generate combinations of data, especially when exploring all potential relationships between two sets. They're also useful for creating test or sample datasets, since CROSS JOINs can produce large result sets with many combinations. In some cases, they help build pivot tables or matrix-style outputs.

Syntax of CROSS JOIN in SQL between two tables

The syntax for the CROSS JOIN is as follows:

SELECT      [column names]
FROM        [TableA]
CROSS JOIN  [TableB]
CROSS JOIN in SQL Server

SQL JOIN & SET operation comparison table

Feature / Join type CROSS JOIN INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN UNION
Definition Cartesian product of both tables Matches rows with matching keys All matches + unmatched from left table All matches + unmatched from right table All matches + unmatched from both sides Combines rows from 2 queries
Join condition Not required Required Required Required Required Not applicable
Null handling None; all rows paired Rows with no match are excluded Fills NULLs for unmatched right rows Fills NULLs for unmatched left rows Fills NULLs on both sides where no match Duplicate rows removed unless UNION ALL
Result size All combinations (large result sets) Only matched rows Matched + unmatched left Matched + unmatched right Matched + unmatched on both sides Rows stacked vertically
Use cases Generate all combinations Standard matching (e.g., FK-PK) Keep all left-side data Keep all right-side data Keep everything Merge results from two SELECTs
Typical example Product variants (e.g., sizes × colors) Orders and customers Customers and all their orders (even none) Orders and their customers (even unknown) Employees and departments (including unassigned) Combining SELECT results from different tables
SQL syntax FROM A CROSS JOIN B FROM A INNER JOIN B ON ... FROM A LEFT JOIN B ON ... FROM A RIGHT JOIN B ON ... FROM A FULL OUTER JOIN B ON ... SELECT ... UNION SELECT ...
Performance caution Risky on large tables Depends on index and join condition Can slow down with NULLs Similar to LEFT JOIN Often slower than INNER JOIN Watch for duplicate rows
Filtering with WHERE Often required to reduce size Common for filtering matched values Can refine unmatched results Can refine unmatched results Often used for final shaping WHERE can be used in each SELECT

Enjoying CROSS JOINs with SQL Complete

While the syntax for a CROSS JOIN in SQL is simple, it requires caution. These queries can produce massive result sets that are hard to manage and analyze. Additionally, you need to remember exact table and column names to write them correctly.

That's where SQL Complete proves invaluable. Its intelligent code completion and context-sensitive hints help you write faster and cleaner code. The Column Picker feature eliminates the need to memorize column names — just select the required columns from the filtered suggestion list.

In this tutorial, we'll explore CROSS JOINs in SQL, their practical use cases, and how SQL Complete can simplify working with JOIN queries.

dbForge SQL Complete

Enjoy even the most complex JOINs with SQL Complete
Free edition available

SQL CROSS JOIN explained with a practical example

Suppose, we have two database tables: Cars, listing car models and their prices, and Colors, listing color names and extra prices for those colors. We need to get all possible combinations of cars and colors. CROSS JOIN will be the solution:

SELECT
   c.Car_model
 ,c1.Color_name
FROM Cars c
CROSS JOIN Colors c1

We have three car models and three colors. The CROSS JOIN query returns nine results — the number of rows in the first table is multiplied by the number of rows in the second table: 3×3=9.

Use CROSS JOIN to join three tables in SQL Server

You can use the CROSS JOIN on as many tables as you want.

Assume, that we need to get all the combinations of not only car models and colors, but also tires that can go with those cars.

SELECT
  c.Car_model
 ,c1.Color_name
 ,t.Tire_manufacturer
FROM Cars c
CROSS JOIN Colors c1
CROSS JOIN Tires t

We have 3 car models, 3 car colors, and now we add 3 tire manufacturers. The result set consists of 27 rows as CROSS JOIN returns all the possible combinations of those: 3×3×3=27.

Use CROSS JOIN to generate a report

CROSS JOIN can help you retrieve database data and display it in the convenient format.

Assume we have the Staff table with the StaffID, Department, Shift_name, Start_time, and End_time columns. We want a report showing all possible shifts for the Maintenance department.

The CROSS JOIN clause will help us retrieve the information. We also filter the results using the WHERE condition.

SELECT s.Department, s1.Shift_name, s1.Start_Time, s1.End_Time
FROM Staff s
CROSS JOIN Staff s1
WHERE s.Department = 'Maintenance'
ORDER BY s1.Start_Time

Use CROSS JOIN to generate a large set of data

CROSS JOINs can produce very large result sets, so they should be used carefully.

For example, if the Car_models and Color_names tables each contain 1,000 rows, a CROSS JOIN between them would generate a result set of 1,000 × 1,000 = 1,000,000 rows.

Because of the potential performance impact, many experts recommend avoiding CROSS JOIN when possible. If it is truly necessary, pre-aggregate or filter the data beforehand to reduce the size of the result set.

How to avoid performance issues associated with CROSS JOINs:

  • Use another JOIN (INNER/LEFT/RIGHT) with 2 ON conditions
  • Use the GROUP BY clause to pre-aggregate data

SQL execution plan of the CROSS JOIN query

Let's compare two queries: one using a CROSS JOIN and the other an INNER JOIN.

Query 1: With CROSS JOIN

SELECT p.Product_name, s.Store_address
FROM Products p
CROSS JOIN Stores s;

Query 2: CROSS JOIN is replaced with INNER JOIN

SELECT p.Product_name, s.Store_address
FROM Products p
INNER JOIN Stores s ON p.ProductID=s.StoreID;

Both queries are fairly heavy and return one million rows. However, as shown in their execution plans, the INNER JOIN is noticeably faster and more efficient in terms of resource cost.

Summary table

Aspect Details
Definition Returns the Cartesian product of two tables (all possible row combinations).
Syntax SELECT * FROM Table1 CROSS JOIN Table2; Or: SELECT * FROM Table1, Table2;
Condition requirement No ON clause or condition is required.
Result size rows(Table1) × rows(Table2) – grows quickly with larger tables.
Use cases
  • Generate test data
  • Report templates
  • All combinations of sets
Filtering The WHERE clause can be added to limit results or simulate INNER JOIN.
Performance impact Can be resource-heavy; use with caution on large tables.
ANSI SQL compliance Fully supported in SQL-92 and major RDBMSs (PostgreSQL, Oracle, MySQL, etc.)
Visual output Every row from the left table paired with every row from the right table.
Alternate name(s) Cartesian JOIN, Cross Product
When NOT to use
  • If a relational condition is needed
  • On large tables without limits

Frequently Asked Questions

What is the difference between Cartesian JOIN and CROSS JOIN?
CROSS JOIN and Cartesian JOIN are essentially the same thing. When you run a statement containing the CROSS JOIN clause, you get a Cartesian Product — each row in the first table is paired with every row in the second table.
Is CROSS JOIN the same as FULL OUTER JOIN?

SQL CROSS JOIN and FULL OUTER JOIN are different JOIN types.

CROSS JOIN delivers a Cartesian product as all possible combinations of all rows in two tables. Besides, it does not have the ON clause as everything is joined with everything.

FULL OUTER JOIN is a combination of the LEFT OUTER and the RIGHT OUTER JOINs. It returns the rows in two tables that match the WHERE clause, and shows NULLs for the rows that don't match the ON condition.

What is a CROSS JOIN and NATURAL JOIN in SQL?
The difference between NATURAL JOIN and CROSS JOIN in SQL is quite straightforward. A NATURAL JOIN joins two tables implicitly, based on the common columns in the two tables that are joined. Common columns are the columns that have the same name and datatype. A NATURAL JOIN can be an INNER JOIN, a LEFT OUTER JOIN, or a RIGHT OUTER JOIN. Meanwhile CROSS JOIN produces a result set that contains all the attributes of both tables, including duplicate and common columns.
Is CROSS JOIN the same as a CROSS APPLY?

When comparing SQL CROSS JOIN and CROSS APPLY, it's important to note that CROSS APPLY returns only the rows from the left table that have matching results from the table-valued function on the right.

In this case, the table-valued function serves as the right input, while the outer table expression is the left input.

Effectively, CROSS APPLY behaves like a row-by-row INNER JOIN.

What is the difference between JOIN and INNER JOIN?
In case you don't specify the type of JOIN in your syntax, INNER JOIN will be used by default. However, for the purpose of code readability and clarity, it is better to specify the JOIN types, especially if the query contains different SQL JOINs.

With SQL Complete, you GET

  • Code snippets for JOIN clauses
  • Context-based prompts for table and column names
  • Navigation between the CASE and END keywords
  • Highlighting of matching keyword pairs
  • Quick info about database objects
  • Instant code formatter with built-in formatting profiles

dbForge SQL Complete

Enjoy even the most complex JOINs with SQL Complete
Free edition available