Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

MySQL

MySQL CROSS JOIN

In the SQL language, the CROSS JOIN statement is a type of join on 2 SQL tables that allows returning the Cartesian product. In other words, it allows returning each row of a table with each row of another table. So performing the Cartesian product of table A that contains 30 results with a table B of 40 results will produce 1200 results (30 x 40 = 1200). In general, the CROSS JOIN statement is combined with the WHERE statement to filter the results that meet certain conditions.

Be careful, the number of results can easily be very high. If it is performed on tables with a lot of rows, it can slow down the server significantly.

 
[st_adsense]  

 

Syntax:

To perform a join with CROSS JOIN, it is necessary to perform an SQL query respecting the following syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT *
FROM table1
CROSS JOIN table2
SELECT * FROM table1 CROSS JOIN table2
SELECT *
FROM table1
CROSS JOIN table2

Alternative method for returning the same results:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT *
FROM table1, table2
SELECT * FROM table1, table2
SELECT *
FROM table1, table2

Either of these syntaxes allows associating all the results of table1 with each of the results of table2.
 

Example :

Let’s say we have a recipe application that contains 2 tables of ingredients, the vegetable table and the fruit table.
 
Vegetable table :

+----------+----------------+
|    vID   | vegetable_name |
+----------+----------------+
|     1    |   Carott       |
|     2    |   Onion        |
|     3    |   Leek         |
+----------+----------------+

 
[st_adsense]  
Fruit table :

+----------+----------------+
|    fID   |   fruit_name   |
+----------+----------------+
|    80    |   Banana       |
|    81    |   Kiwi         |
|    82    |   Pear         |
+----------+----------------+

For some reason, the application must combine all vegetables with all fruits. All combinations must be displayed. To do this, one or more of the following queries must be performed:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT vID, vegetable_name, fID, fruit_name
FROM vegetable
CROSS JOIN fruit
SELECT vID, vegetable_name, fID, fruit_name FROM vegetable CROSS JOIN fruit
SELECT vID, vegetable_name, fID, fruit_name
FROM vegetable
CROSS JOIN fruit

OR:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
SELECT vID, vegetable_name, fID, fruit_name
FROM vegetable, fruit
SELECT vID, vegetable_name, fID, fruit_name FROM vegetable, fruit
SELECT vID, vegetable_name, fID, fruit_name
FROM vegetable, fruit

Output:

+----------+----------------+-----------+---------------+
|    vID   | vegetable_name |     fID   |  fruit_name   |
+----------+----------------+-----------+---------------+
|     1    |    Carott      |     80    |    Banana     |
|     1    |    Carott      |     81    |    Kiwi       |
|     1    |    Carott      |     82    |    Pear       |
|     2    |    Onion       |     80    |    Banana     |
|     2    |    Onion       |     81    |    Kiwi       |
|     2    |    Onion       |     82    |    Pear       |
|     3    |    Leek        |     80    |    Banana     |
|     3    |    Leek        |     81    |    Kiwi       |
|     3    |    Leek        |     82    |    Pear       |
+----------+----------------+-----------+---------------+

The result shows that each vegetable is associated with each fruit. With 3 fruits and 3 vegetables, there are 9 rows of results (3 x 3 = 9).
 
[st_adsense] mcq

Leave a Reply

Your email address will not be published. Required fields are marked *