0% found this document useful (0 votes)
33 views26 pages

SQL and Relational Algebra SQL and Relational Algebra: Zaki Malik September 02, 2008 September 02, 2008

The document discusses various relational algebra operators including selection, projection, union, intersection, difference, cartesian product, and theta join. It provides examples of how each operator works using sample relations and how they translate to SQL queries. The key operators covered are selection (σ), projection (π), union (∪), intersection (∩), difference (-), cartesian product (X), and theta join (θ).

Uploaded by

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

SQL and Relational Algebra SQL and Relational Algebra: Zaki Malik September 02, 2008 September 02, 2008

The document discusses various relational algebra operators including selection, projection, union, intersection, difference, cartesian product, and theta join. It provides examples of how each operator works using sample relations and how they translate to SQL queries. The key operators covered are selection (σ), projection (π), union (∪), intersection (∩), difference (-), cartesian product (X), and theta join (θ).

Uploaded by

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

SQL and Relational Algebra

SQL and Relational Algebra

Zaki Malik
September 02, 2008
September 02, 2008
Basics of Relational Algebra
Basics of Relational Algebra

• Four types of operators:
– Select/Show parts of a single relation: projection and selection.

– Usual set operations (union, intersection, difference).
– Combine the tuples
p of two relations, such as cartesian
, p
product 
and joins.
– Renaming.
Projection
• The projection operator produces from a relation R a new 
relation containing only some of R’s columns.

• “Delete” (i.e. not show) attributes not in projection list.
“D l t ” (i t h ) tt ib t ti j ti li t
• Duplicates eliminated

• To obtain a relation containing only the columns A1,A2, . . . An
of R
RA:    π A1,A2, . . . An (R)
SQL:   SELECT A1,A2, . . . An FROM R;
Selection
• The selection operator applied to a relation R produces a new 
relation with a subset of R’s tuples.
• The tuples in the resulting relation satisfy some condition C 
that involves the attributes of R
that involves the attributes of R.
– with duplicate removal

RA: σc (R)
RA: 
SQL:   SELECT *FROM R WHERE C;
The WHERE clause of a SQL command corresponds to σ( ).
• The WHERE clause of a SQL command corresponds to  ()
Union
• The union of two relations R and S is the set of tuples that are 
in R or in S or in both.
– R and S must have identical sets of attributes and the types of 
the attributes must be the same.
the attributes must be the same.
– The attributes of R and S must occur in the same order.

• What is the schema of the result ?
RA:    R U S
SQL:   (SELECT * FROM R)
UNION
(SELECT * FROM
FROM S);
Union
sid  sname  rating  age 

22 
22 dustin 
d i 7 45.0
45 0
S1 31  lubber  8  55.5 
58  rusty  10  35.0 
sid  sname  rating  age 
S2 28 yuppy 9 35.0
31  lubber  8  55.5 
44  guppy  5  35.0 
58 rusty 10 35 0
35.0
S 1∪ S 2
sid  sname  rating  age 
22  dustin  7  45.0 
31  lubber  8  55.5 
58  rusty  10  35.0 
44
44  guppy 5 35 0
35.0
28  yuppy  9  35.0 
Intersection
• The intersection of two relations R and S is the set of tuples  
that are in both R and S.
• Same conditions hold on R and S as for the union operator.
– R
R and S must have identical sets of attributes and the types of the 
and S must have identical sets of attributes and the types of the
attributes must be the same.
– The attributes of R and S must occur in the same order.

RA:    R ∩ S
SQL (SELECT * FROM
SQL:   (SELECT * FROM R)
INTERSECT
((SELECT * FROM S););
Intersection
S1 S2

sid  sname  rating  age  sid  sname  rating  age 


28  yuppy  9  35.0 
22  dustin  7  45.0 
31 lubber 8 55 5
55.5
31  lubber  8 55.5
44  guppy  5  35.0 
58  rusty  10  35.0 
58  rusty  10  35.0 

S1∩ S2
sid 
id sname rating
i age
31  lubber  8  55.5 
58  rusty  10  35.0 
Difference
• The difference of two relations R and S is the set of tuples that 
are in R but not in S.
• Same conditions hold on R and S as for the union operator.
– R
R and S must have identical sets of attributes and the types of the 
and S must have identical sets of attributes and the types of the
attributes must be the same.
– The attributes of R and S must occur in the same order.
RA:    R ‐ S
SQL:   (SELECT * FROM R)
EXCEPT
(SELECT * FROM S);
R – (R 
• R  (R – S) = R ∩
=R∩S
Difference
S1 S2

sid  sname  rating  age  sid  sname  rating  age 


22  dustin  7  45.0  28  yuppy  9  35.0 
31  lubber  8  55.5  31  lubber  8  55.5 
58
58  rusty  10 35 0
35.0 44 guppy 5 3 0
35.0
58  rusty  10  35.0 

S1− S2
sid  sname  rating  age 

22  dustin  7  45.0 


Cartesian Product
Cartesian Product
• The Cartesian product (or cross‐product or product) of two 
relations R and S is a the set of pairs that can be formed by 
pairing each tuple of R with each tuple of S.
– The
The result is a relation whose schema is the schema for R 
result is a relation whose schema is the schema for R
followed by the schema for S.

RA:    R X S
SQL:   SELECT * FROM R , S ;
Cartesian Product
S1 R1
sid sname rating age sid bid day
22 d ti
dustin 7 45 0
45.0
22 101 10/10/96
31 lubber 8 55.5
58 103 11/12/96
58 rusty 10 35.0

(sid)  sname  rating 


S1 x R1
age  (sid)  bid  day  ?
22
22  dustin 7 45 0
45.0 22
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
58  rusty
t 10 35 0
35.0 22
22  101 10/10/96
58  rusty  10  35.0  58  103  11/12/96 

We rename attributes to avoid ambiguity or we prefix


attribute with the name of the relation it belongs to.
Theta Join
Theta‐Join

• The theta‐join of two relations R and S is the set of tuples in 
the Cartesian product of R and S that satisfy some condition C.
RA:    R ∞ S
C

SQL:   SELECT
SQL SELECT * 
*
FROM R , S 
WHERE C;;

R ∞
• R C S
S  = σ
C
(R x S)
Theta‐Join
S1 R1
sid sname rating age sid bid day
22 dustin 7 45 0
45.0 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0

S1>< R1
S1.sid < R1.sid
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 58 103 11/12/96

R >< c S = σ c ( R × S)
Natural Join
Natural Join
• The natural join of two relations R and S is a set of pairs of 
tuples, one from R and one from S, that agree on whatever 
attributes are common to the schemas of R and S.
• The schema for the result contains the union of the attributes 
The schema for the result contains the union of the attributes
of R and S.
• Assume the schemas R(A,B, C) and S(B, C,D)
RA:    R ∞ S
SQL:   SELECT * 
FROM R , S 
WHERE R.B = S.B AND R.C = S.C;
Operators Covered So far
Renaming
• If two relations have the same attribute, disambiguate the 
attributes by prefixing the attribute with the name of the 
relation it belongs to.
• How do we answer the query 
How do we answer the query “Name Name pairs of students who 
pairs of students who
live at the same address”?    Students(Name, Address)
– We need to take the cross‐product of Students with itself?
– How do we refer to the two “copies” of Students?
– Use the rename operator.
RA:   ρS (A1,A
RA: A2, . . . An)
((R)) : give R the name S; R has n attributes, which are 
: give R the name S; R has n attributes, which are
called A1,A2, . . . ,An in S
SQL: Use the AS keyword in the FROM clause: Students AS Students1 
renames Students to Students1.
renames Students to Students1
SQL: Use the AS keyword in the SELECT clause to rename attributes.
Renaming

• Are these correct ? 
• No !!! the result includes tuples where a student is paired 
with himself/herself
ith hi lf/h lf
• Solution: Add the condition S1.name <> S2.name.
P ti i R l ti
Practicing Relational Algebra
l Al b
Q1: Find names of sailors who have 
reserved boat #103
Reserves(sid, bid, day)  
Reserves(sid bid day)
Sailors(sid, sname, rating, age)

• Solution 1:
πsname(σbid = 103 (Reserves ∞ Sailors))

• Solution 2 (more efficient)


πsname((σbid = 103 Reserves) ∞ Sailors)

• Solution 3 (using rename operator)


P(Temp1 (σbid = 103 Reserves))
P(Temp2 (Temp1 ∞ Sailors))
πsname(Temp2)
Q2: Find names of sailors who have 
reserved a red boat
Reserves(sid, bid,
Reserves(sid bid day) Sailors(sid,
Sailors(sid sname,
sname rating,
rating age)
Boats(bid, bname, color)

• Solution 1:
πsname((σcolor = ‘red’
red Boats) ∞ Reserves ∞ Sailors )

• Solution 2 (more efficient)


πsname(πsid ((πbidσcolor = ‘red’ Boats)∞ Reserves )∞ Sailors )
Q3: Find the colors of boats 
reserved by Lubber
Reserves(sid, bid, day) Sailors(sid, sname, rating, age)
Boats(bid, bname, color)

• S
Solution:
l ti
πcolor((σsname = ‘Lubber’ Sailor)∞ Reserves ∞ Boats )
Q4: Find the names of sailors who 
have reserved at least one boat
Reserves(sid, bid, day) Sailors(sid, sname, rating, age)
Boats(bid, bname, color)

• Solution:
S l ti
πsname(Sailor∞ Reserves)
Q5: Find the names of sailors who 
have reserved a red or a green boat
Reserves(sid, bid, day) Sailors(sid, sname, rating, age)
Boats(bid, bname, color)

• S
Solution:
l ti
πsname(σcolor=‘red’ or color = ‘green’ Boats ∞ Reserves ∞ Sailors)
Q6: Find the names of sailors who 
have reserved a red and a green boat
Reserves(sid, bid, day) Sailors(sid, sname, rating, age)
Boats(bid, bname, color)

• Solution:
πsname(σcolor=‘red’ and color = ‘green’ Boats ∞ Reserves ∞ Sailors)

A ship cannot have TWO colors at the same time

πsname(σcolor=‘red’ Boats ∞ Reserves ∞ Sailors)



πsname(σcolor = ‘green’ Boats ∞ Reserves ∞ Sailors)
Q7: Find the sids of sailors with age over 
20 who have not reserved a red boat
Reserves(sid, bid, day) Sailors(sid, sname, rating, age)
Boats(bid, bname, color)

Strategy ? ? ?
Find all sailors (sids) with age over 20
Find all sailors (sids) who have reserved a red boat
Take their set difference

• Solution:
πsid (σage>20 Sailors) – πsid ((σcolor=‘red’ Boats) ∞ Reserves)

You might also like