Mrs. Sunita M Dol
Page 1
Joined Relations
JOINS are used to retrieve data from multiple tables. An Oracle JOIN is performed
whenever two or more tables are joined in a SQL statement.
There are 4 different types of joins:
 INNER JOIN (or sometimes called simple join)
 LEFT OUTER JOIN (or sometimes called LEFT JOIN)
 RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
 FULL OUTER JOIN (or sometimes called FULL JOIN)
INNER JOIN (simple join)
Visual Illustration
In this visual diagram, the INNER JOIN returns the shaded area:
The INNER JOIN would return the records where table1 and table2 intersect.
LEFT OUTER JOIN
Another type of join is called an LEFT OUTER JOIN. This type of join returns all rows from
the LEFT-hand table specified in the ON condition and only those rows from the other table
where the joined fields are equal (join condition is met).
Visual Illustration
In this visual diagram, the LEFT OUTER JOIN returns the shaded area:
The LEFT OUTER JOIN would return the all records from table1 and only those records
from table2 that intersect with table1.
Mrs. Sunita M Dol
Page 2
RIGHT OUTER JOIN
Another type of join is called an RIGHT OUTER JOIN. This type of join returns all rows
from the RIGHT-hand table specified in the ON condition and only those rows from the
other table where the joined fields are equal (join condition is met).
Visual Illustration
In this visual diagram, the RIGHT OUTER JOIN returns the shaded area:
The RIGHT OUTER JOIN would return the all records from table2 and only those records
from table1 that intersect with table2.
FULL OUTER JOIN
Another type of join is called an FULL OUTER JOIN. This type of join returns all rows
from the LEFT-hand table and RIGHT-hand table with nulls in place where the join
condition is not met.
Visual Illustration
In this visual diagram, the FULL OUTER JOIN returns the shaded area:
The FULL OUTER JOIN would return the all records from both table1 and table2.
Each of the variants of the join operations in SQL consists of a join type and a join condition.
The join condition defines which tuples in the two relations match and what attributes are
present in the result of the join. The join type defines how tuples in each relation that do not
match any tuple in the other relation (based on the join condition) are treated.
Figure 1 shows some of the allowed join types and join conditions. The first join type is the
inner join, and the other three are the outer joins.
Figure 1: Join types and join conditions.
Consider the relations for joined relations
Mrs. Sunita M Dol
Consider the relations for joined relations
Mrs. Sunita M Dol
Page 3
Mrs. Sunita M Dol
Page 4
Inner Join
Using NATURAL condition
select * from course;
course_id title dept_namecredits
BIO-101 Intro. to Biology Biology 4
BIO-301 Genetics Biology 4
BIO-399 Computational Biology Biology 3
CS-101 Intro. to Computer ScienceComp. Sci. 4
CS-190 Game Design Comp. Sci. 4
CS-315 Robotics Comp. Sci. 3
CS-319 Image Processing Comp. Sci. 3
CS-347 Database System ConceptsComp. Sci. 3
EE-181 Intro. to Digital Systems Elec. Eng. 3
FIN-201 Investment Banking Finance 3
HIS-351 World History History 3
MU-199 Music Video Production Music 3
PHY-101 Physical Principles Physics 4
select * from prereq;
course_id prereq_id
BIO-301 BIO-101
BIO-399 BIO-101
CS-190 CS-101
CS-315 CS-101
CS-319 CS-101
CS-347 CS-101
EE-181 PHY-101
select * from course natural join prereq;
Mrs. Sunita M Dol
Page 5
course_id title dept_name credits prereq_id
BIO-301 Genetics Biology 4 BIO-101
BIO-399 Computational
Biology
Biology 3 BIO-101
CS-190 Game Design Comp. Sci. 4 CS-101
CS-315 Robotics Comp. Sci. 3 CS-101
CS-319 Image
Processing
Comp. Sci. 3 CS-101
CS-347 Database System
Concepts
Comp. Sci. 3 CS-101
EE-181 Intro. to Digital
Systems
Elec. Eng. 3 PHY-101
select * from course natural inner join prereq;
course_id title dept_namecreditsprereq_id
BIO-301 Genetics Biology 4 BIO-101
BIO-399 Computational Biology Biology 3 BIO-101
CS-190 Game Design Comp. Sci. 4 CS-101
CS-315 Robotics Comp. Sci. 3 CS-101
CS-319 Image Processing Comp. Sci. 3 CS-101
CS-347 Database System ConceptsComp. Sci. 3 CS-101
EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101
Using ON condition
select * from course inner join prereq on course.course_id=prereq.course_id;
course_id title dept_namecreditscourse_idprereq_id
BIO-301 Genetics Biology 4 BIO-301 BIO-101
BIO-399 Computational Biology Biology 3 BIO-399 BIO-101
CS-190 Game Design Comp. Sci. 4 CS-190 CS-101
Mrs. Sunita M Dol
Page 6
course_id title dept_namecreditscourse_idprereq_id
CS-315 Robotics Comp. Sci. 3 CS-315 CS-101
CS-319 Image Processing Comp. Sci. 3 CS-319 CS-101
CS-347 Database System ConceptsComp. Sci. 3 CS-347 CS-101
EE-181 Intro. to Digital Systems Elec. Eng. 3 EE-181 PHY-101
Using USING condition
select * from course inner join prereq using (course_id);
course_id title dept_namecreditsprereq_id
BIO-301 Genetics Biology 4 BIO-101
BIO-399 Computational Biology Biology 3 BIO-101
CS-190 Game Design Comp. Sci. 4 CS-101
CS-315 Robotics Comp. Sci. 3 CS-101
CS-319 Image Processing Comp. Sci. 3 CS-101
CS-347 Database System ConceptsComp. Sci. 3 CS-101
EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101
Mrs. Sunita M Dol
Page 7
Left Outer Join
Using NATURAL condition
select * from course natural left outer join prereq;
course_id title dept_namecreditsprereq_id
BIO-101 Intro. to Biology Biology 4
BIO-301 Genetics Biology 4 BIO-101
BIO-399 Computational Biology Biology 3 BIO-101
CS-101 Intro. to Computer ScienceComp. Sci. 4
CS-190 Game Design Comp. Sci. 4 CS-101
CS-315 Robotics Comp. Sci. 3 CS-101
CS-319 Image Processing Comp. Sci. 3 CS-101
CS-347 Database System ConceptsComp. Sci. 3 CS-101
EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101
FIN-201 Investment Banking Finance 3
HIS-351 World History History 3
MU-199 Music Video Production Music 3
PHY-101 Physical Principles Physics 4
Using ON condition
select * from course left outer join prereq on course.course_id = prereq.course_id;
course_id title dept_namecreditscourse_idprereq_id
BIO-101 Intro. to Biology Biology 4
BIO-301 Genetics Biology 4 BIO-301 BIO-101
BIO-399 Computational Biology Biology 3 BIO-399 BIO-101
CS-101 Intro. to Computer ScienceComp. Sci. 4
CS-190 Game Design Comp. Sci. 4 CS-190 CS-101
CS-315 Robotics Comp. Sci. 3 CS-315 CS-101
Mrs. Sunita M Dol
Page 8
CS-319 Image Processing Comp. Sci. 3 CS-319 CS-101
CS-347 Database System ConceptsComp. Sci. 3 CS-347 CS-101
EE-181 Intro. to Digital Systems Elec. Eng. 3 EE-181 PHY-101
FIN-201 Investment Banking Finance 3
HIS-351 World History History 3
MU-199 Music Video Production Music 3
PHY-101 Physical Principles Physics 4
Using USING condition
select * from course left outer join prereq using (course_id);
course_id title dept_namecreditsprereq_id
BIO-101 Intro. to Biology Biology 4
BIO-301 Genetics Biology 4 BIO-101
BIO-399 Computational Biology Biology 3 BIO-101
CS-101 Intro. to Computer ScienceComp. Sci. 4
CS-190 Game Design Comp. Sci. 4 CS-101
CS-315 Robotics Comp. Sci. 3 CS-101
CS-319 Image Processing Comp. Sci. 3 CS-101
CS-347 Database System ConceptsComp. Sci. 3 CS-101
EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101
FIN-201 Investment Banking Finance 3
HIS-351 World History History 3
MU-199 Music Video Production Music 3
PHY-101 Physical Principles Physics 4
Mrs. Sunita M Dol
Page 9
Practice Problem Statements
Perform all join operation on the following relations
The employee and ft-works relations.
Perform all join operation on the following relations
References:
 Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan
(McGraw Hill International Edition) sixth edition.
 Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan
(McGraw Hill International Edition) fifth edition.
 http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/
 http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/
 http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/

9.Joins.pdf

  • 1.
    Mrs. Sunita MDol Page 1 Joined Relations JOINS are used to retrieve data from multiple tables. An Oracle JOIN is performed whenever two or more tables are joined in a SQL statement. There are 4 different types of joins:  INNER JOIN (or sometimes called simple join)  LEFT OUTER JOIN (or sometimes called LEFT JOIN)  RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)  FULL OUTER JOIN (or sometimes called FULL JOIN) INNER JOIN (simple join) Visual Illustration In this visual diagram, the INNER JOIN returns the shaded area: The INNER JOIN would return the records where table1 and table2 intersect. LEFT OUTER JOIN Another type of join is called an LEFT OUTER JOIN. This type of join returns all rows from the LEFT-hand table specified in the ON condition and only those rows from the other table where the joined fields are equal (join condition is met). Visual Illustration In this visual diagram, the LEFT OUTER JOIN returns the shaded area: The LEFT OUTER JOIN would return the all records from table1 and only those records from table2 that intersect with table1.
  • 2.
    Mrs. Sunita MDol Page 2 RIGHT OUTER JOIN Another type of join is called an RIGHT OUTER JOIN. This type of join returns all rows from the RIGHT-hand table specified in the ON condition and only those rows from the other table where the joined fields are equal (join condition is met). Visual Illustration In this visual diagram, the RIGHT OUTER JOIN returns the shaded area: The RIGHT OUTER JOIN would return the all records from table2 and only those records from table1 that intersect with table2. FULL OUTER JOIN Another type of join is called an FULL OUTER JOIN. This type of join returns all rows from the LEFT-hand table and RIGHT-hand table with nulls in place where the join condition is not met. Visual Illustration In this visual diagram, the FULL OUTER JOIN returns the shaded area: The FULL OUTER JOIN would return the all records from both table1 and table2. Each of the variants of the join operations in SQL consists of a join type and a join condition. The join condition defines which tuples in the two relations match and what attributes are present in the result of the join. The join type defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated. Figure 1 shows some of the allowed join types and join conditions. The first join type is the inner join, and the other three are the outer joins. Figure 1: Join types and join conditions.
  • 3.
    Consider the relationsfor joined relations Mrs. Sunita M Dol Consider the relations for joined relations Mrs. Sunita M Dol Page 3
  • 4.
    Mrs. Sunita MDol Page 4 Inner Join Using NATURAL condition select * from course; course_id title dept_namecredits BIO-101 Intro. to Biology Biology 4 BIO-301 Genetics Biology 4 BIO-399 Computational Biology Biology 3 CS-101 Intro. to Computer ScienceComp. Sci. 4 CS-190 Game Design Comp. Sci. 4 CS-315 Robotics Comp. Sci. 3 CS-319 Image Processing Comp. Sci. 3 CS-347 Database System ConceptsComp. Sci. 3 EE-181 Intro. to Digital Systems Elec. Eng. 3 FIN-201 Investment Banking Finance 3 HIS-351 World History History 3 MU-199 Music Video Production Music 3 PHY-101 Physical Principles Physics 4 select * from prereq; course_id prereq_id BIO-301 BIO-101 BIO-399 BIO-101 CS-190 CS-101 CS-315 CS-101 CS-319 CS-101 CS-347 CS-101 EE-181 PHY-101 select * from course natural join prereq;
  • 5.
    Mrs. Sunita MDol Page 5 course_id title dept_name credits prereq_id BIO-301 Genetics Biology 4 BIO-101 BIO-399 Computational Biology Biology 3 BIO-101 CS-190 Game Design Comp. Sci. 4 CS-101 CS-315 Robotics Comp. Sci. 3 CS-101 CS-319 Image Processing Comp. Sci. 3 CS-101 CS-347 Database System Concepts Comp. Sci. 3 CS-101 EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101 select * from course natural inner join prereq; course_id title dept_namecreditsprereq_id BIO-301 Genetics Biology 4 BIO-101 BIO-399 Computational Biology Biology 3 BIO-101 CS-190 Game Design Comp. Sci. 4 CS-101 CS-315 Robotics Comp. Sci. 3 CS-101 CS-319 Image Processing Comp. Sci. 3 CS-101 CS-347 Database System ConceptsComp. Sci. 3 CS-101 EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101 Using ON condition select * from course inner join prereq on course.course_id=prereq.course_id; course_id title dept_namecreditscourse_idprereq_id BIO-301 Genetics Biology 4 BIO-301 BIO-101 BIO-399 Computational Biology Biology 3 BIO-399 BIO-101 CS-190 Game Design Comp. Sci. 4 CS-190 CS-101
  • 6.
    Mrs. Sunita MDol Page 6 course_id title dept_namecreditscourse_idprereq_id CS-315 Robotics Comp. Sci. 3 CS-315 CS-101 CS-319 Image Processing Comp. Sci. 3 CS-319 CS-101 CS-347 Database System ConceptsComp. Sci. 3 CS-347 CS-101 EE-181 Intro. to Digital Systems Elec. Eng. 3 EE-181 PHY-101 Using USING condition select * from course inner join prereq using (course_id); course_id title dept_namecreditsprereq_id BIO-301 Genetics Biology 4 BIO-101 BIO-399 Computational Biology Biology 3 BIO-101 CS-190 Game Design Comp. Sci. 4 CS-101 CS-315 Robotics Comp. Sci. 3 CS-101 CS-319 Image Processing Comp. Sci. 3 CS-101 CS-347 Database System ConceptsComp. Sci. 3 CS-101 EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101
  • 7.
    Mrs. Sunita MDol Page 7 Left Outer Join Using NATURAL condition select * from course natural left outer join prereq; course_id title dept_namecreditsprereq_id BIO-101 Intro. to Biology Biology 4 BIO-301 Genetics Biology 4 BIO-101 BIO-399 Computational Biology Biology 3 BIO-101 CS-101 Intro. to Computer ScienceComp. Sci. 4 CS-190 Game Design Comp. Sci. 4 CS-101 CS-315 Robotics Comp. Sci. 3 CS-101 CS-319 Image Processing Comp. Sci. 3 CS-101 CS-347 Database System ConceptsComp. Sci. 3 CS-101 EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101 FIN-201 Investment Banking Finance 3 HIS-351 World History History 3 MU-199 Music Video Production Music 3 PHY-101 Physical Principles Physics 4 Using ON condition select * from course left outer join prereq on course.course_id = prereq.course_id; course_id title dept_namecreditscourse_idprereq_id BIO-101 Intro. to Biology Biology 4 BIO-301 Genetics Biology 4 BIO-301 BIO-101 BIO-399 Computational Biology Biology 3 BIO-399 BIO-101 CS-101 Intro. to Computer ScienceComp. Sci. 4 CS-190 Game Design Comp. Sci. 4 CS-190 CS-101 CS-315 Robotics Comp. Sci. 3 CS-315 CS-101
  • 8.
    Mrs. Sunita MDol Page 8 CS-319 Image Processing Comp. Sci. 3 CS-319 CS-101 CS-347 Database System ConceptsComp. Sci. 3 CS-347 CS-101 EE-181 Intro. to Digital Systems Elec. Eng. 3 EE-181 PHY-101 FIN-201 Investment Banking Finance 3 HIS-351 World History History 3 MU-199 Music Video Production Music 3 PHY-101 Physical Principles Physics 4 Using USING condition select * from course left outer join prereq using (course_id); course_id title dept_namecreditsprereq_id BIO-101 Intro. to Biology Biology 4 BIO-301 Genetics Biology 4 BIO-101 BIO-399 Computational Biology Biology 3 BIO-101 CS-101 Intro. to Computer ScienceComp. Sci. 4 CS-190 Game Design Comp. Sci. 4 CS-101 CS-315 Robotics Comp. Sci. 3 CS-101 CS-319 Image Processing Comp. Sci. 3 CS-101 CS-347 Database System ConceptsComp. Sci. 3 CS-101 EE-181 Intro. to Digital Systems Elec. Eng. 3 PHY-101 FIN-201 Investment Banking Finance 3 HIS-351 World History History 3 MU-199 Music Video Production Music 3 PHY-101 Physical Principles Physics 4
  • 9.
    Mrs. Sunita MDol Page 9 Practice Problem Statements Perform all join operation on the following relations The employee and ft-works relations. Perform all join operation on the following relations References:  Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill International Edition) sixth edition.  Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill International Edition) fifth edition.  http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/  http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/  http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/