DB2 UNIT 7

download DB2 UNIT 7

of 13

Transcript of DB2 UNIT 7

  • 7/28/2019 DB2 UNIT 7

    1/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 1 of 13

    DB2TRANSPARENCIES

    (Unit 7)

  • 7/28/2019 DB2 UNIT 7

    2/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 2 of 13

    11 The JOIN Operation

    Objectives - Student will

    Understand how to code an efficient JOIN

  • 7/28/2019 DB2 UNIT 7

    3/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 3 of 13

    11 The JOIN Operation

    Matches one table to another based on data

    common between the tables

    Join Predicate

    Columns with identical characteristics

    Result Table contains rows comprised of columns from

    all the tables in the join

    There is NO JOIN VERB

    May join up to 15 tables

    May join a table to itself (reflexive)

  • 7/28/2019 DB2 UNIT 7

    4/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 4 of 13

    11 The JOIN Operation

    Example 50

    select *

    from orders

    , items

    where p_no = 102;

  • 7/28/2019 DB2 UNIT 7

    5/13DB2_Tr Ver. 1.0.0 04/12/1998 Page 5 of 13

    11 The JOIN Operation

    Example 51

    select c.company

    , o.o_no

    , o.o_date

    from customer c

    , orders o

    where c.c_no = o.c_no;

  • 7/28/2019 DB2 UNIT 7

    6/13DB2_Tr Ver. 1.0.0 04/12/1998 Page 6 of 13

    11 The JOIN Operation

    Example 52

    select e.fname || ' ' || e.lname

    , o.o_no , o.o_date , i.p_no , i.quantity

    , i.quantity * i.price

    from orders o , items i , employee e

    where o.o_no = i.o_no and o.s_no = e.e_no

    order by 1, 2;

  • 7/28/2019 DB2 UNIT 7

    7/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 7 of 13

    11 the JOIN Operation

    Example 53

    select b.fname || ' ' || b.lname

    , b.department

    from staff a

    , staff b

    where b.department = a.department

    and a.empid = 110;

  • 7/28/2019 DB2 UNIT 7

    8/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 8 of 13

    11 The JOIN Operation

    Using Composite Keys (Compound Key)

    Will have multiple conditions within the WHERE clause, one

    for each column of the key

  • 7/28/2019 DB2 UNIT 7

    9/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 9 of 13

    12 UNION

    Objectives - Student will

    Understand how to code a UNION

  • 7/28/2019 DB2 UNIT 7

    10/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 10 of 13

    12 UNION

    Merge the results of two or more queries

    Removes duplicate rows

    Each SELECT must contain the same number of

    expressions (columns)

    Corresponding expressions must be the comparabledata types

  • 7/28/2019 DB2 UNIT 7

    11/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 11 of 13

    12 UNION

    Example 54

    select 'Part ', pno, 'Made in England

    '

    from part

    where city = 'london'

    UNION

    select 'Supplier', sno, 'Located in

    England'

    from supplier

    where city = 'london'

    order by 2 desc;

  • 7/28/2019 DB2 UNIT 7

    12/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 12 of 13

    12 UNION

    UNION ALL

    Includes all rows returned (does not eliminate duplicates)

  • 7/28/2019 DB2 UNIT 7

    13/13

    DB2_Tr Ver. 1.0.0 04/12/1998 Page 13 of 13

    12 UNION

    Example 55/56

    select 'Part ', pno

    from part

    where city = 'london'

    UNION ALL

    select Part , pno

    from partsupp

    order by 1;

    select 'Part ', pno

    from part

    where city = 'london'

    UNION

    select Part , pno

    from partsupp

    order by 1;