
SQL Cross Join combines each row of one table with each row of another table and the number of rows in the result set is the product of number of rows in both the tables. In this article, we will briefly discuss how to combine two or more tables using SQL Cross Join.
In the previous articles, we have discussed different types of Joins in SQL, such as Inner Join, Left Join, and Right Join. This article will discuss another type of SQL Join, i.e., Cross Join.
Must Check: Free Database and SQL Online Course and Certificates
Must Check: SQL tutorial
Table of Content
What is SQL Cross Join?
Similar to other Joins in SQL, cross join is used to combine two or more different tables. Cross join in SQL combines each row of one table with each row of another table and returns the cartesian product of all the table elements.
In simple terms, cross join in SQL returns all the possible combinations of both tables.
- The result set of Cross Join is given in the ordered pair in which the first element is from table1 and the second element is from table 2.
- Number of rows in the result set is equal to the product of the number of rows in the first table and the number of rows in the second table.
Read Also: Update Query in SQL
The product of cross join can be easily understood by the given below example:
The above picture contains the two sets, each containing three elements, and when the cross join (cartesian product) is performed, the result set will contain nine elements.
Let’s take an example to get a better understanding of how SQL cross works:
Example: Let there be two tables, table 1 ( containing student name) and table 2 ( containing subject name), and when we cross join both the tables, we will get the following result.
Syntax
The query of Cross Join can be written in two ways:
1st Method:
SELECT column_namesFROM table_1 CROSS JOIN table_2;
2nd Method:
SELECT column_namesFROM table_1, table_2;
Must Read: SQL Logical Operator
Note:
- Both the methods will produce the same result
- Unlike the Inner Join, Left Join, and Right Join, SQL Cross Join does not require any joining conditions.
Now, let’s use the same above example and write the query to produce the result:
Firstly we will create both Student and Subject tables:
CREATE TABLE Student ( Id int PRIMARY KEY Student Name VARCHAR (56));
INSERT INTO Student VALUES (1, Kartik);INSERT INTO Student VALUES (2, Amit);INSERT INTO Student VALUES (2, Shewtank);
SELECT *FROM Student;
The above query will produce the following table:
Id | Student Name |
1 | Kartik |
2 | Amit |
3 | Shewtank |
CREATE TABLE Subject ( Id int PRIMARY KEY Subject Name VARCHAR (56));
INSERT INTO Subject VALUES (1, English);INSERT INTO Subject VALUES (2, Hindi);INSERT INTO Subject VALUES (2, Math);
SELECT *FROM Subject;
The above query will produce the following result:
Id | Subject Name |
1 | Engish |
2 | Hindi |
3 | Math |
Must Read: SQL LIMITS
Now, finally, we will write the query to combine both the table using cross join:
SELECT st.Student Name, su.Subject NameFROM Student stCROSS JOIN Subject su;
The above query will produce the following table:
Student Name | Subject Name |
Kartik | English |
Amit | English |
Shewtank | English |
Kartik | Hindi |
Amit | Hindi |
Shewtank | Hindi |
Kartik | Math |
Amit | Math |
Shewtank | Math |
We were looking for this; the cross join query (the result set) returns the same nine entries.
Till now, we have learned how to join two tables using cross join, and now we will see how to join more than two tables using SQL Cross Join.
SQL CROSS JOIN with Three Different Tables
Let there are three different tables Menu, Restaurant Name, and Location
Restaurant Name:
Id | Restaurant Name |
1 | Evergreen |
2 | Hudson Cafe |
Menu
Id | Food Item |
1 | Chole Bhature |
2 | Lassi |
3 | Diet Coke |
Location
Id | Location |
1 | Green Park |
2 | Mukherjee Nagar |
Now, we want to see all the possible combinations where these food items are available. The below query will help you to find all the possible combinations:
SELECT m.Food Item AS Item, r.Restaurant Name, l.Location
FROM Menu mCROSS JOIN Restaurant rCROSS JOIN Location l;
Output
Item | Restaurant Name | Location |
Chole Bhature | Evergreen | Green Park |
Lassi | Evergreen | Green Park |
Diet Coke | Evergreen | Green Park |
Chole Bhature | Hudson Cafe | Green Park |
Lassi | Hudson Cafe | Green Park |
Diet Coke | Hudson Cafe | Green Park |
Chole Bhature | Evergreen | Mukherjee Nagar |
Lassi | Evergreen | Mukherjee Nagar |
Diet Coke | Evergreen | Mukherjee Nagar |
Chole Bhature | Hudson Cafe | Mukherjee Nagar |
Lassi | Hudson Cafe | Mukherjee Nagar |
Diet Coke | Hudson Cafe | Mukherjee Nagar |
Must Read: All about DML Command in SQL
Must Read: SQL ACID Properties
Conclusion
In this article, we have briefly discussed how to join two tables in sql using cross join with examples.
Hope this article will help you in your data science/data analysis journey.
Keep Learning!!
Keep Sharing!!
Download this article as PDF to read offline
Download as PDF
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio
Comments