oracle

Saturday, 22 April 2017

SQL: Cross Join

This join Cartesion join, becasuce it returns values with cartesian prodct.
It mean that every value of first colume is attached with all the values of  second column .

Syntax:
select columns_name from table1 cross join table2 On(expression) or Using(column_name) Where(expression) order by column name;

Example:
select emp_name, dept_name from emp cross join dept;

Result:
Talha IT
Talha Oracle
Talha Finance
Talha Accounts
Talha Marketing
Zubair IT
Zubair Oracle
Zubair Finance
Zubair Accounts
Zubair Marketing
Salman IT
Salman Oracle
Salman Finance
Salman Accounts
Salman Marketing
shani IT
shani Oracle
shani Finance
shani Accounts
shani Marketing
Waqass IT
Waqass Oracle
Waqass Finance
Waqass Accounts
Waqass Marketing

Or Example:
select emp_name, dept_name from emp , dept;
Result:
Talha IT
Talha Oracle
Talha Finance
Talha Accounts
Talha Marketing
Zubair IT
Zubair Oracle
Zubair Finance
Zubair Accounts
Zubair Marketing
Salman IT
Salman Oracle
Salman Finance
Salman Accounts
Salman Marketing
shani IT
shani Oracle
shani Finance
shani Accounts
shani Marketing
Waqass IT
Waqass Oracle
Waqass Finance
Waqass Accounts
Waqass Marketing

Example with Where clause:
select emp_name, dept_name from emp cross join dept where dept_name='IT';

Result:
Talha IT
Zubair IT
Salman IT
shani IT
Waqass IT

No comments:

Post a Comment