SQL CROSS JOIN Example
Let’s look at some examples of CROSS JOIN statement in SQL to understand it’s working.
Demo SQL Database
In this CROSS JOIN tutorial, we will use the following two tables in examples:
Table 1- Customer
ID | NAME | AGE | PHONE |
---|---|---|---|
1 | AMIT JAIN | 21 | 98474 |
2 | JATIN VERMA | 47 | 63996 |
Table 2- Orders
ORDER_ID | AMOUNT | PLACED_ON |
---|---|---|
101 | 999 | 2023-04-19 |
102 | 4999 | 2023-04-20 |
To create both these tables on your system, you can write the following code:
CREATE DATABASE w3wiki;
USE w3wiki;
CREATE TABLE CUSTOMER(
ID INT,
NAME VARCHAR(20),
AGE INT,
PHONE INT);
CREATE TABLE ORDERS(
ORDER_ID INT,
AMOUNT INT,
PLACED_ON DATE);
INSERT INTO CUSTOMER VALUES(1,'AMIT JAIN',21,98474);
INSERT INTO CUSTOMER VALUES(2,'JATIN VERMA',47,63996);
INSERT INTO ORDERS VALUES(101,999,'2023-04-19');
INSERT INTO ORDERS VALUES(102,4999,'2023-04-20');
CROSS JOIN Example
In this example, we will use the CROSS JOIN command to match the data of the Customer and Orders table.
Query
SELECT *
FROM CUSTOMER
CROSS JOIN ORDERS;
Output
As we can see, whether the other table matches or not, the CROSS JOIN keyword returns all similar records from both tables. Therefore, if there are rows in “Customers” or “Orders” that do not match any entries in either table, those rows will also be listed.
SQL CROSS JOIN
SQL CROSS JOIN returns all the records from the left and right tables. CROSS JOIN returns a combination of each row in the left table paired with each row in the right table.
Contact Us