0% found this document useful (0 votes)
13 views31 pages

Joins in SQL

Uploaded by

s g shrinivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views31 pages

Joins in SQL

Uploaded by

s g shrinivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

JOINS IN SQL

What is meant by JOIN?


JOINS in SQL are commands which are used
to combine rows from two or more tables,
based on a related column between those
tables.
JOINS are used when a user is trying to
extract data from tables which have one-to-
many or many-to-many relationships
between them.
TYPES OF JOINS

• 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

QL> SELECT A.EMPNO, A.SAL FROM EMP A, EMP


B WHERE A.SAL < B.SAL;
SQL> SELECT A.ID, A.NAME FROM CLASS A, CLASS B
WHERE A.ID = B.ID;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy