ansi_join

Upload: belgaum

Post on 07-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 ansi_join

    1/17

    SQL Complex Joins

    & ANSI ISO 1999 Join Syntax

    Northern Arizona University

    College of Business

  • 8/6/2019 ansi_join

    2/17

    Queries Using Multiple Aliases for a Table

    The query processing software of a relational DBMS is designed

    to take only a single pass through each table appearing in a

    select statement.

    If multiple passes are needed, we may use a sub select

    the first sub select example we used was an example of this. We

    went through the Flight table once to determine an average fare.

    Then the outer select looked through the flight table again

    comparing individual rows to this average.

    Alternatively, we may include the same table more than once ina select statement by using different aliases.

    When multiple aliases for a table are created each is treated as a

    separate table when the select statement is executed and we can,

    in effect, search through the same table more than once.

  • 8/6/2019 ansi_join

    3/17

    Multiple table Alias Example -

    The Dual Relationship Between Airports and Flights The airline database provides a classic example of a case

    where multiple aliases for a table are needed. There is a dual

    relationship between Airport and Flight. That is, each Flight has

    a relationship to two different Airports, one that it comes form,

    and one that is going to. Note that a standard where clause to join the Airport and Flight

    tables would not work here. There is no single row of the Airport

    table which matches both origin and destination.

    What is needed is two copies of the airport table, one

    associated with the origin airport and the other associated withthe destination airport.

    The next slide shows this for a query where we wanted the

    airport code and full airport name for both the origin and

    destination airport.

  • 8/6/2019 ansi_join

    4/17

    Select with Multiple Copies of the Airport Table

    1 select flight_no, orig, oa.air_location as orig_airport_name,

    2 dest, da.air_location as dest_airport_name

    3 from airport oa, airport da, flight f

    4 where oa.air_code = orig

    5* and da.air_code = dest

    FLIGHT_NO ORI ORIG_AIRPORT_NAME DES DEST_AIRPORT_NAME

    --------- --- --------------------------- --- --------------------------

    101 FLG Flagstaff AZ PHX Phoenix AZ

    102 PHX Phoenix AZ MPS Minneapolis/St Paul MN

    103 MPS Minneapolis/St Paul MN PHX Phoenix AZ

    104 PHX Phoenix AZ FLG Flagstaff AZ

    15 PHX Phoenix AZ LAX Los Angeles CA

    17 PHX Phoenix AZ LAX Los Angeles CA

    31 PHX Phoenix AZ LAX Los Angeles CA

    . . . . .

    The select statement below creates the

    logical relationships shown to the right.

    The 2 copies of the AIRPORT table

    appear only in temporary storage as the

    select statement is executed. Physical

    storage of the AIRPORT table in the database

    is unaffected.

    AIRPORT

    OAAIRPORT

    DA

    FLIGHT

    origin destination

  • 8/6/2019 ansi_join

    5/17

    supervised by >

    Using Multiple Table Aliases -

    An Intra-table Relationship Example

    Multiple copies of a table are also often needed when a table contains

    an intra-table relationship (some rows of the table have a relationship to

    other rows of the same table).

    The example below shows an employee table with this type ofrelationship. The Sup_ID column is used to link an employees row to

    the row of the employees supervisor.

    An employee is supervised by at most one other employee, while a

    supervisory employee can supervise more than one employees

    E_ID E_NAME HIRE_DATE SALARY SUP_ID

    ---- --------------- --------- --------- ----5678 Gates 10-JAN-91 80000000

    3456 Jones 12-FEB-03 80000 5678

    4567 Smythe 18-MAR-98 75000 5678

    1234 Adams 14-OCT-04 32500 3456

    2345 Bates 12-DEC-99 35000 3456

    6789 Lewis 12-NOV-02 38000 4567

    7890 Earle 08-OCT-02 38000 4567

    EMPLOYEE

    < supervises

  • 8/6/2019 ansi_join

    6/17

    Intra-Table Relationship Query Example

    Based on the table on the previous slide, to retrieve

    information about each employees supervisor,

    their name for example, along with information

    about the employee, we would need to create an

    alias (S) for the employee table to search for rows

    corresponding to the supervisor of the employeefound in the original (E) copy of the table.

    Employee

    S

    Employee

    E

    1 select e.e_id, e.e_name, s.e_id as super_id, s.e_name as super_name

    2 from employee e, employee s

    3* where e.sup_id = s.e_id

    E_ID E_NAME SUPER SUPER_NAME---- --------------- _ID ---------------

    3456 Jones 5678 Gates

    4567 Smythe 5678 Gates

    1234 Adams 3456 Jones

    2345 Bates 3456 Jones

    6789 Lewis 4567 Smythe

    7890 Earle 4567 Smythe

  • 8/6/2019 ansi_join

    7/17

    In-line View or Derived Table

    in the FROM

    Clause

    Oracle (and IBMs DB2) now support the use

    of in-line views

    An in-line view is just a sub-select that isplaced in the FROM tables component of a

    query and given an alias

    Sub-select is performed first and its results

    treated as a temporary table when executing

    the outer select

  • 8/6/2019 ansi_join

    8/17

    In-line view example

    select flight_no, orig, f.dest, fare, meal fromflight f, (select dest, min(fare) as min_fare from flight

    where orig = 'PHX' group by dest) fm

    where orig = 'PHX'

    and f.dest = fm.destand fare = fm.min_fare

    DEST MIN_FARE

    LAX 49

    SFO 109

    FLG 48.5

    MPS 156FLIGHT_NO ORIG DEST FARE MEAL

    102 PHX MPS 156 L

    104 PHX FLG 48.5 S

    15 PHX LAX 49 B31 PHX LAX 49 S

    33 PHX LAX 49 S

    35 PHX LAX 49 S

    40 PHX LAX 49

    600 PHX SFO 109 B

    604 PHX SFO 109 B

    606 PHX SFO 109 L

  • 8/6/2019 ansi_join

    9/17

    ANSI ISO Join Operators

    The ANSI standard for SQL was

    modified in 1999 to provide support for

    alternative syntax for a number of SQL

    statement clauses

    Oracle implemented most of these

    extensions beginning with version 9i(we are using version 10g)

  • 8/6/2019 ansi_join

    10/17

    ANSI ISO Join Available in 9i+

    FROM table1 JOIN table2ONjoin_condition

    Replaces the join condition in the WHERE

    clause as well as the FROM E.g. FROM flight f JOIN ticket t ON f.flight_no =

    t.flight_no

    Use of table aliasing to resolve column

    names is unchanged E.G Select f.flight_no, orig, flight_date

    from flight f join ticket t

    on f.flight_no = t.flight_no

    where orig = PHX;

  • 8/6/2019 ansi_join

    11/17

    Multi-table joins using ON Clause

    Add additional table name and onclause

    in sequence. E.g.SELECT *

    FROM flight f join ticket t on f.flight_no = t.flight_no

    join passenger p on p.itinerary_no=t.itinerary_no

    where f.flight_no = 101;

  • 8/6/2019 ansi_join

    12/17

    ANSI ISO Natural JOIN

    When Performing an equi-join on tables havingexactly one common column (the same name and data type and length

    in each table)

    Can use the phrase NATURAL JOIN and skip the joincondition E.g. Select *

    FROM flight f natural join ticket t;

    Or - for multi-table joins

    Select flight_no, orig, dest, flight_date, itinerary_no,

    Pass_namefrom flight natural join (ticket natural join passenger)

    NOTES: aliases not need for the key columns,

    primary keys of all tables must be included in column listfor multi-table joins

  • 8/6/2019 ansi_join

    13/17

    Equi - Join Concept

    The joins we have used thus fare areequi-joins, an equi-join

    Produces a row of output only when a

    match value for the condition is found ineach table.

    But what if there are rows in one of thetables that have no matching values in the

    related table? When an equi-join is used that unmatched

    row of data is not included in the output.

  • 8/6/2019 ansi_join

    14/17

    Outer - Join Concept

    An outer join

    Produces a row of output when a matching value

    for the condition tested is found in each table.

    Includes one row for each unmatched row of datain the outer joined table leaving any attributes from

    the unmatched table blank.

    Traditional Oracle outer join syntax

    Places (+) next to the side of a join condition thatis allowed to be null (have no matching rows)

  • 8/6/2019 ansi_join

    15/17

    Outer join Example

    select pass_name, f_name || ' ' || l_name as ff_name, cur_year_miles

    from passenger p, frequent_flyer ff

    where p.ff_no = ff.ff_no (+) ;

    SAMPLE RESULTS

    PASS_NAME FF_NAME CUR_YEAR_MILES

    -------------------- ------------------------------- --------------

    ALICE ELLIS Alice Ellis 11200

    HOMER ELLIS Alice Ellis 11200

    WILL GATES Tip Felt 0

    TIP FELT Tip Felt 0

    DAVID PETERSON David Peterson 8100

    GLORIA ANDERSON

    ANDY ANDERSON

  • 8/6/2019 ansi_join

    16/17

    OUTER JOINS ANSI-ISO

    SELECT

    col_listFROM table1 LEFTOUTER JOIN table2

    ONjoin_condition

    Also a RIGHTOUTER JOIN and a FULL OUTER

    JO

    IN E.G Select f.flight_no, orig, flight_date

    from flight fleft outer join ticket t

    on f.flight_no = t.flight_no

    where orig = PHX; Parallel structure for RIGHTOUTER JOIN

    FULL OUTER JOIN puts a row in the output if either table has

    an unmatched value and places nulls in the fields from the

    omitted table

  • 8/6/2019 ansi_join

    17/17

    ASI Syntax Outer Join Example

    select pass_name, f_name || ' ' || l_name as ff_name, cur_year_miles

    from passenger p left outer join frequent_flyer ff

    on p.ff_no = ff.ff_no;

    SAMPLE RESULTS

    PASS_NAME FF_NAME CUR_YEAR_MILES

    -------------------- ------------------------------- --------------

    ALICE ELLIS Alice Ellis 11200

    HOMER ELLIS Alice Ellis 11200

    WILL GATES Tip Felt 0

    TIP FELT Tip Felt 0

    DAVID PETERSON David Peterson 8100

    GLORIA ANDERSONANDY ANDERSON