0% found this document useful (0 votes)
7 views

Join and Union Queries

The document discusses JOIN and UNION queries in SQL, explaining how to retrieve data from multiple tables using join conditions. It provides examples of SQL queries to find sailors who have reserved specific boats, including filtering by boat color and using UNION to combine results. The document also highlights the importance of using range variables and the efficiency of different query strategies.

Uploaded by

cixabih107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Join and Union Queries

The document discusses JOIN and UNION queries in SQL, explaining how to retrieve data from multiple tables using join conditions. It provides examples of SQL queries to find sailors who have reserved specific boats, including filtering by boat color and using UNION to combine results. The document also highlights the importance of using range variables and the efficiency of different query strategies.

Uploaded by

cixabih107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Join and Union Queries

JOIN Queries
SELECT S.sname
FROM Tables to be joined
Sailors S1, Reserves R1
WHERE S1.sid=R1.sid AND R1.bid=103 Join condition (s)

Cartesian Product / Cross Product

JOIN
Filtering joined tuples not
matching conditions
Example of Conceptual
Evaluation
SELECT S.sname
FROM Sailors S1, Reserves R1
WHERE S1.sid=R1.sid AND R1.bid=103

sid sname rating age sid bid day


22 dustin 7 45.0 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0
Conceptual Evaluation
Strategy
• Essentially, we simulate the computation of the query and
figure out its answer.
• Strategy/Algorithm
• Compute the cross-product of relation-list.
• Discard resulting tuples if they fail qualifications.
• Delete attributes that are not in target-list.
• If DISTINCT is specified, eliminate duplicate rows.

• This strategy is probably the least efficient way to compute a


query! An optimizer will find more efficient strategies to
compute the same answers.
Example of Conceptual
Evaluation
SELECT S.sname sid sname rating age
sid bid day
FROM Sailors S1, Reserves R1 22 dustin 7 45.0
22 101 10/10/96
WHERE S1.sid=R1.sid AND R1.bid=10331 lubber 8 55.5
58 103 11/12/96
58 rusty 10 35.0

(sid) sname rating age (sid) bid day


22 dustin 7 45.0 22 101 10/ 10/ 96
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 22 101 10/ 10/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
58 rusty 10 35.0 22 101 10/ 10/ 96
58 rusty 10 35.0 58 103 11/ 12/ 96
Intuitively …
SELECT S.sname Find the names of Sailors
FROM Sailors S1, Reserves R1 that reserved boat and boat id 103
WHERE S1.sid=R1.sid AND R1.bid=103

(sid) sname rating age (sid) bid day


22 dustin 7 45.0 22 101 10/ 10/ 96
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 22 101 10/ 10/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
58 rusty 10 35.0 22 101 10/ 10/ 96
58 rusty 10 35.0 58 103 11/ 12/ 96
Another Example
• Retrieve ids, ratings and ages for all Sailors older
than 30 and have rented boat 103.
• What modifications to earlier example below:
SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND bid=103
A Note on Range Variables
• Really needed only if the same relation appears twice in the
FROM clause. The previous query can also be written as:

SELECT S.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND R.bid=103 It is good style,

sname
however, to use
OR
range variables
SELECT
Sailors, Reserves
always!

FROM
WHERE Sailors.sid=Reserves.sid AND
Reserves.bid=103
Find sailors who’ve reserved at least
one boat
Sailors Reserves Boats
sid sname rating age bid bname color
sid bid day
22 dustin 7 45.0 101 Interlake blue
22 101 10/10/96
31 lubber 8 55.5 102 Interlake red
58 103 11/12/96
58 rusty 10 35.0 103 Clipper green
104 Marine red

• Which tables do we need in the FROM


clause?
• If sids?
• snames?
Find sailor names who’ve reserved at least
one boat
SELECTS.sname
FROM Sailors S, Reserves R
WHERE S.sid=R.sid

(sid) sname rating age (sid) bid day


22 dustin 7 45.0 22 101 10/ 10/ 96
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 22 101 10/ 10/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
58 rusty 10 35.0 22 101 10/ 10/ 96
58 rusty 10 35.0 58 103 11/ 12/ 96

• Would adding DISTINCT to this query make a difference?


• Would we still need both relations if we were finding
just sids?
Find sailors who’ve reserved at least one
blue boat
Sailors Reserves Boats
sid sname rating age bid bname color
sid bid day
22 dustin 7 45.0 101 Interlake blue
22 101 10/10/96
31 lubber 8 55.5 102 Interlake red
58 103 11/12/96
58 rusty 10 35.0 103 Clipper green
104 Marine red

• Which tables do we need in the FROM


clause?
• If sids?
• snames?
Joins
Find sailor names who’ve reserved at least
one blue boat
Sailors Reserves Boats
sid sname rating age bid bname color
sid bid day
22 dustin 7 45.0 101 Interlake blue
22 101 10/10/96
31 lubber 8 55.5 102 Interlake red
58 103 11/12/96
58 rusty 10 35.0 103 Clipper green
104 Marine red
• Lets modify this query

SELECT S.sname
Sailors S, Reserves R
SELECT S.sname
FROM
S.sid=R.sid AND bid=103
Sailors S, Reserves R, Boats B
WHERE
FROM
WHERE S.sid=R.sid AND R.bid=B.bid
AND B.color = ‘blue’
Find sids, names of sailors who’ve reserved
a red or a green boat
Boats B1 Reserves R1
bid bname color sid bid day
101 Interlak blue
e 22 101 10/10/96
102 Interlak red 58 103 11/12/96
e
103 Clipper green
Sailors S1
104 Marine red Which relations will
sid sname rating age
we need?
22 dustin 7 45.0
31 lubber 8 55.5 All three!!
58 rusty 10 35.0
Find sid’s and names of sailors who’ve reserved a red or a
green boat(1): Logical OR

SELECT S.sid, S.sname


FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid AND R.bid = B.bid
AND (B.color = ‘red’ OR B.color = ‘green’)
SELECT S.sid, S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid AND R.bid = B.bid
AND (B.color = ‘red’ OR B.color = ‘green’)
• Beginning with the Cartesian product (subset
shown below).
• How many columns?
• How manybid
bcolor bname
tuples?
sid sname rating age sid bid date

Blue Interlake 101 22 dustin 7 45.0 22 101 10/ 10/ 96

Blue Interlake 101 31 lubber 8 55.0 22 101 10/ 10/ 96

Blue Interlake 101 58 rusty 10 35.5 22 101 10/ 10/ 96

Red Interlake 102 22 dustin 7 45.0 22 101 10/ 10/ 96

Red Interlake 102 31 lubber 8 55.0 22 101 10/ 10/ 96

Red Interlake 102 58 rusty 10 35.5 58 103 11/ 12/ 96

…… …….. … … ….. ….. …….. ….. ….. …..


B S R
bcolor bname bid sid sname rating age sid bid date

Blue Interlake 101 22 dustin 7 45.0 22 101 10/ 10/ 96

Blue Interlake 101 31 lubber 8 55.0 22 101 10/ 10/ 96

Blue Interlake 101 58 rusty 10 35.5 22 101 10/ 10/ 96

Red Interlake 102 22 dustin 7 45.0 22 101 10/ 10/ 96

Red Interlake 102 31 lubber 8 55.0 22 101 10/ 10/ 96

Red Interlake 102 58 rusty 10 35.5 58 103 11/ 12/ 96

…… ……… ……. ……… …………. …… …….. …… …… ………

WHERE S.sid = R.sid AND R.bid = B.bid


AND (B.color = ‘red’ OR B.color =
‘green’)

Outcome?
1 tuple, 10 columns
Final Result?
1 tuple, 2 columns
Find sid’s and names of sailors who’ve reserved a red or a
green boat (2) : Set UNION

SELECTS.sid, S.sname
FROM Sailors S, Boats B, Reserves R
WHERE S.sid=R.sid AND R.bid=B.bid AND
Sailors that have reserved red boats

B.color=‘red’
UNION
SELECTS.sid, S.sname
FROM Sailors S, Boats B, Reserves R
Sailors that have reserved green boa

WHERE S.sid=R.sid AND R.bid=B.bid AND


B.color=‘green’
Comments on UNION

• UNION: Can be used to compute the union of any two union-


compatible sets of tuples (which are themselves the result of
SQL queries).
• Also available: EXCEPT (What do we get if we replace UNION by
EXCEPT?)

• UNION is a set union


• UNION ALL is a bag union (duplicates will be present)
Find sid’s and names of sailors who’ve reserved a red or a
green boat(1): Logical AND?

SELECT S.sid, S.sname


FROM Sailors S, Boats B, Reserves R
WHERE S.sid = R.sid AND R.bid = B.bid
AND (B.color = ‘red’ OR B.color = ‘green’)

• If we replace OR by AND, what


do we get?
Find sid’s of sailors who’ve reserved a red and a
green boat
SELECT S.sid
• INTERSECT: computes the FROM Sailors S, Boats B1, Reserves R1,
intersection of any two union- Boats B2, Reserves R2
WHERE S.sid=R1.sid AND R1.bid=B1.bid
compatible sets of tuples.
AND S.sid=R2.sid AND R2.bid=B2.bid
• Included in the SQL/92 AND (B1.color=‘red’ AND B2.color=‘green’)
standard, but some systems
don’t support it.
• What if we use S.name?. Key field!
SELECT S.sid
FROM Sailors S, Boats B,
Reserves R
WHERE S.sid=R.sid AND
R.bid=B.bid
AND B.color=‘red’
INTERSECT
SELECT S.sid
FROM Sailors S, Boats B,
Intermediate result after cross product followed by the 4
filter conditions associated with bid applied
SELECT S.sid
FROM Sailors S, Boats B1, Reserves R1,
Boats B2, Reserves R2
WHERE S.sid=R1.sid AND R1.bid=B1.bid
AND S.sid=R2.sid AND R2.bid=B2.bid
AND (B1.color=‘red’ AND B2.color=‘green’)

A subset of the 14cols96 rows intermediate result (before filtering rows)

bcolor (sid) bid sid sname rating age (sid) bid bcolor

Blue 22 101 22 dustin 7 45.0 22 101 Blue

Green 58 103 22 dustin 7 45.0 58 103 Green

Blue 22 101 31 lubber 8 55.5 22 101 Blue

Green 58 103 31 lubber 8 55.5 58 103 Green

Blue 22 101 58 rusty 10 35.0 22 101 Blue

Green 58 103 58 rusty 10 35.0 58 103 Green


Some Variations of “Inner” JOINs
S.sname
S.sname
SELECT
FROM Sailors S, Reserves R
SELECT
FROM Sailors S, Reserves R
WHERE S.sid > R.sid
WHERE S.sid = R.sid
Theta Join
Equi Join

(sid) sname rating age (sid) bid day


22 dustin 7 45.0 22 101 10/ 10/ 96
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 22 101 10/ 10/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
58 rusty 10 35.0 22 101 10/ 10/ 96
58 rusty 10 35.0 58 103 11/ 12/ 96
Natural Join = equijoin + eliminate duplicate columns
OUTER JOINS: Left, Right and Full
LOJ : all normal joined tuples plus those in the left relation that
don’t have matching tuples in the right relation filled with Null
values
ROJ: Similar Employee
Name EmpId DeptName Dept
Harry 3415 Finance DeptName Manager
Sally 2241 Sales Sales Harriet
George 3401 Finance Production Charles
Harriet 2202 Sales
Tim 1123 Executive

Employee Dept
Name EmpId DeptName Manager
Harry 3415 Finance NULL
Sally 2241 Sales Harriet
George 3401 Finance NULL FOJ : LOJ plus ROJ
Harriet 2202 Sales Harriet
Tim 1123 Executive NULL
NULL NULL Production Charles
Full Outer Join
Find names of Sailors that have not
reserved any boats
• How?
More on Outer Joins
• More complex conditions based on NULL values

You might also like