Professional Documents
Culture Documents
Joins in SQL
Joins in SQL
1. EQUI JOIN :
EQUI JOIN creates a JOIN for equality or matching column(s) values of the relative tables. EQUI
JOIN also create JOIN by using JOIN with ON and then providing the names of the columns with their
relative tables to check equality using equal sign (=).
SELECT * FROM EMP,DEPT WHERE EMP.DEPT_NO=DEPT.DEPT_NO;
2.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 * FROM EMP INNER JOIN DEPT ON EMP.DEPT_NO=DEPT.DEPT_NO
3.OUTER JOIN
1. LEFT OUTER 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.
2. RIGHT OUTER 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.
3. FULL OUTER JOIN: FULL JOIN creates the result-set by combining result of both LEFT
JOIN and RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows
for which there is no matching, the result-set will contain NULL values.
4. Natural Join :
Natural Join joins two tables based on same attribute name and datatypes. The resulting
table will contain all the attributes of both the table but keep only one copy of each
common column.
SELECT * FROM EMP NATURAL JOIN DEPT
5. Cross Join :
Cross join allows us to join each and every row of both the tables. It is similar to the cartesian product
that joins all the rows.
SELECT * FROM EMP CROSS JOIN DEPT
6. SELF JOIN: As the name signifies, in SELF JOIN a table is joined to itself. That is, each row of the
table is joined with itself and all other rows depending on some conditions. A SELF JOIN is a
regular join, but the table is joined with itself – this is extremely useful for
comparisons within a table.