Your cart is currently empty!
Category: sql
-
Khóa học miễn phí SQL – Cross Join nhận dự án làm có lương
SQL – Cross Join
Table of content
The SQL Cross Join
An SQL Cross Join is a basic type of inner join that is used to retrieve the Cartesian product (or cross product) of two individual tables. That means, this join will combine each row of the first table with each row of second table (i.e. permutations).
A Cartesian product, or a cross product, is the result achieved from multiplication of two sets. This is done by multiplying all the possible pairs from both the sets.
The sample figure below illustrates the cross join in a simple manner.
As you can see, we considered two table columns: Hair Style and Hair Type. Each of these columns contain some records that need to be matched. Hence, using cross join, we combine each record in the “Hair Style” column with all records in the “Hair Type” column. The resultant table obtained is considered as the Cartesian product or Joined table.
Syntax
Following is the basic syntax of the Cross Join query in SQL −
SELECT column_name(s) FROM table1 CROSS JOIN table2;
Example
Assume we have created a table named CUSTOMERS, which contains the personal details of customers including their name, age, address and salary etc., using the following query −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ), (2, ''Khilan'', 25, ''Delhi'', 1500.00 );
The table will be created as −
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 Let us create another table ORDERS, containing the details of orders made and the date they are made on.
CREATE TABLE ORDERS ( OID INT NOT NULL, DATE VARCHAR (20) NOT NULL, CUSTOMER_ID INT NOT NULL, AMOUNT DECIMAL (18, 2) );
Using the INSERT statement, insert values into this table as follows −
INSERT INTO ORDERS VALUES (100, ''2009-10-08 00:00:00'', 3, 1500.00), (101, ''2009-11-20 00:00:00'', 2, 1560.00);
The table is displayed as follows −
OID DATE CUSTOMER_ID AMOUNT 100 2009-10-08 00:00:00 3 1500.00 101 2009-11-20 00:00:00 2 1560.00 Now, if we execute the following Cross Join query on these two tables given above, the cross join combines each row in CUSTOMERS table with each row in ORDERS table.
SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS CROSS JOIN ORDERS;
Output
The resultant table is as follows −
ID NAME AMOUNT DATE 2 Khilan 1500.00 2009-10-08 00:00:00 1 Ramesh 1560 2009-11-20 00:00:00 2 Khilan 1560 2009-11-20 00:00:00 1 Ramesh 1500.00 2009-10-08 00:00:00 Joining Multiple Tables with Cross Join
We can also join more than two tables using cross join. In this case, multiple-way permutations are displayed and the resultant table is expected to contain way more records than the individual tables.
Syntax
Following is the syntax to join multiple tables using cross join in SQL −
SELECT column_name(s) FROM table1 CROSS JOIN table2 CROSS JOIN table3 CROSS JOIN table4 .... .... .... CROSS JOIN tableN;
Example
Assume we have created another table named ORDER_RANGE using the following query −
CREATE TABLE ORDER_RANGE ( SNO INT NOT NULL, ORDER_RANGE VARCHAR (20) NOT NULL );
Now, we can insert values into this empty tables using the INSERT statement as follows −
INSERT INTO ORDER_RANGE VALUES (1, ''1-100''), (2, ''100-200''), (3, ''200-300'');
The ORDER_RANGE table is created as follows −
SNO ORDER_RANGE 1 1-100 2 100-200 3 200-300 Following query combines the three tables CUSTOMERS, ORDERS and ORDER_RANGE, using cross join −
SELECT ID, NAME, AMOUNT, DATE, ORDER_RANGE FROM CUSTOMERS CROSS JOIN ORDERS CROSS JOIN ORDER_RANGE;
Output
The resultant table is given below −
ID NAME AMOUNT DATE ORDER_RANGE 2 Khilan 1560 2009-11-20 00:00:00 1-100 1 Ramesh 1560 2009-11-20 00:00:00 1-100 2 Khilan 1500.00 2009-10-08 00:00:00 1-100 1 Ramesh 1500.00 2009-10-08 00:00:00 1-100 2 Khilan 1560 2009-11-20 00:00:00 100-200 1 Ramesh 1560 2009-11-20 00:00:00 100-200 2 Khilan 1500.00 2009-10-08 00:00:00 100-200 1 Ramesh 1500.00 2009-10-08 00:00:00 100-200 2 Khilan 1560 2009-11-20 00:00:00 200-300 1 Ramesh 1560 2009-11-20 00:00:00 200-300 2 Khilan 1500.00 2009-10-08 00:00:00 200-300 1 Ramesh 1500.00 2009-10-08 00:00:00 200-300
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
Khóa học miễn phí SQL – Full Join nhận dự án làm có lương
SQL – Full Join
The SQL Full Join
SQL Full Join creates a new table by joining two tables as a whole. The joined table contains all records from both the tables and fills NULL values for missing matches on either side. In short, full join is a type of outer join that combines the result-sets of both left and right joins.
MySQL does not support Full Outer Join. Instead, you can imitate its working by performing union operation between the result-sets obtained from Left Join and Right Join.
Let us understand this concept in detail with the help of a Venn diagram below. Assume that we have two tables as two sets (represented by circles). The result-set (or newly joined table) obtained using full join is nothing but the union of these two sets.

You can also achieve the equivalent result-set of FULL JOIN by performing the UNION operation on result-sets of the LEFT JOIN and RIGHT JOIN.
Syntax
Following is the basic syntax of Full Join in SQL −
SELECT column_name(s) FROM table1 FULL JOIN table2 ON table1.column_name = table2.column_name;
Example
Assume we have created a table named CUSTOMERS, which contains the personal details of customers including their name, age, address and salary etc.Using the following query −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ), (2, ''Khilan'', 25, ''Delhi'', 1500.00 ), (3, ''Kaushik'', 23, ''Kota'', 2000.00 ), (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ), (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ), (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ), (7, ''Muffy'', 24, ''Indore'', 10000.00 );
The table will be created as −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Let us create another table ORDERS, containing the details of orders made and the date they are made on.
CREATE TABLE ORDERS ( OID INT NOT NULL, DATE VARCHAR (20) NOT NULL, CUSTOMER_ID INT NOT NULL, AMOUNT DECIMAL (18, 2) );
Using the INSERT statement, insert values into this table as follows −
INSERT INTO ORDERS VALUES (102, ''2009-10-08 00:00:00'', 3, 3000.00), (100, ''2009-10-08 00:00:00'', 3, 1500.00), (101, ''2009-11-20 00:00:00'', 2, 1560.00), (103, ''2008-05-20 00:00:00'', 4, 2060.00);
The table is displayed as follows −
OID | DATE | CUSTOMER_ID | AMOUNT |
---|---|---|---|
102 | 2009-10-08 00:00:00 | 3 | 3000.00 |
100 | 2009-10-08 00:00:00 | 3 | 1500.00 |
101 | 2009-11-20 00:00:00 | 2 | 1560.00 |
103 | 2008-05-20 00:00:00 | 4 | 2060.00 |
Following query joins the two tables CUSTOMERS and ORDERS in SQL Server −
SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS FULL JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Output
The resultant table is produced as follows −
ID | NAME | AMOUNT | DATE |
---|---|---|---|
1 | Ramesh | NULL | NULL |
2 | Khilan | 1560 | 2009-11-20 00:00:00 |
3 | Kaushik | 3000 | 2009-10-08 00:00:00 |
3 | Kaushik | 1500 | 2009-10-08 00:00:00 |
4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
5 | Hardik | NULL | NULL |
6 | Komal | NULL | NULL |
7 | Muffy | NULL | NULL |
Joining Multiple Tables with Full Join
The Full Join query can also be used to join more than just two tables. To do that, we sequentially combine two tables at a time, until all the tables are joined together.
Note that in MySQL database, there is no provision to directly use the FULL JOIN keyword to perform join operation on multiple tables. Instead, calculate the UNION of LEFT JOIN and RIGHT JOIN on two tables at a time, until all the tables are joined.
Syntax
The syntax to join multiple tables using Full Join is given below −
SELECT column1, column2, column3... FROM table1 FULL JOIN table2 ON condition_1 FULL JOIN table3 ON condition_2 .... .... FULL JOIN tableN ON condition_N;
Example
To demonstrate Full Join, let us consider the sample tables CUSTOMERS and ORDERS that we previously created, and create another table name EMPLOYEE using the following query −
CREATE TABLE EMPLOYEE ( EID INT NOT NULL, EMPLOYEE_NAME VARCHAR (30) NOT NULL, SALES_MADE DECIMAL (20) );
Now, we can insert values into this empty tables using the INSERT statement as follows −
INSERT INTO EMPLOYEE VALUES (102, ''SARIKA'', 4500), (100, ''ALEKHYA'', 3623), (101, ''REVATHI'', 1291), (103, ''VIVEK'', 3426);
The EMPLOYEE table created, will be as shown below −
EID | EMPLOYEE_NAMENAME | SALES_MADE |
---|---|---|
102 | SARIKA | 4500 |
100 | ALEKHYA | 3623 |
101 | REVATHI | 1291 |
103 | VIVEK | 3426 |
Let us join these three tables using the full join query given below −
SELECT CUSTOMERS.ID, CUSTOMERS.NAME, ORDERS.DATE, EMPLOYEE.EMPLOYEE_NAME FROM CUSTOMERS FULL JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID FULL JOIN EMPLOYEE ON ORDERS.OID = EMPLOYEE.EID;
Through this query, we will display the id, name of the customer along with the date on which the orders are made and the name of the employee who sold the item.
Output
The resultant table is obtained as follows −
ID | NAME | DATE | EMPLOYEE_NAME |
---|---|---|---|
1 | Ramesh | NULL | NULL |
2 | Khilan | 2009-11-20 00:00:00 | REVATHI |
3 | Kaushik | 2009-10-08 00:00:00 | ALEKHYA |
3 | Kaushik | 2009-10-08 00:00:00 | SARIKA |
4 | Chaitali | 2008-05-20 00:00:00 | VIVEK |
5 | Hardik | NULL | NULL |
6 | Komal | NULL | NULL |
7 | Muffy | NULL | NULL |
Full Join with WHERE Clause
Joins use the ON clause to filter records by default. Let us suppose there is a further requirement to filter these records based on a certain condition/constraint, we can also make use of the WHERE clause with Joins.
Syntax
The syntax of Full Join when used with WHERE clause is given below −
SELECT column_name(s) FROM table1 FULL JOIN table2 ON table1.column_name = table2.column_name WHERE condition;
Example
Consider the previous two tables CUSTOMERS and ORDERS, and join them using the following Full Join query by applying some constraints using the WHERE clause.
SELECT ID, NAME, DATE, AMOUNT FROM CUSTOMERS FULL JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID WHERE ORDERS.AMOUNT > 2000.00;
Output
The resultant table after applying the WHERE clause with full join contains the rows that has amount values greater than 2000.00 −
ID | NAME | DATE | AMOUNT |
---|---|---|---|
3 | Kaushik | 2009-10-08 00:00:00 | 3000.00 |
4 | Chaitali | 2008-05-20 00:00:00 | 2060.00 |
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc