DB2 Unit 7

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

DB2 TRANSPARENCIES (Unit 7)

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

11 The JOIN Operation


Objectives - Student will
Understand how to code an efficient JOIN

DB2_Tr Ver. 1.0.0 04/12/1998

Page 2 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)
DB2_Tr Ver. 1.0.0 04/12/1998 Page 3 of 13

11 The JOIN Operation


Example 50

select * from orders , items

where p_no = 102;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 4 of 13

11 The JOIN Operation


Example 51

select c.company , o.o_no , o.o_date from customer , orders c o

where c.c_no = o.c_no;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 5 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 order by 1, 2;

and o.s_no = e.e_no

DB2_Tr Ver. 1.0.0 04/12/1998

Page 6 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;

DB2_Tr Ver. 1.0.0 04/12/1998

Page 7 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

DB2_Tr Ver. 1.0.0 04/12/1998

Page 8 of 13

12 UNION
Objectives - Student will
Understand how to code a UNION

DB2_Tr Ver. 1.0.0 04/12/1998

Page 9 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 comparable data types

DB2_Tr Ver. 1.0.0 04/12/1998

Page 10 of 13

12 UNION
Example 54
select 'Part ' ', pno, 'Made in England

from part
where city = 'london' UNION select 'Supplier', England' from supplier where city = 'london' order by 2 desc;
DB2_Tr Ver. 1.0.0 04/12/1998 Page 11 of 13

sno, 'Located in

12 UNION
UNION ALL
Includes all rows returned (does not eliminate duplicates)

DB2_Tr Ver. 1.0.0 04/12/1998

Page 12 of 13

12 UNION
Example 55/56

select 'Part from part

',

pno

select 'Part from part

',

pno

where city = 'london' UNION ALL select Part from partsupp order by 1; , pno

where city = 'london' UNION select Part from partsupp order by 1; , pno

DB2_Tr Ver. 1.0.0 04/12/1998

Page 13 of 13

You might also like