Lecture 2 of Advanced Databases
Advanced SQL
Instructor: Mr.Ahmed Al Astal
Page 2
Advanced SQL
SQL Join Operations
Inner Joins.
Outer Joins
Self Joins.
Agenda
Page 3
Advanced SQL
An SQL JOIN clause combines records from two tables in a database.
A JOIN is a means for combining fields from two tables by using values common to each.
In Relational Algebra, the join operation is one of the most useful operations in and is the most commonly used way to combine information from two or more relations.
• Join can be defined as a cross-product followed by selections and projections
Join Operations
Page 4
Advanced SQL
All subsequent explanations on join types in this Slides make use of the following two tables.
.
Join Operations
The rows in these tables serve to illustrate the effect of different types of joins and join-predicates. In these tables, Department.DepartmentID is the primary key, while Employee.DepartmentID is a foreign key
Page 5
Advanced SQL
An inner join essentially combines the records from two tables (A and B) based on a given join-predicate.
The result of the join can be defined as the outcome of:
First taking the Cartesian product (or cross-join) of all records in the tables (combining every record in table A with every record in table B)
Then return all records which satisfy the join predicate.
SQL specifies two different syntactical ways to express joins:
1. The first, called "explicit join notation", uses the keyword JOIN
2. The second uses the "implicit join notation".
Inner join
Page 6
Advanced SQL
Example of an explicit inner join (see the results at the notes page):
Example of an implicit inner join:
Join Examples
Page 7
Advanced SQL
1. Equi-join.An equi-join, also known as an equijoin, is a specific type of comparator-based join, or theta join, that uses only equality comparisons in the join-predicate.
SQL provides optional syntactic sugar for expressing equi-joins, by way of the USING construct
Types of inner joins
Page 8
Advanced SQL
2. Natural joinA natural join offers a further specialization of equi-joins. The join predicate arises implicitly by comparing all columns in both tables that have the same column-name in the joined tables.
The resulting joined table contains only one column for each pair of equally-named columns.
The previous sample query for inner joins can be expressed as a natural join in the following way:
Types of inner joins
See the results at the notes page
Page 9
Advanced SQL
3. Cross joinA cross join, cartesian join or product returns the cartesian product of the sets of records from the two joined tables.
Thus, it equates to an inner join where the join-condition always evaluates to True or join-condition is absent in statement.
Types of inner joins
explicit cross join
explicit cross join
Page 10
Advanced SQL
An outer join does not require each record in the two joined tables to have a matching record.
An outer join selects rows from both tables including rows from one or both tables without matching rows in the other table
Outer joins subdivide further into
Left outer joins
Right outer joins
And full outer joins
Outer joins
Page 11
Advanced SQL
The result of a left outer join (or simply left join) for table A and B is all rows from the left table (A) plus all matching rows from the right table (B).
Column values from the right table are replaced with null values when the matching right-side row does not exist in the left-side table.
1) Left outer join
Page 12
Advanced SQL
For example, this allows us to find an employee's department, but still to show the employee even when their department does not exist (contrary to the inner-join example, where employees in non-existent departments are excluded from the result).
See the results at the note page
1) Left outer join (Cont.)
Page 13
Advanced SQL
A right outer join returns all the values from the right table and matched values from the left table (NULL in case of no matching join predicate).
2) Right outer join
Page 14
Advanced SQL
For example, this allows us to find each employee and his or her department, but still show departments that have no employees.)
See the results at the note page
2) Right outer join (Cont.)
Page 15
Advanced SQL
A full outer join combines the results of both left and right outer joins. The joined table will contain all records from both tables, and fill in NULLs for missing matches on either side.
3) Full outer join
Page 16
Advanced SQL
For example, this allows us to see each employee who is in a department and each department that has an employee, but also see each employee who is not part of a department and each department which doesn't have an employee.
See the results at the note page
3) Full outer join (Cont.)
Page 17
Advanced SQL
A self-join is joining a table to itself. This is best illustrated by the following example. Consider that all the employee information is contained within a single large table.
Considering a modified Employee table such as the following:
Self-join