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

Slides:



Advertisements
Similar presentations
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
Advertisements

Chapter 4 Joining Multiple Tables
Displaying Data from Multiple Tables
4-1 Copyright  Oracle Corporation, All rights reserved. Types of Joins Equijoin Non-equijoin Outer join Self join.
Notice that No where clause In the syntax.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
 After completing this lesson, you should be able to do the following: ◦ Interpret the concept of a hierarchical query ◦ Create a tree-structured report.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
1 Copyright © 2009, Oracle. All rights reserved. B Table Descriptions.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Produced By © Information System Department Introduction to SQL 1 Mr. Anser Ghazzaal Lecturer,Information System Department.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
Sections 3 – Joins & Hierarchical Queries
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
Basisdata Pertanian. After completing this lesson, you should be able to do the following:  Write SELECT statements to access data from more than one.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
JDeveloper 10g and Oracle ADF Business Components Getting the Most Out of Your Data Avrom Roy-Faderman Senior Programmer November, 2005.
Copyright © 2004, Oracle. All rights reserved. D ISPLAYING D ATA FROM M ULTIPLE T ABLES.
4 Copyright © 2007, Oracle. All rights reserved. Manipulating Large Data Sets.
4 Displaying Data from Multiple Tables Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
CSED421 Database Systems Lab Join. Human Resources (HR) schema.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Special Joins Week 4. Objective –Write SELECT statements to display left, right and full outer joins.
Join CSED421 Database Systems Lab. Joining Tables Use a join to query data from more than one table. SELECTtable1.column, table2.column FROMtable1, table2.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
7 Copyright © 2004, Oracle. All rights reserved. Hierarchical Retrieval.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Joins, Subqueries, CTE and Indices
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables Using Joins
多表查询 Schedule: Timing Topic 55 minutes Lecture 55 minutes Practice
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Oracle Join Syntax.
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

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;