Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins.

Similar presentations


Presentation on theme: "Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins."— Presentation transcript:

1 Database Programming Sections 3 – Oracle Joins

2 Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins

3 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

4 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.

5 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.

6 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

7 Marge Hohly7 What is an Equijoin?  Employees  Departments Employee_ID Department_ID 20010 20120 20220 12450 14150 Department_IDDepartment_name 10 Administration 20 Marketing 20 Marketing 50 Shipping 50 Shipping 60 IT

8 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 100King90 1700 101Kochhar90 1700 102De Haan90 1700 103Hunold60 1400 104Ernst60 1400 107Lorentz60 1400

9 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

10 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

11 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.

12 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

13 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;

14 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

15 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;

16 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 A1000 2999 B3000 5999 C6000 9999 D10000 14999 E15000 24999 F25000 40000

17 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;

18 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;

19 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 Kochhar100 101 Kochhar 102 De Haan100 102 De Haan 103 Hunold102 103 Hunold 104 Ernst103 104 Ernst 107 Lorentz103 107 Lorentz

20 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.

21 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

22 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.

23 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 (+);

24 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;


Download ppt "Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins."

Similar presentations


Ads by Google