Joins in SQL
Joins in SQL
• CROSS JOIN
• INNER JOIN
• FULL JOIN
• LEFT JOIN OUTER JOIN
• RIGHT JOIN
• SELF JOIN
Cross JOIN or Cartesian Product
• This type of JOIN returns the cartesian
product of rows from the tables in Join. It
will return a table which consists of
records which combines each row from
the first table with each row of the
second table.
SYNTAX
• SELECT column-name-list FROM
table-name1 CROSS JOIN table-
name2;
EXAMPLE
CLASS TABLE
CLASS_INFO TABLE
SELECT * FROM class CROSS JOIN
class_info;
INNER JOINN OR EQUI JOIN
This type of join returns those records which
have matching values in both tables.
SYNTAX
• SELECT column-name-list FROM table-
name1 INNER JOIN table-name2 WHERE
table-name1.column-name = table-
name2.column-name;
SELECT * from class INNER JOIN class_info
where class.id = class_info.id;
Natural JOIN
• SELECT * FROM table-name1 NATURAL JOIN
table-name2;
SELECT * from class NATURAL JOIN
class_info;
OUTER JOIN
• Outer Join is based on both matched and
unmatched data
• Left Outer Join
• Right Outer Join
• Full Outer Join
LEFT Outer Join
• The left outer join returns a resultset table
with the matched data from the two tables
and then the remaining rows of the left table
and null from the right table's columns
SYNTAX
• SELECT column-name-list FROM table-name1
LEFT OUTER JOIN table-name2 ON table-
name1.column-name = table-name2.column-
name;
SELECT * FROM class LEFT OUTER JOIN
class_info ON (class.id = class_info.id);
RIGHT Outer Join
• The right outer join returns a resultset table
with the matched data from the two tables
being joined, then the remaining rows of
the right table and null for the
remaining left table's columns.
SYNTAX
SELECT column-name-list FROM table-name1
RIGHT OUTER JOIN table-name2 ON table-
name1.column-name = table-name2.column-
name;;
SELECT * FROM class RIGHT OUTER JOIN
class_info ON (class.id = class_info.id);
Full Outer Join
SELECT column-name-list FROM table-name1
FULL OUTER JOIN table-name2 ON table-
name1.column-name = table-name2.column-
name;
SELECT * FROM class FULL OUTER JOIN
class_info ON (class.id = class_info.id);
SQL> SELECT EMPNO, ENAME, DEPTNO, DNAME FROM EMP NATURAL
JOIN DEPT;
• The NATURAL JOIN clause is based on all
columns in the two tables that have the
same name.
• It selects rows from the two tables that
have equal values in all matched
columns.
SELF JOIN