Cross Join in SQL

SQL

A Cross Join, also known as a Cartesian Join, produces a result set that is the product of two tables. Unlike other joins, it doesn’t consider any condition for the combination; instead, it forms all possible combinations of rows between the tables involved.

Syntax of Cross Join

The syntax for a Cross Join is straightforward:

SELECT * FROM table1
CROSS JOIN table2;
  • FROM table1: Specifies the first table involved in the cross join operation. Replace table1 with the actual name of your first table.
  • CROSS JOIN: This clause is where the magic happens. It instructs the database to combine every row from table1 with every row from table2. The result is a Cartesian product, meaning every row from the first table is paired with every row from the second table.
  • table2: Specifies the second table involved in the cross join operation. Replace table2 with the actual name of your second table

Let’s set up a simple database to work with throughout this tutorial.

Employees Table:

EmployeeIDEmployeeNameDepartment
1John DoeIT
2Jane SmithHR
3Bob JohnsonMarketing

Projects Table:

ProjectIDProjectName
101Database System
102Web Development
103Marketing Campaign

Example 1: Simple CROSS JOIN

Let’s begin with a basic example using our Employees and Projects tables:

SELECT * FROM Employees CROSS JOIN Projects;

Output:

EmployeeIDEmployeeNameDepartmentProjectIDProjectName
1John DoeIT101Database System
1John DoeIT102Web Development
1John DoeIT103Marketing Campaign
2Jane SmithHR101Database System
2Jane SmithHR102Web Development
2Jane SmithHR103Marketing Campaign
3Bob JohnsonMarketing101Database System
3Bob JohnsonMarketing102Web Development
3Bob JohnsonMarketing103Marketing Campaign

Example 2: Adding WHERE Clause

We can further refine our results by adding a WHERE clause to filter the combinations. Let’s find employees working on the ‘Web Development’ project:

SELECT * FROM Employees CROSS JOIN Projects WHERE Employees.Department = 'IT' AND Projects.ProjectName = 'Web Development';

Output:

EmployeeIDEmployeeNameDepartmentProjectIDProjectName
1John DoeIT102Web Development

Example 3: Combining CROSS JOIN with Other Joins

We can use CROSS JOIN in combination with other join types to create more complex queries. Let’s combine it with an INNER JOIN to find employees assigned to projects:

SELECT * FROM Employees CROSS JOIN Projects INNER JOIN Assignments ON Employees.EmployeeID = Assignments.EmployeeID AND Projects.ProjectID = Assignments.ProjectID;

Output:

EmployeeIDEmployeeNameDepartmentProjectIDProjectNameAssignmentID
1John DoeIT101Database System1
2Jane SmithHR102Web Development2

This query combines the results of CROSS JOIN with an INNER JOIN, filtering only the rows where there is a matching assignment.

Example 4: Using CROSS JOIN for Cartesian Product

Expanding on the basic concept, let’s create a Cartesian product of three tables - Employees, Projects, and Departments:

SELECT * FROM Employees CROSS JOIN Projects CROSS JOIN Departments;

Output:

EmployeeIDEmployeeNameDepartmentProjectIDProjectNameDepartmentIDDepartmentName
1John DoeIT101Database System1IT
1John DoeIT101Database System2HR
1John DoeIT101Database System3Marketing

This query creates a Cartesian product of Employees, Projects, and Departments, resulting in every possible combination.

Example 5: CROSS JOIN with Aggregation

We can also use CROSS JOIN in conjunction with aggregation functions. Let’s find the total number of projects each employee could be assigned to:

SELECT Employees.EmployeeID, Employees.EmployeeName, COUNT(Projects.ProjectID) AS TotalProjects
FROM Employees
CROSS JOIN Projects
GROUP BY Employees.EmployeeID, Employees.EmployeeName:

Output:

EmployeeIDEmployeeNameTotalProjects
1John Doe9
2Jane Smith9
3Bob Johnson9

This query counts the total number of projects each employee could be assigned to using CROSS JOIN and grouping.