SQL Part 2: Kal@ittelkom - Ac.id
SQL Part 2: Kal@ittelkom - Ac.id
SQL Part 2: Kal@ittelkom - Ac.id
Outline
Obtaining Data from Multiple Tables Cartesian Product Types of Join
Inner Join Outer Join Self Join
Cartesian Products
When a join condition is invalid or omitted completely, the result is a Cartesian product, in which all combinations of rows are displayed.
All rows in the first table are joined to all rows in the second table
To avoid a Cartesian product, always include a valid join condition in a WHERE clause.
Types of Join
Inner Join
Equijoin Natural Join Cross Join
Outer Join
Left Outer Join Right Outer Join Full Outer Join
Self Join
What is an Equijoin?
EMPLOYEES
DEPARTMENTS
Foreign key
Primary key
EQUIJOIN
To determine an employees department name, you compare the value in the DEPARTMENT_ID column in the EMPLOYEES table with the DEPARTMENT_ID values in the DEPARTMENTS table. The relationship between the EMPLOYEES and DEPARTMENTS tables is an equijointhat is, values in the DEPARTMENT_ID column on both tables must be equal. Note: Equijoins are also called simple joins or inner joins.
Non-Equijoins
EMPLOYEES JOB_GRADES
Salary in the EMPLOYEES table must be between lowest salary and highest salary in the JOB_GRADES table.
Natural Joins
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. The join can happen only on columns having the same names and data types in both the tables. If the columns have the same name, but different data types, then the NATURAL JOIN syntax causes an error.
Cross Joins
The CROSS JOIN clause produces the cross-product of two tables. This is the same as a Cartesian product between the two tables.
Outer Joins
DEPARTMENTS EMPLOYEES
Outer Join
An outer join does not require each record in the two joined tables to have a matching record. The joined table retains each record even if no other matching record exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins, depending on which table(s) one retains the rows from (left, right, or both).
Additional Conditions
SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id) AND e.manager_id = 149 ;
Self Join
Sometimes you need to join a table to itself. To find the name of each employees manager, you need to join the EMPLOYEES table to itself, or perform a self join. For example, to find the name of Whalens manager, you need to:
Find Whalen in the EMPLOYEES table by looking at the LAST_NAME column. Find the manager number for Whalen by looking at the MANAGER_ID column. Whalens manager number is 101. Find the name of the manager with EMPLOYEE_ID 101 by looking at the LAST_NAME column. Kochhars employee number is 101, so Kochhar is Whalens manager.
In this process, you look in the table twice. The first time you look in the table to find Whalen in the LAST_NAME column and MANAGER_ID value of 101. The second time you look in the EMPLOYEE_ID column to find 101 and the LAST_NAME column to find Kochhar.
Self Joins
EMPLOYEES (WORKER) EMPLOYEES (MANAGER)
Review
Obtaining Data from Multiple Tables Cartesian Product Types of Join
Inner Join Outer Join Self Join