Database Programming Sections 3 – Joins & Hierarchical Queries.

Post on 31-Dec-2015

240 views 5 download

Transcript of Database Programming Sections 3 – Joins & Hierarchical Queries.

Database Programming

Sections 3 – Joins & Hierarchical Queries

Marge Hohly 2

Overview 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 joins

Marge Hohly 3

Natural Join

ANSI/ISO SQL: 1999 Join equivalent of an equijoin

Join on all common columnsie. columns with same name and data type

SYNTAX:SELECT field1, field2FROM table1 NATURAL JOIN table2WHERE fieldn = value;

Marge Hohly 4

Example Natural Join

SELECT event_id, song_id, cd_numberFROM d_play_list_items NATURAL JOIN d_track_listingsWHERE event_id = 105;

Example Natural Join

SELECT first_name, last_name, event_date, descriptionFROM d_clients NATURAL JOIN d_events;

Marge Hohly 5

FIRST_NAME LAST_NAME EVENT_DATE DESCRIPTION

Hiram Peters 14-May-04 Party for 200, red, white, blue motif

Lauren Vigil 28-Apr-04 Black tie at Four Season hotel

Marge Hohly 6

Example

Use an equijoin between the DJs on Demand database tables, d_songs and d_types. Display the type code, description and title. Limit the rows returned to those type codes between 70 and 80.

Marge Hohly 7

Cross Join

ANSI/ISO SQL: 1999 syntax for achieving of a Cartesian product

Syntax: SELECT *

FROM employeesCROSS JOIN departments;

Last section Cartesian Product:SELECT *FROM employees, departments;

Marge Hohly 8

Join Using ANSI/ISO SQL: 1999 join condition used to

join two tables on only one column Typically used where NATURAL JOIN cannot

be used because there are other common columns with same name and different data types

No table name or alias can be used on the referenced column anywhere in the USING statement

Marge Hohly 9

Join Using SYNTAX:

SELECT field1, field2, field3FROM table1JOIN table2USING(column name);

Example:SELECT e.employee_id, e.last_name, d.location_idFROM employees eJOIN departments dUSING(department_id);

Marge Hohly 10

Example SELECT e.employee_id,

e.last_name, d.location_idFROM employees e JOIN departments dUSING(department_id);

Marge Hohly 11

JOIN ON ANSI/ISO: 1999 join clause that may be used to specify the

condition or columns used: Syntax:

SELECT field1, field2, field3FROM table1 JOIN table2ON(table1.fieldx=table2.fieldy);

SELECT e.last_name emp, m.last_name mgrFROM employees e JOIN employees mON(e.manager_id = m.employee_id);

SELECT e.last_name as "EMP", w.last_name as "MGR“FROM employees e JOIN employees wON (e.manager_id = w.employee_id)WHERE e.last_name like 'H%';

Where clause can limit results.

Marge Hohly 12

Example of JOIN ON SELECT e.last_name,

e.department_id, d.department_nameFROM employees e JOIN departments dON (e.department_id = d.department_id);

Marge Hohly 13

Summary/Comparison

Marge Hohly 14

Marge Hohly 15

Examples2. Join DJs on Demand d_play_list_items, d_track_listings,

and d_cds tables with the JOIN USING syntax. Include the song ID, CD number, title, and comments in the output.

3. Display the city, department name, location ID, and department ID for departments 10, 20, and 30 for the city of Seattle.

6. Display job title, employee first name, last name, and email for all employees that are stock clerks.

9. (Use Join On) Query and display manager ID, department ID, department name, first name, and last name for all employees in departments 80, 90, 110, and 190.

Marge Hohly 16

Three-way Joins with the ON clause

A three-way join is the join of three tables Syntax:

SELECT employee_id, city, department_nameFROM employees eJOIN departments dON (d.department_id = e.department_id)JOIN locations lON (d.location_id=l.location_id);

Marge Hohly 17

Example SELECT employee_id, city,

department_nameFROM employees eJOIN departments dON (d.department_id = e.department_id)JOIN locations lON (d.location_id=l.location_id);

Marge Hohly 18

Revised example SELECT e.employee_id,

l.city, d.department_nameFROM employees e, departments d, locations lWHERE e.department_id = d.department_idAND d.location_id = l.location_id;

Marge Hohly 19

INNER JOINS

An inner join returns only those rows that match the join condition

SELECT e.last_name, e.department_id, d.department_nameFROM employees eJOIN departments dON (e.department_id = d.department_id);

Marge Hohly 20

OUTER JOIN

Outer joins return those rows that match the join condition and those that do not

There are three ANSI/ISO SQL: 1999 outer joins: LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN

Marge Hohly 21

LEFT OUTER JOIN SELECT e.last_name, e.department_id, d.department_name

FROM employees eLEFT OUTER JOIN departments dON(e.department_id=d.department_id);

SQL 99 equivalent:

SELECT e.last_name, e.department_id, d.department_nameFROM employees e, departments dWHERE e.department_id=d.department_id(+);

This statement will return those employees who do not have a department_id

Marge Hohly 22

RIGHT OUTER JOIN SELECT field1, field2 ....

FROM table1 aRIGHT OUTER JOIN table2 bON (a.field=b.field);

or USING(field name);

SELECT e.last_name, e.department_id, d.department_nameFROM employees eRIGHT OUTER JOIN departments dON(e.department_id=d.department_id);

This statement will return those departments who do not have any employees in them

Marge Hohly 23

FULL OUTER JOIN The FULL OUTER JOIN returns both matched and all

unmatched rows Syntax:

SELECT field1, field2, field3FROM table1 aFULL OUTER JOIN table2 bON a.field=b.field;

SELECT e.last_name, e.department_id, d.department_nameFROM employees eFULL OUTER JOIN departments dON(e.department_id=d.department_id);

There is no direct comparable Oracle specific join

Marge Hohly 24

Example Construct a join to display a list of Global

Fast Foods customers whether or not they have placed an order as yet and all the customers who have placed orders.

SELECT c.first_name, c.last_name, o.order_number,o.order_date, o.order_totalFROM   f_customers cLEFT OUTER JOIN f_orders oON   (c.id = o.cust_id);

Marge Hohly 25

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 joins

On clause

Self-Join

Marge Hohly 26

Self-JoinEmployee_id last_name manager_id employee_id last_name

100 King 100 King

101 Kochar 100 101 Kochar

102 De Haan 100 102 De Haan

103 Hunold 102 103 Hunold

104 Ernst 103 104 Ernst

107 Lorentz 103 107 Lorentz

124 Mourgos 100 124 Mourgos

Marge Hohly 27

Manager_id in worker table = employee_id in Manager table

EMPLOYEES (worker) table EMPLOYEES (manager) table

Example SELECT chair.last_name || ‘ is the section chair of ‘ ||

player.last_nameFROM band_members chair, band_members playerWHERE player.chair_id = chair.member_id;

Marge Hohly 28

Hierarchical Queries

Similar to the self-join Similar to an Organization Chart Shows company, department , family

structure etc.

Marge Hohly 29

Using Hierarchical Queries

Retrieve data based on natural hierarchical relationships between rows

If data within a single table called tree walking

Marge Hohly 30

Hierarchical queries

Marge Hohly 31

EMPLOYEES as an organization chart

Kochar De Haan Mourgos

Hunold Rajs

Ernst

King

Hierarchical queries keywords

START WITH CONNECT BY PRIOR LEVEL

Marge Hohly 32

Hierarchical queries keyword example

Marge Hohly 33

Another example

SELECT last_name || ' reports to ' ||PRIOR last_name "Walk Top Down“FROM employeesSTART WITH last_name = 'King‘CONNECT BY PRIOR employee_id = manager_id;

Marge Hohly 34

Results of query

Walk Top Down

King reports to

Kochhar reports to King

Whalen reports to Kochhar

Higgins reports to Kochhar

Gietz reports to Higgins

De Haan reports to King

Hunold reports to De Haan

Ernst reports to Hunold

Lorentz reports to Hunold

Marge Hohly 35

Marge Hohly 36

Hierarchical query report

SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_') AS ORG_CHARTFROM employeesSTART WITH last_name = 'King‘CONNECT BY PRIOR employee_id = manager_id;

Marge Hohly 37

Hierarchical queries pruning

Delete branches of tree with WHERE clause or CONNECT BY PRIOR clause

WHERE - only row named is excluded CONNECT BY PRIOR – entire branch is

excluded View example on next slide

Marge Hohly 38

Pruning Example

SELECT last_nameFROM employeesWHERE last_name <> 'Higgins‘START WITH last_name = 'Kochhar‘CONNECT BY PRIOR employee_id = manager_id;

Only the branch under King is returned

Marge Hohly 39

Pruning Example

SELECT last_nameFROM employeesSTART WITH last_name = 'Kochhar‘CONNECT BY PRIOR employee_id = manager_idAND last_name <> 'Higgins';

Marge Hohly 40