You are on page 1of 3

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(0

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:

123 546784:9;<
=  >! " # %&(' # *),+- ./0
?:@ 7ABCDFEHGI
123 546784:9;KJLICD
 M
=  N OP(5 ?:@ 7ABCDFEHG:0
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:

Q 776 GR:S7T:4:9;<
=! " # %&(' # VUF678W50
123  XYC @ 9YCZ[ 2 \
 >](>^$_````5 Q 776 GR:S7T:4:9;50
 M
123  XYC @ 9YCZ[ 2 aJLICD
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

d Find the names of the persons who live and work in the same city.
Relational algebra:

XF678WB6 2 RC 6 GI
 N OP(5VUF678W,JLI:6 2  CHG0
 M
=  >OP((" OP(5VXF678WB6 2 RC 6 G 
CD 0(0

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:

      
8( "  
,CD50 JL R
3 2 G 2 
Z 7:0
 CD50
[ 23 8[7887;C8A<
= P!P!" P!P OP((" OP(       70
 M
=   [ 23 8[7887;C8A 0

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:

Q 776 G46784:9;<
 (( " # %&(' # VUF678W50
XF678W Q 776 GR\
=  5VUF678W50
 M
XF678W Q 776 GR Q 776 G46784:9;
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:

[ 2  2 RC :S7T[ 9;K
= R]( ! " # &(' # VUF678W50
XF678WXYC @ [ 2  2 8AS7T[59;,
UF678W!
" [ 2  2 RC :S7T[ 9;50
2 :GR B8 ?:@F2 GF[86 3 8[59;,
=  >](#$ ](5VXF678WXYC @ [ 2  2 8AFS T[59;0
 M
=  5VUF678W50% 2 :GR B8 ?:@F2 GF[86 3 8[59;

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:

 8 :[ 9;8;CRC \


ROP( ! " # &(' # :6 2  CHG0
E 3R2 ZRCHG  87;6 3F2 G8A    ::[59;8B86 2 RC56 G
= R! 5+ 
 0   8 :[ 9;8;CRC 
1 67 2 8 A ? :F,
E 3R2 ZRCHG  87;6 3F2 GA    87[ 9;8B6 2 RC56 GR
:6 2  HC G
 M
= R! 5:6 2  CHG0% R! 5 1 67 2 8 A ? :F0
Relational algebra (another solution):

 8 :[ 9;8;CRC \


ROP( ! " # &(' # :6 2  CHG0
 M
:6 2  CHG  8:[ 9;8;CRC 
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))

You might also like