Link Search Menu Expand Document

Module 3: Cross Join Explained

Previous Tutorial - Next Tutorial

A cross join is the simplest type of join, though not the most commonly used one. This join performs the Cartesian product of the two input tables. In other words, it performs a multiplication between the tables and returns a row for each combination of rows from both sides of the table. For example, if you have X rows in table T-1 and Y rows in table T-2, then the result of a cross join between tables T1 and T2 is a virtual table with X multiplication Y rows. Needless to say, this can produce a large volume of rows and could be an effective way to populate a test database table with some simple data.

In most situations, the result of a cross join will not make sense except if you have some precise requirement. This is the general syntax of cross join. It's very straightforward and there is no need to define a predicate with ON keyword. In the diagram, the left table has three rows with key values A, B, and C. The right table has four rows with the key values, B1, B2, C1, and D1. If I use a cross join for these two tables, then the result of this query is a table with 12 rows containing all possible combinations of rows from the two input tables.

Now let's open the SQL Server Management Studio to see this cross join in the action. To easily understand this join, I'm going to use the product category table as this table contains only four records. If I execute this query, I'm getting four rows. Now I'm going to do the cross join with the same table. The cross join between these two tables will give us four multiplication four equal to sixteen (4x4=16)rows. To use the same table, I need to give the alias on this table.

SQL Cross Joins

Now join the same table using the cross join. So for that, I need to write(CROSS JOIN Production.ProductCategory AS pc2). Use this alias in the column name. Now, add the order by clause to easily understand the result. The product category table contains four records, and if I execute this query, then you clearly see that for every product category, four rows are added to the result set.

If you do the math, then the result will contain four multiplications; four is equal to 16 (4x4=16)rows. This is how a cross join works. You can rewrite the same query again using the older style syntax where you specify a comma between these two tables. You can simply omit the cross join syntax and separate the table reference with the comma. First of all, copy the same query. I just need to specify a comma between these two tables.

SQL Cross Join

If I execute this query, then it will give me the same result, 16 rows. However, it is recommended to stick to the cross join syntax as it is less prone to errors and allows for more consistent code.

Web API for developers Free Trial Offline SDK

Here's SQL video tutorial:

Other useful tutorials:

Back to top

© , Learn SQL 24 / 7 — All Rights Reserved - Terms of Use - Privacy Policy