SQL Cross Join
Now let us see take a deeper dive into the cross join in SQL with the help of examples. All the queries in the examples will be written using the MySQL database. Consider we have the following tables with the given data: Table 1: MatchScore
Table 2: Departments
Table 3: employee
Table 4: department
Table 5: loan
Table 6: borrower
Table 7: customer
Table 8: orders
Example 1:Write a query to perform the cross join operation considering the MatchScore table as the left table and the Departments table as the right table. Query: We have used the SELECT command with the asterisk to retrieve all the columns present in the MatchScore and Departments table. Then we have used the CROSS JOIN keyword to perform the cross join operation on the MatchScore and Departments table. Since there are 4 records in the MatchScore and 3 records in the Departments table, after performing the cross join operation, we will get 12 rows. After executing this query, you will find the following result:
Each row from the MatchScore table is combined with each row of the Departments table. Since there are four records in the MatchScore and three records in the Departments table, we have got 12 rows in the final table after performing the cross join operation. Example 2:Write a query to perform the cross join operation considering the employee table as the left table and the department table as the right table. Query: We have used the SELECT command with the asterisk to retrieve all the columns present in the employee and department table. Then we have used the CROSS JOIN keyword to perform the cross join operation on the employee and department table. Since there are five records in the employee and four records in the department table, after performing the cross join operation, we will get 20 rows. After executing this query, you will find the following result:
Each row from the employee table is combined with each row of the department table. Since there are five records in the employee table and four records in the department table, we have got 20 rows in the final table after performing the cross join operation. Example 3: Write a query to perform the cross join operation considering the loan table as the left table and the borrower table as the right table. Query: We have used the SELECT command with the asterisk to retrieve all the columns present in the loan and the borrower table. Then we have used the CROSS JOIN keyword to perform the cross join operation on the loan and the borrower table. Since there are four records in the loan table and four records in the borrower table, after performing the cross join operation, we will get 16 rows. After executing this query, you will find the following result:
Each row from the loan table is combined with each row of the borrower table. Since there are four records in the loan table and four records in the borrower table, after performing the cross join operation, we have got 16 rows. Example 4:Write a query to perform the cross join operation considering the customer table as the left table and the orders table as the right table. Query: We have used the SELECT command with the asterisk to retrieve all the columns present in the customer and orders table. Then we have used the CROSS JOIN keyword to perform the cross join operation on the customer table and the orders table. Since there are three records in the loan table and three records in the orders table, after performing the cross join operation, we will get 9 rows. After executing this query, you will find the following result:
Each row from the customers' table is combined with each row of the orders table. Since there are three records in the loan table and three records in the orders table, after performing the cross join operation, we will get 9 rows. Next TopicSQL Primary Key |