Chapter 4 Intermediate SQL Joins Chapter 4 Intermediate

  • Slides: 16
Download presentation
Chapter 4: Intermediate SQL Joins

Chapter 4: Intermediate SQL Joins

Chapter 4: Intermediate SQL n Join Expressions n Views n Transactions n Integrity Constraints

Chapter 4: Intermediate SQL n Join Expressions n Views n Transactions n Integrity Constraints n SQL Data Types and Schemas n Authorization

JOINS

JOINS

Job Interview Question

Job Interview Question

Joined Relations n Join operations take two relations and return as a result another

Joined Relations n Join operations take two relations and return as a result another relation. n A join operation is a Cartesian product which requires that tuples in the two relations match (under some condition). It also specifies the attributes that are present in the result of the join n The join operations are typically used as subquery expressions in the from clause

Types of Joins n [Inner] Join n [Natural] Outer Joins l [Natural] Left Outer

Types of Joins n [Inner] Join n [Natural] Outer Joins l [Natural] Left Outer Join l [Natural] Right Outer Join l [Natural] Full Outer Join

A Picture is Worth…

A Picture is Worth…

Recall Join - Using n select name, title from (instructor natural join teaches) join

Recall Join - Using n select name, title from (instructor natural join teaches) join course using(course_id);

Join operations – Example n Relation course n Relation prereq n Observe that prereq

Join operations – Example n Relation course n Relation prereq n Observe that prereq information is missing for CS-315 and course information is missing for CS-347

Outer Join n An extension of the join operation that avoids loss of information.

Outer Join n An extension of the join operation that avoids loss of information. n Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join. n Uses null values. n Example query l Find all courses without a prerequisite.

Left Outer Join n select * course natural left outer join prereq n select

Left Outer Join n select * course natural left outer join prereq n select * from course natural left outer join prereq

Right Outer Join n select * course natural right outer join prereq

Right Outer Join n select * course natural right outer join prereq

Joined Relations n Join operations take two relations and return as a result another

Joined Relations n Join operations take two relations and return as a result another relation. n These additional operations are typically used as subquery expressions in the from clause n Join condition – defines which tuples in the two relations match, and what attributes are present in the result of the join. n 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.

Full Outer Join n course natural full outer join prereq

Full Outer Join n course natural full outer join prereq

Joined Relations – Examples n course inner join prereq on course_id = prereq. course_id

Joined Relations – Examples n course inner join prereq on course_id = prereq. course_id n What is the difference between the above, and a natural join? n course left outer join prereq on course_id = prereq. course_id

Joined Relations – Examples n course natural right outer join prereq n course full

Joined Relations – Examples n course natural right outer join prereq n course full outer join prereq using (course_id)