Database Programming Sections 3 – Oracle Joins
Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins
Marge Hohly3 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 Hohly4 Joining Tables Using Oracle Syntax Use a join to query data from more than one table. SELECT table1.column, table2.column FROM table1, table2 WHERE 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 Hohly5 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 Hohly6 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 Hohly7 What is an Equijoin? Employees Departments Employee_ID Department_ID Department_IDDepartment_name 10 Administration 20 Marketing 20 Marketing 50 Shipping 50 Shipping 60 IT
Marge Hohly8 Retrieving Records with Equijoins SELECT employees.employee_id, employees.last_name, employees.department_id, departments.department_id, departments.location_id FROM employees,departments WHERE employees.department_id = departments.department_id; 19 rows retrieved in following example. EMPLOYEE_IDLAST_NAMEDEPARTMENT_ID LOCATION_ID 100King Kochhar De Haan Hunold Ernst Lorentz
Marge Hohly9 Using the AND Operator SELECT last_name, employees.department_id, department_name FROM employees,departments WHERE employees.department_id = departments.department_id AND last_name = 'Matos'; LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME Ernst6010Administration Lorentz6020Marketing Davies50 Shipping Matos5060IT Vargas5080Sales
Marge Hohly10 Ambiguous Column Names SELECT last_name, department_id, location_id FROM employees, departments; Run the above Note that both tables contain department_id column and thus the query is ambiguous
Marge Hohly11 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 Hohly12 Generating a Cartesian Product SELECT last_name, d.department_id, location_id FROM employees e, departments d; Employees table has 20 rows Departments table has 8 rows Cartesian product: 20x8=160 rows Run the query
Marge Hohly13 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_id FROM employees e,departments d WHERE e.department_id = d.department_id;
Marge Hohly14 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_NAME DEPARTMENT_ID DEPARTMENT_NAME Location_idCity Ernst6010Administration 1400Southlake Lorentz6020Marketing 1700Seattle Davies50 Shipping 1800Toronto Matos5060IT 2500Oxford Vargas5080Sales
Marge Hohly15 Three Table Join Example SELECT e.last_name, d.department_name, l.city FROM employees e,departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id;
Marge Hohly16 Non-Equijoins LAST_NAME SALARY Ernst6000 Lorentz4200 Davies3100 Matos2600 Vargas2500 Ziotkey10500 Salary in the EMPLOYEES table must be between lowest salary and highest salary in the JOB_GRADES table. EMPLOYEES table JOB_GRADES table GPALOWEST_SALHIGHEST_SAL A B C D E F
Marge Hohly17 Retrieving Records with Non-Equijoins Run the following query: SELECT e.last_name, e.salary, j.grade_level FROM employees e, job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal;
Marge Hohly18 Retrieving Records with Non-Equijoins SELECT e.last_name, e.salary FROM employees e, job_grades j WHERE e.salary > j.lowest_sal AND e.salary < 5800;
Marge Hohly19 Self Joins Manager_id in the Worker table is equal to Employee_id in the Manager table. Employees (Worker) Employees (Manager) Employee_idLAST_NAME Manager_ID Employee_idLAST_NAME 100 King 100 King 101 Kochhar Kochhar 102 De Haan De Haan 103 Hunold Hunold 104 Ernst Ernst 107 Lorentz Lorentz
Marge Hohly20 Joining a table to itself example SELECT worker.last_name || ' works for ' || manager.last_name FROM employees worker, employees manager WHERE worker.manager_id = manager.employee_id; Run the above to see results.
Marge Hohly21 Outer Joins There are no employees in department 190. Departments table Employees table DEPARTMENT_NAME DEPARTMENT_ID LAST_NAME Administration1090King Marketing2090Kochhar Shipping5090De Haan IT60 Hunold Sales8060Ernst Contracting19050Rajs
Marge Hohly22 Outer Join example SELECT d.department_name, e.last_name FROM departments d, employees e WHERE 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 Hohly23 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.column FROM table1, table2, WHERE table1.column = table2.column (+);
Marge Hohly24 Using Outer Joins - examples SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id(+) = d.department_id;