SQL join clause is used to to retrieve data from two or more database tables. In previous article, I have explained the Different Types of SQL Joins. In this article, I would explain the difference among inner join, equi join and natural join.
This is the most used join in the SQL. this join returns only those records/rows that match/exists in both the database tables.
Inner Join Example
SELECT * FROM tblEmp JOIN tblDept ON tblEmp.DeptID = tblDept.DeptID;
In the join condition, you can also use other operators like <,>,<>.
Equi join is a special type of join in which we use only equality operator. Hence, when you make a query for join using equality operator then that join query comes under Equi join.
Equi Join Example
SELECT * FROM tblEmp JOIN tblDept ON tblEmp.DeptID = tblDept.DeptID; --Using Clause is not supported by SQL Server --Oracle and MySQL Query SELECT * FROM tblEmp INNER JOIN tblDept USING(DeptID)
Inner join can have equality (=) and other operators (like <,>,<>) in the join condition.
Equi join only have equality (=) operator in the join condition.
Equi join can be an Inner join, Left Outer join, Right Outer join
The USING clause is not supported by SQL Server and Sybase. This clause is supported by Oracle and MySQL.
Natural join is a type of equi join which occurs implicitly by comparing all the same names columns in both tables. The join result have only one column for each pair of equally named columns.
Natural Join Example
--Run in Oracle and MySQL SELECT * FROM tblEmp NATURAL JOIN tblDept
In the above join result we have only one column "DeptID" for each pair of equally named columns.
In Natural join, you can't see what columns from both the tables will be used in the join. In Natural join, you might not get the desired result what you are expecting.
Natural join clause is not supported by SQL Server, it is supported by Oracle and MySQL.
What do you think?
I hope you will enjoy the tips while playing with SQL Server. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.