In this article we will learn about Different Types of SQL JOINs. A SQL Join statement is used to combine data or rows from two or more tables based on a common field between them. A join condition is a relationship among some columns in the data tables that take part in SQL join. Basically, database tables are related to each other with keys. We use this keys relationship in SQL Joins. Please read my previous article of Find all the Columns name in a table.
Different Types of SQL Joins
When we want to get the related data from the table or more than one table then we do joining of the tables. There can be different type of join.
- Inner Join
- Outer join ( Left/Right)
- Self join
- Cross join
SQL Join Examples
Suppose we have 3 set of tables and the result set are look likes below:
Inner Join
The INNER JOIN keyword selects all rows from both the tables as long as the condition satisfies. This keyword will create the result-set by combining all rows from both the tables where the condition satisfies i.e value of the common field will be same.
SELECT emp.FirstName,emp.Salary,dept.DeptName,desg.DesigName FROM Employee emp INNER JOIN Department dept ON emp.EmpDept=dept.DeptId INNER JOIN Designation desg ON dept.DeptId=desg.intDeptId
When we execute the statement the Final query should look like this:]
Left Join
This join returns all the rows of the table on the left side of the join and matching rows for the table on the right side of join. The rows for which there is no matching row on right side, the result-set will contain null. LEFT JOIN is also known as LEFT OUTER JOIN
SELECT emp.FirstName,emp.Salary,dept.DeptName,desg.DesigName FROM Employee emp left JOIN Department dept ON emp.EmpDept=dept.DeptId left JOIN Designation desg ON dept.DeptId=desg.intDeptId
When we execute the statement the Final query should look like this:
You can see the query fetch all the records from left side ( Table A ) if it not matched with B also.
Right Join
RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the right side of the join and matching rows for the table on the left side of join. The rows for which there is no matching row on left side, the result-set will contain null. RIGHT JOIN is also known as RIGHT OUTER JOIN.
SELECT emp.FirstName,emp.Salary,dept.DeptName,desg.DesigName FROM Employee emp RIGHT JOIN Department dept ON emp.EmpDept=dept.DeptId RIGHT JOIN Designation desg ON dept.DeptId=desg.intDeptId
When we execute the statement the Final query should look like this:
Self Join
The SQL SELF JOIN is used to join a table to itself as if the table were two tables; temporarily renaming at least one table in the SQL statement.
SELECT a.EmployeeID, b.FirstName, a.SALARY FROM Employee a, Employee b WHERE a.SALARY < b.SALARY;
When we execute the statement the Final query should look like this:
Cross Join
Cross join is a Cartesian join means Cartesian product of both the tables. This join does not need any condition to join two tables. This join returns records/rows that are multiplication of record number from both the tables means each row on left table will related to each row of right table
SELECT emp.FirstName,emp.Salary,dept.DeptName,desg.DesigName FROM Employee AS emp, Department AS dept, Designation As desg ORDER BY emp.FirstName
When we execute the statement the Final query should look like this:
You can see above the result set is Cartesian product of 3 tables.
Still have any query or concern about SQL joins then give comments on the DISQUS Forum.