SQL Join -
Statement:
SQL
joins are used to gather
and manipulate data across several tables based
on a relationship between certain columns in these tables.
Note:
These tables should have common columns.
For
Example:
The tables Order and Part shown in below,
Order
Table:
ORDERDATE
|
NAME
|
PARTNUM
|
REMARKS
|
15-MAY-1996
|
TRUE WHEEL
|
42
|
PAID
|
19-MAY-1996
|
TRUE WHEEL
|
76
|
PAID
|
30-MAY-1996
|
BIKE SPEC
|
54
|
PAID
|
17-JAN-1996
|
LE SHOPPE
|
76
|
PAID
|
1-JUN-1996
|
AAA BIKE
|
10
|
PAID
|
Part
Table:
PARTNUM
|
DESCRIPTION
|
PRICE
|
54
|
PEDALS
|
54.25
|
42
|
SEATS
|
24.50
|
46
|
TIRES
|
15.25
|
23
|
MOUNTAIN BIKE
|
350.45
|
76
|
ROAD BIKE
|
530.00
|
10
|
TANDEM
|
1200.00
|
Now join PART and ORDERS:
SELECT
O.ORDERDATE, O.NAME, O.PARTNUM, P.PARTNUM, P.DESCRIPTION
FROM
ORDERS O, PART P WHERE O.PARTNUM = P.PARTNUM
The Result Set
is,
ORDERDATE
|
NAME
|
PARTNUM
|
PARTNUM
|
DESCRIPTION
|
15-MAY-1996
|
TRUE WHEEL
|
42
|
42
|
SEATS
|
30-MAY-1996
|
BIKE SPEC
|
54
|
54
|
PEDALS
|
1-JUN-1996
|
AAA BIKE
|
10
|
10
|
TANDEM
|
[Note that PARTNUM fields that are common to both tables]
No comments:
Post a Comment