- Data is stored in multiple tables that are related to each other in relational data bases like SQL Server, MySQL etc with a common key value.
- When we need to get row by combining one or more tables based on common fields between them can be achieved using a SQL Join.
- When we need to extract records from two or more tables into a new table, JOIN clause helps us to acheive this based on certain condition.
- Based on logical relationship between tables join clause helps us to query and access data from multiple tables.
- In other words, Join show how data from a row can be fetched from one table to another table.
Different types of Joins are:
- Inner join/Simple Join
- Left Outer join/Left Join
- Right Outer Join/Right join
- Full Outer join
- Cross Join
- Self Join
Inner Join
- In SQL if a defined condition is valid the inner join will select all the matching rows and columns from both tables.
Syntax
Left Join
- In left join all records are retrived from left table(table1) and matched rows and column from right table(table2).
- If there is no matching rows or columns, left join returns NULL.
Syntax
Right Join or Right Outer Join
- Records from the right table are retrived in RIGHT JOIN and the matched rows or columns from the left table(table1).
- If there is no matching rows or columns it will return NULL.
Syntax
Full Join or Full Outer Join
- This join is a combination of both Left join and right join.
- The joined tables returns all the records from both the tables.
- If no matches are found in the table, this join will return a NULL Value.
- It is also called as Full Outer Join.
Syntax
Cross Join
- It is also known as Cartesian Join which returns cartesian product of two or more joined tables.
- Cross join produces a table that merges each rows from the first table with each rows of second table.
- No condition ir required for CROSS JOIN.
Syntax
Self Join
- Tables formed by joining itself is called Self Join.
- It makes a temporary naming of atleast one table in SQL Statement.
Syntax