SQL CARTESIAN JOIN or CROSS JOIN - The Coding Shala

Home >> Learn SQL >> SQL CARTESIAN JOIN

SQL CARTESIAN JOIN or CROSS JOIN

The SQL CARTESIAN JOIN returns the cartesian product of the sets of rows from the joined tables. SQL CARTESIAN JOIN is also called SQL CROSS JOIN.
SQL CARTESIAN JOIN or CROSS JOIN - The Coding Shala

SQL CARTESIAN JOIN Syntax

The basic syntax of SQL CARTESIAN JOIN or CROSS JOIN is as follows - 

 SELECT table1.column1, table2.column2...  
 FROM table1, table2 [, table3 ];  

SQL CARTESIAN JOIN Examples

The following two tables are used for the examples -
Table 1. 'emp' table 

 emp_id     emp_name     city     dept_no     salary  
 1          Akshay        Pune      101       50000  
 3          Nikhil        Pune      101       51000  
 5          Mohit         Delhi     103       40000  
 6          Shubham       Surat     105       42000  
 7          Akash         Mumbai    106       45000  

Table 2. 'dept' table 

dept_no     dept_name      total_emp  
 101          Product Dev       50  
 102          Consulting       100  
 103          Product Consult   20  
 104          Marketing        150  
 105          Sales            250  

The following SQL query will join these two tables using CARTESIAN JOIN or CROSS JOIN -

SQL >> select emp_id, emp_name, dept_name  
        from emp, dept;  
 Output >>   
 emp_id     emp_name     dept_name  
 1          Akshay     Product Dev  
 1          Akshay     Consulting  
 1          Akshay     Product Consult  
 1          Akshay     Marketing  
 1          Akshay     Sales  
 3          Nikhil     Product Dev  
 3          Nikhil     Consulting  
 3          Nikhil     Product Consult  
 3          Nikhil     Marketing  
 3          Nikhil     Sales  
 5          Mohit     Product Dev  
 5          Mohit     Consulting  
 5          Mohit     Product Consult  
 5          Mohit     Marketing  
 5          Mohit     Sales  
 6          Shubham     Product Dev  
 6          Shubham     Consulting  
 6          Shubham     Product Consult  
 6          Shubham     Marketing  
 6          Shubham     Sales  
 7          Akash     Product Dev  
 7          Akash     Consulting  
 7          Akash     Product Consult  
 7          Akash     Marketing  
 7          Akash     Sales  



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala