Lecture5
- 1. Query Examples in Relational Algebra and SQL
Consider the relation schemas as follows.
works(person name, company name, salary);
lives(person name, street, city);
located in(company name, city);
managers(person name, manager name);
where manager name refers to person name.
a Find the names of the persons who work for company ’FBC’ (company name=’FBC’).
Relational algebra:
¢¡¢£¥¤§¦©¨!$# #%$¥')(§0!$1%$#32 #%$1465 718@9@51'BADC¥E©F¢G¥H@H
SQL:
Select person_name
From works
Where company_name = ’FBC’
b List the names of the persons who work for company ’FBC’ along with the cities they live in.
Relational algebra:
I¢P¥Q ¡¢£SR¢TU VRXW¢Y`a!$# #%$b(§0!$1%$#32 #%$14 5 718@9 5 'BADC¥E©F¢G¥H
cXd ¡Uef§g¥h¢¡pirqs I¢P¥Q ¡¢£SR¢TU VRXW¢Yut$vs¦¢g¥h¢¡¢£
¢¡¢£¥¤§¦©¨wa!$# #%$x 01y$€@2S' cXd ¡Uef§g¥h¢¡pirqXH
SQL:
Select lives.person_name, city
From works, lives
Where company_name = ’FBC’ and
works.person_name = lives.person_name
c Find the persons who work for company ’FBC’ with a salary of more than 10000. List the names of these
persons along with the streets and cities where they live.
Relational algebra:
¡U §£UT©q‚£XƒU„XRXW¢Y`a(§0!$1%$#32 #%$14 5 718@9 5 '†…pTU V‡§£SH
I¢P¥Q ¡©ˆ‰g¥¨ d W‰g¥¢‘ P ¦’!$# #%$b(§%1“1%@2b”6•$–1–1–1–S' ¡U §£UT©q‚£XƒU„XRXW¢YSH
¢¡¢£¥¤§¦©¨w I¢P¥Q ¡©ˆ‰g¥¨ d W‰g¥¢‘ P ¦—t$vs¦¢g¥h¢¡¢£
SQL:
Select lives.person_name, stree, city
From lives, works
Where lives.person_name = works.person_name and salary 10000
and works.company_name = ’FBC’
1
- 2. d Find the names of the persons who live and work in the same city.
Relational algebra:
ˆpTU V‡fT¡ P ¨‚g©T©qs!$# #%$x 01y$€@2S'†…pTU V‡§£Dt$vs¦XT¡ P ¨¢¡£¢ grq¢H
¢¡¢£¥¤§¦©¨wa!$# #%$b(§01y$€@2@4¥¤§¦ 01y$€@2S'†ˆpTU V‡fT¡ P ¨‚g©T©q©¨ '¦¢g¥h¢¡¢£©H@H
SQL:
Select person_name
From works, lives, locates_in
Where works.person_name = lives.person_name and
works.company_name = located_in.company_name and
located_in.city = lives.city
e Find the names of the persons who live in the same city and on the same street as their managers.
Relational algebra:
¢¢£ ¢¢ (V%$#%@ #%$14 !¦ !$# #%$¥'#D'¦¢g¥h¢¡¢£SH t$v Q‚P q P ¢¡U XH$¨% '¦¢g¥h¢¡¢£SH
‘ P¥Q ¡V‘U¨V ¢¡V¡U¨Y§g¥¨Ve`a('!¦ $€131€34 !¦ (€131€0)1'!¦ 01y$€@2@4 !¦ 01y$€@2¥' ¢¢£ ¢¢UH
¢¡¢£¥¤§¦©¨wa¡'!¦ !$# #%$¥'$‘ P¥Q ¡V‘U¨V ¢¡V¡U¨Y§g¥¨Ve©H
SQL:
Select e.person_name
From lives e, lives m, managers
Where e.person_name = managers.person_name and
m.person_name = managers.manager_name and
e.street = m.street and e.city = m.city
f Find the names of the persons who do not work for company ’FBC’.
Relational algebra:
¡U §£UT©qR¢TU VRXW¢Y`!$# #%$(§0!$1%$#@@2 #%$14 5 718@9 5 '†…pTU V‡§£SH
ˆpTU V‡ ¡U §£UT©q‚£’a!$# #%$S'†…pTU V‡§£SH
¢¡¢£¥¤§¦©¨wˆpTU V‡ ¡U §£UT©q‚£$2 ¡U §£UT©qR¢TU VRXW¢Y
SQL:
Select person_name
From works
Where person_name not in (Select person_name
From works
Where company_name = ’FBC’)
g Find the persons whose salaries are more than the salary of everybody who work for company ’SBC’.
Relational algebra:
‘ P ¦ P ‚g©¡¢£XƒU„‘©W¢Yua‚%1“1%@2 (§0!$1%$#32 #%$14 5 (8@9 5 '†…pTU V‡§£SH
ˆpTU V‡§£¥ˆ‰g¥¨ d ‘ P ¦ P Ve¢ƒU„‘SW¢YD…pTU V‡§£3¨4 '$‘ P ¦ P ‚g©¡¢£XƒU„‘©W¢YSH
¢P Xq‚£©f¡¢£V£ cXdpP qp‘VT Q ¡V‘SW¢YDa!$# #%$b(§%1“1%@2§5 (6¦ %1“1%@2S'†ˆpTU V‡§£¥ˆ‰g¥¨ d ‘ P ¦ P Vepƒ©„‘SW¢Y¥H
¢¡¢£¥¤§¦©¨wa!$# #%$S'†…pTU V‡§£SH72 ¢P Xq‚£©f¡¢£V£ cXdpP qp‘VT Q ¡V‘SW¢Y
2
- 3. SQL:
Select person_name
From works
Where salary all (Select salary
From works
Where company_name = ’SBC’)
h Find the names of the companies that is located in every city where company ’SBC’ is located in.
Relational algebra:
¦V¦X‘©W¢YVY§g¥¨‚g©¡¢£’‚01y$€@2 (§0!$1%$#32 #%$14 5 (8@9 5 '¦XT¡ P ¨¢¡£¢ grq¢H
i Q‚P ‚grq ¦V¦UYT Q¡ pP qVe ¨ ¦X¦X‘SW¢YVfVT¡ P ¨‚gST©q§£ a‚0!$1%$#32 #%$S'£¢)C¥¤§¦©¨ H ¨ ¦V¦X‘©W¢YVY§g¥¨‚g©¡¢£
I TU¨¢¡ P ¦V¦©e c X¤p¡D i Q‚P ‚grq ¦V¦UYT Q¡ pP qe ¨ ¦V¦U‘©WYVfT P ¨‚gST©q‚£ 2 ¦XT¡ P ¨¢¡£¢ grq
¢¡¢£¥¤§¦©¨wa‚0!$1%$#32 #%$S'¦XT¡ P ¨¢¡£¢ grq¢H72 ‚0!$1%$#32 #%$S' I TU¨¢¡ P ¦V¦©e c X¤p¡¥H
Relational algebra (another solution):
¦V¦X‘©W¢YVY§g¥¨‚g©¡¢£’‚01y$€@2 (§0!$1%$#32 #%$14 5 (8@9 5 '¦XT¡ P ¨¢¡£¢ grq¢H
¢¡¢£¥¤§¦©¨w ¦XT¡ P ¨¢¡£¢ grq ¦V¦X‘©W¢YVY§g¥¨‚g©¡¢£
SQL:
Select company_name
From located_in t
Where (Select city
From located_in s
Where t.company_name = s.company_name)
contains (Select city
From located_in s1
Where s1.company_name = ’SBC’)
SQL (another solution):
Select company_name
From located_in t
Where not exists
(Select *
From located_in s
Where s.company_name = ’SBC’ and
s.city not in
(Select city
From located_in l
Where l.company_name = t.company_name))
3