Database Programming
Sections 3 – Oracle Joins
Marge Hohly 2
Obtaining Data from Multiple Tables: Using Joins
Marge Hohly 3
Types of Joins Oracle Proprietary
Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer Join Self join
SQL: 1999 Compliant Joins: Cross joins Natural joins Using clause Full or Two sided
outer joins Arbitrary join
conditions for outer
Marge Hohly 4
Joining Tables Using Oracle Syntax Use a join to query data from more than
one table. SELECT table1.column, table2.column
FROM table1, table2WHERE table1.column1 = table2.column2; Write the join condition in the WHERE clause. Prefix the column name with the table name
when the same column name appears in more than one table.
Marge Hohly 5
Cartesian Products A Cartesian product is formed when:
A join condition is omitted A join condition is invalid 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.
Marge Hohly 6
Generating a Cartesian Product SELECT last_name,department_name
FROM employees,departments Employees table has 20 rows Departments table has 8 rows Cartesian product: 20x8=160 rows
Run the query
Marge Hohly 7
What is an Equijoin? Employees
Departments
Employee_ID Department_ID
200 10201 20202 20124 50141 50
Department_ID Department_name
10 Administration
20 Marketing
20 Marketing
50 Shipping
50 Shipping
60 IT
Marge Hohly 8
Retrieving Records with Equijoins SELECT employees.employee_id, employees.last_name,
employees.department_id, departments.department_id, departments.location_idFROM employees,departmentsWHERE employees.department_id = departments.department_id;
19 rows retrieved in following example.
EMPLOYEE_ID LAST_NAME DEPARTMENT_ID DEPARTMENT_ID LOCATION_ID
100 King 90 90 1700
101 Kochhar 90 90 1700
102 De Haan 90 90 1700
103 Hunold 60 60 1400
104 Ernst 60 60 1400
107 Lorentz 60 60 1400
Marge Hohly 9
Using the AND Operator SELECT last_name, employees.department_id, department_name
FROM employees,departmentsWHERE employees.department_id = departments.department_id AND last_name = 'Matos';
LAST_NAMEDEPARTMENT_ID
DEPARTMENT_ID DEPARTMENT_NAME
Ernst 60 10 Administration
Lorentz 60 20 Marketing
Davies 50 50 Shipping
Matos 50 60 IT
Vargas 50 80 Sales
Marge Hohly 10
Ambiguous Column Names SELECT last_name, department_id,
location_idFROM employees, departments;
Run the above Note that both tables contain
department_id column and thus the query is ambiguous
Marge Hohly 11
Qualifying Ambiguous Column Names Use table prefixes to qualify column
names that are in multiple tables. Improve performance by using table
prefixes. Distinguish columns that have
identifical names but reside in different tables by using column aliases.
Marge Hohly 12
Generating a Cartesian Product SELECT last_name, d.department_id,
location_idFROM employees e, departments d; Employees table has 20 rows Departments table has 8 rows Cartesian product: 20x8=160 rows
Run the query
Marge Hohly 13
Using Tables Aliases Simplify queries by using tables aliases. Improve performance by using table
prefixes. SELECT e.employee_id, e.last_name,
e.department_id, d.department_id, d.location_idFROM employees e,departments dWHERE e.department_id = d.department_id;
Marge Hohly 14
Joining More than Two Tables To join n tables together, you need a minimum of n-1 join
conditions. For example, to join three tables, a minimum of two joins is required.
LAST_NAMEDEPARTMENT_ID
DEPARTMENT_ID DEPARTMENT_NAME Location_id City
Ernst 60 10 Administration 1400 Southlake
Lorentz 60 20 Marketing 1700 Seattle
Davies 50 50 Shipping 1800 Toronto
Matos 50 60 IT 2500 Oxford
Vargas 50 80 Sales
Marge Hohly 15
Three Table Join Example SELECT e.last_name, d.department_name, l.city
FROM employees e,departments d, locations lWHERE e.department_id = d.department_id AND d.location_id = l.location_id;
Marge Hohly 16
Non-EquijoinsLAST_NAME
SALARY
Ernst 6000
Lorentz 4200
Davies 3100
Matos 2600
Vargas 2500
Ziotkey 10500
Salary in the EMPLOYEES table must be between lowest salary and highest salary in the JOB_GRADES table.
EMPLOYEES table JOB_GRADES table
GPA LOWEST_SAL HIGHEST_SAL
A 1000 2999
B 3000 5999
C 6000 9999
D 10000 14999
E 15000 24999
F 25000 40000
Marge Hohly 17
Retrieving Records with Non-Equijoins Run the following query: SELECT e.last_name, e.salary,
j.grade_levelFROM employees e, job_grades jWHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal;
Marge Hohly 18
Retrieving Records with Non-Equijoins SELECT e.last_name, e.salary
FROM employees e, job_grades jWHERE e.salary > j.lowest_sal AND e.salary < 5800;
Marge Hohly 19
Self Joins Manager_id in the Worker table is equal to
Employee_id in the Manager table. Employees (Worker) Employees (Manager)
Employee_id LAST_NAMEManager_ID
Employee_id LAST_NAME
100 King 100 King
101 Kochhar 100 101 Kochhar
102 De Haan 100 102 De Haan
103 Hunold 102 103 Hunold
104 Ernst 103 104 Ernst
107 Lorentz 103 107 Lorentz
Marge Hohly 20
Joining a table to itself example SELECT worker.last_name || ' works for '
|| manager.last_nameFROM employees worker, employees managerWHERE worker.manager_id = manager.employee_id;
Run the above to see results.
Marge Hohly 21
Outer Joins There are no employees in department 190. Departments table Employees table
DEPARTMENT_NAMEDEPARTMENT_ID
DEPARTMENT_ID LAST_NAME
Administration 10 90 King
Marketing 20 90 Kochhar
Shipping 50 90 De Haan
IT 60 60 Hunold
Sales 80 60 Ernst
Contracting 190 50 Rajs
Marge Hohly 22
Outer Join example SELECT d.department_name,
e.last_nameFROM departments d, employees eWHERE d.department_id = e.department_id AND d.department_id = 190;
Run this and you will see there are no employees in the 190 department.
Marge Hohly 23
Outer Join Syntax You use an outer join to also see rows
that do not meet the join condition. The Outer join operator is the plus
sign (+). SELECT table1.column, table2.column
FROM table1, table2,WHERE table1.column (+) = table2.column;
SELECT table1.column, table2.columnFROM table1, table2,WHERE table1.column = table2.column (+);
Marge Hohly 24
Using Outer Joins - examples SELECT e.last_name, e.department_id,
d.department_nameFROM employees e, departments dWHERE e.department_id(+) = d.department_id;
Top Related