0% found this document useful (0 votes)
14 views38 pages

Lecture 03

Uploaded by

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

Lecture 03

Uploaded by

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

Relational Algebra Queries

CS430/630
Lecture 3

Slides based on “Database Management Systems” 3rd ed, Ramakrishnan and Gehrke
Relational Algebra

� Basic operations:
� Selection  Selects a subset of rows from relation
� Projection  Deletes unwanted columns from relation
� Cross-product  Allows us to combine several relations
� Join ⊳⊲ Combines several relations using conditions
� Division  A bit more complex, will cover later on
� Set-difference
Union ∪ Intersection ∩

� Renaming  Helper operator, does not derive new result, just
renames relations and fields
 (R(F ), E)
� F contains oldname newname pairs
Operator Precedence

� In decreasing order of priority:


1. Selection  Projection 
2. Cross-product  Join ⊳⊲
3.
4.
Set-difference
Union ∪
 Intersection ∩

Example:  Reserves ⊳⊲ Sailors


bid 103
means ( Reserves) ⊳⊲ Sailors
bid 103
not  (Reserves ⊳⊲ Sailors)
bid 103
Example Schema
Sailors
Boats
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
Reserves
bid name color
101 interlake red
103 clipper green
sid bid day
22 101 10/10/96
58 103 11/12/96
Sample Query 0
Sailors Boats
sid sname rating age bid name color
Reserves
sid bid day

� Find names of sailors who have ratings at least 8.


Detail of sailor sid Another sailor detail

 (( Sailors)
snam rating 8
sname
Sailors
rating 8
Join Reserves ⊳⊲ Sailors
Reserves Sailors
sid bid day sid sname rating age
22 101 10/10/96 22 dustin 7 45.0
58 103 11/12/96 31 lubber 8 55.5
Reserves ⊳⊲ Sailors
58 rusty 10 35.0
sid bid day sname rating age
22 101 10/10/9 dustin 7 45.0
6
58 103 11/12/9 rusty 10 35.0
6

Each sid in Reserves is filled out with Sailor attributes


Sample Query 1
Sailors Boats
sid sname rating age bid name color
Reserves
sid bid day
bid only, no Boat deta
� Find names of sailors who’ve reserved boat #103
Detail of sailor sid sid, bid in reserves table
sname(( Reserves)⊳⊲ Sailors)
bid 103
sname( (Reserves⊳⊲ Sailors))
bid 103
Example Schema
Sailors
Boats
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
Reserves
bid name color
101 interlake red
103 clipper green
sid bid day
22 101 10/10/96
58 103 11/12/96
Sample Query 2
Sailors Boats
sid sname rating age bid name color
Reserves
sid bid day

� Find names of sailors who’ve reserved a red boat


Detail of sailor sid, bid Detail of boat bid
sid …
sname(
(( ( B))⊳ R)⊳⊲ S)

sid bid color 'red'
 sname(( Boats) ⊳⊲ Reserves ⊳⊲ Sailors)
color ' red
'
Sample Query 2
� Find names of sailors who’ve reserved a red boat
Detail of sailor sid sid, bid … Detail of boat bid

� One way that’s


right:
 sname((
color ' red Boats) ⊳⊲ Reserves ⊳⊲ Sailors)
'

… but this next is Wrong!: Watch out for precedence!


sname
Boats⊳⊲
(snamReserves⊳⊲ Sailors
color e(
'red' Boats))⊳⊲ Reserves⊳
⊲ Sailors

color 'red'

empty!
Sample Query 3
Sailors Boats
sid sname rating age bid name color
Reserves
sid bid day

� Find names of sailors who’ve reserved a red or a green boat


 (Tempboats, ( Boats))
' red  color ' green
color ' '

sname(Tempboats⊳⊲ Reserves⊳⊲ Sailors)


sname(
B⊳⊲ R⊳⊲ S)
color 'red color
'green'
Sample Query 4
Sailors Boats
sid sname rating age bid name color
Reserves
sid bid day

� Find names of sailors who’ve reserved a red and a green boat


 (Tempred,  (( Boats)⊳⊲ Reserves))
sid color '
red '
  (( green' Boats)⊳⊲
(Tempgreen, sid color '
Reserves))

 sname((Tempred  Tempgreen) ⊳⊲ Sailors)


Sample Query 5
Sailors Boats
sid sname rating age bid name color
Reserves
sid bid day

� Find names of sailors who’ve reserved only red boats


 (Tempred,  (( Boats)⊳⊲ Reserves))
sid color '
red '
(Tempothers, (( Boats)⊳⊲ Reserves))
sid color
'red'
sname((Tempred Sailors)
Tempothers)⊳⊲
Time to try it yourself…
� Try the exercises on the handed-out sheet
� You can confer with neighbors—this is not graded
� Turn in completed paper for the 3 points

� Lab sheet (Solution) (Solution posted later)

� Note: you need to attend class to get credit for this


work—it is a form of class participation.
An Example of Self-Joins
Sailors
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0

� Find sailors with maximum age


� No max operator in RA… (SQL has this)
� Need a trick: self join with “left” age smaller than “right” age
� This will list rows for all ages for left side but the max age row(s)
� Then use all-sailors – this list
An Example of Self-Join: cross-product
with rows eliminated by condition
“LeftHalf”
sid1 sname1 rating1 age1 sid2 sname2 rating2 age2
22 dustin 7 45.0 22 dustin 7 45.0
22 dustin 7 45.0 31 lubber 8 55.5
22 dustin 7 45.0 58 rusty 10 35.0
31 lubber 8 55.5 22 dustin 7 45.0
31 lubber 8 55.5 31 lubber 8 55.5
31 lubber 8 55.5 58 rusty 10 35.0
58 rusty 10 35.0 22 dustin 7 45.0
58 rusty 10 35.0 31 lubber 8 55.5
58 rusty 10 35.0 58 rusty 10 35.0
� Join condition:“left” age smaller than “right” age
An Example of Self-Join: Max ages
(S1,Sailors) (S2,Sailors)

(TempJoin(1 f f f f 4),
 1,2 2,3 3,4
S1⊳⊲ S1.age  S2.age S2)
(LeftHalf , f f f4
TempJoin)
f 1,
2, 3,

� Finally, subtract the resulting left hand side from the


initial relation, and you get sailors with maximum ages
� Final result is
SailorsLeftHalf
More on Natural Joins
Natural Joins match all same-named columns
� Consider two tables T1 and T2:
T1(id1, attr1, city) T2(id2, id1, attr2, city)
� Probably want to join on id1, a key for T1 showing up
in both tables
� But T1 and T2 have id1 and city in common, so a
natural join T1 ⨝ T2 matches both
� If we don’t want non-key columns matched like this
� We can use a theta join with an explicit condition:
T1 ⨝t1.id1=t2.id1 T2
� Or project out city before one of the joins
Consider the Example Schema, modified to
have a name attribute for two entities
Boats
Sailors
sid name rating ageReserves
bid name color
22 dustin 7 sid 45.0
bid day 101 interlake red
31 lubber 8 22 55.5
101 10/10/96103 clipper green
58 rusty 10 58 35.0
103 11/12/96
Sample Query 2 on modified schema
Sailors Boats
Reserves bid name color
sid name rating age
sid bid day

� Find names of sailors who’ve reserved a red boat


Detail of sailor sid sid, bid … Detail of boat bid

� Old solution:
 sname(( Boats) ⊳⊲ Reserves ⊳⊲ Sailors)
color ' red
'
� Returns an empty relation!
� It’s looking for matches on name as well as bid, sid
Sample Query 2 on modified schema
Sailors Boats
Reserves bid name color
sid name rating age
sid bid day

� Find names of sailors who’ve reserved a red boat


� Old solution for unmodified schema: returns empty table here

sname(( color ' red Boats) ⊳⊲ Reserves ⊳⊲ Sailors)

'
� Here we can project out boat names before join to Sailors
name(((  B)⊳ R)⊳⊲ S)

bid color 'red'
sid sname rating age
Another self join: 28 yuppy 9 35.0
Close competitors
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
� Find pairs of sailors (sids) with ratings that differ by no more
than one.
sid1 sid2
(S1,Sailors) (S2,Sailors)
28 28
(TempJoin(1sid1,5sid2), 28 31
S1⊳ S1.ratingS 2.rating 1^S1.rating S 2.rating 1 28 58
⊲ S2) TempJoin 31 28
 … …
sid1,sid2
� We don’t want a lot of these results…
sid sname rating age
Another self join 28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
� Find pairs of different sailors (sids) with ratings that differ
by no more than one, listing each unordered pair once.

(S1,Sailors) (S2,Sailors)
(TempJoin(1 sid1,5 sid2),
S1⊳ S1.rating  S2.rating 1^ S1.rating  S2.rating 1^ S1.sid 
S2)
⊲ S2.sid

 TempJoin sid1 sid2


sid1,sid2 28 31
� That’s better!
28 58
Like Query 0
Sailors Boats
sid sname rating age bid name color
Reserves
sid bid day

� Find colors of boats with names starting with C


Detail of Boat Another Boat detail
(assume lowercase names)
 (( Boats)
color 
 name'c'^name'd' Boats

color name'c'^name'd'
Like Query 3
Sailors Boats
sid sname rating age bid name color
Reserves
sid bid day

� Find names of sailors who’ve reserved a red or a green


boat. List names and the boat color (two rows if the
sailor rented both color boats)
 (Tempboats, ( Boats))
' red  color ' green
color ' '

 sname ,colo (Tempboats⊳⊲ Reserves⊳⊲ Sailors)


r

You might also like