In SQL, a cross join is a type of join that combines all rows from one table with all rows from another table. The result of a cross join is a Cartesian product of the two tables, which means that it contains all possible combinations of rows from the two tables.
The syntax for a cross join is as follows:
SELECT * FROM table1 CROSS JOIN table2; |
CROSS JOIN is a type of join in relational databases that produces a Cartesian product of two or more tables. Unlike INNER JOINs and OUTER JOINs, which are used to combine tables based on specific conditions, CROSS JOINs do not require any specific matching criteria. Each row from the first table is combined with every row from the second table, resulting in a potentially large output.
While CROSS JOINs can be useful in certain scenarios, they are generally less common and can have specific use cases:
- Generating reports: Cross joins can be used to generate reports that show all possible combinations of data from two or more tables. For example, you could use a cross join to generate a report that shows all possible products that could be ordered by a customer, along with the customer’s name, address, and phone number.
- Data mining: Cross joins can be used for data mining purposes, such as when you want to identify patterns or trends in data. For example, you could use a cross join to identify all customers who have ordered the same product twice in a row.
- Performance testing: Cross joins can be used to perform performance testing on your database. For example, you could use a cross join to generate a large number of rows and then measure how long it takes your database to process them.
Let’s consider two tables:

Let’s applied CROSS JOINS into tables.

Real life examples of JOINS
- Where do you work with e-commerce project here we have two tables orders and customers. Now orders table generally contains customer’s id. Here using INNER JOIN we can combine similar records of both tables and could fetch that which order placed by which customer.
- For calculating sports statistics you need to watch that every player record With the Gamers result tables record. Here if the player record store into a table and gamers result store into another table. Using left join, we can watch every players records with their gamer’s result combined.
- Use case of right join, during employee management, we have generally two tables employee and department. If we want to see every department and associated employees, then we can use here right join. Here employee as left table (parent) and department as right table (child). We can perform right join here.
Here is a short note about joins:
INNER JOIN: The INNER JOIN is the most common type of join. It retrieves records that have matching values in both tables. Rows without matching values are excluded from the result set.
For example, if you have a table of customers and a table of orders, you could use an INNER JOIN to get all customers who have placed orders. The INNER JOIN would only return rows where the customer ID in the customers table matches the customer ID in the orders table.
LEFT JOIN (or LEFT OUTER JOIN): The LEFT JOIN retrieves all records from the left (first) table and the matching records from the right (second) table. If there’s no match in the right table, NULL values are used for the columns from the right table.
For example, if you have a table of customers and a table of orders, you could use a LEFT JOIN to get all customers, even if they have not placed any orders. The LEFT JOIN would return all rows from the customers table, and the matching rows from the orders table. If a customer does not have any orders, the columns from the orders table will be NULL.
RIGHT JOIN (or RIGHT OUTER JOIN): The RIGHT JOIN is similar to the LEFT JOIN, but it retrieves all records from the right table and matching records from the left table. If there’s no match in the left table, NULL values are used for the columns from the left table.
For example, if you have a table of customers and a table of orders, you could use a RIGHT JOIN to get all orders, even if they have not been placed by a customer in the customers table. The RIGHT JOIN would return all rows from the orders table, and the matching rows from the customers table. If an order does not have a customer, the columns from the customers table will be NULL.
FULL OUTER JOIN: The FULL OUTER JOIN retrieves all records from both tables and fills in NULL values for columns where there is no match.
For example, if you have a table of customers and a table of orders, you could use a FULL OUTER JOIN to get all customers and orders, even if there are no matches between the two tables. The FULL OUTER JOIN would return all rows from both tables, and the NULL values would be used to indicate where there is no match.
CROSS JOIN: The CROSS JOIN generates a Cartesian product of rows from both tables, resulting in a combination of all rows from one table with all rows from the other table. No specific match criteria are used.
For example, if you have a table of customers and a table of orders, you could use a CROSS JOIN to get a table with all possible combinations of customers and orders. This would include customers who have not placed any orders, as well as customers who have placed multiple orders.
The CROSS JOIN is not as commonly used as the other types of joins, but it can be useful for some tasks, such as generating a list of all possible products that could be ordered by a customer.