{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture5

# lecture5 - Query Examples in Relational Algebra and SQL...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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: 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: 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: 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' @[email protected]@17 641\$# 3\$1\$")\$# \$" HH G F E A'598 5 &% 2#% &!0 (' &% #! sv\$ h g t g Q P gW d PI w HSYWXRXUXqTUU ' 2 % % % ! g g Q P [email protected]"(b&\$# #\$" P W d I SVUp' 5 @17 5 41\$%# 23#\$%1&\$"a`XXUXUU H T 98 & !0 ( Y W R q T H q i h g f e XrpU 2 & ! dXc '[email protected]\$y10 x\$%# #\$"aw sv\$uXVURS QI sqrpU Xc h g t Y W R T P i h g f e d ! Y R T Q H G F E A 98 &% 2#%& 0 ( CDB' 5 @17 5 41\$# 3\$1\$!"b&\$%# #\$"a`WXVURS PI 1 SV Q Vpq pXc VX 7SVUTp'S\$%# #\$"aw Y W T P d f q P 2 H & ! HWSpV P P d gV [email protected]%" 6( @%11"b\$# \$"aDWSV Q Vpq pXc VqX P Y e T ' 2% 5 2 % ( &% #! Y T P d f H Y W g SUX P P \$' &3VUpDWSUeV P P d VUTp 4 T Y g H T 98 4&% 2#% !0 2 % Y W g SVUp' 5 @( 5 1\$# 3\$1&\$"( @%11"auUX P P g Find the persons whose salaries are more than the salary of everybody who work for company 'SBC'. Relational algebra: XVUUU \$UU VUpw Y W R T R q T 2 q T T H T ' SV UpS&\$%# #\$"aUU VUTp ! q T H T 98 4&% 2#%& 0 SVUp' 5 @17 5 1\$# @@\$1\$!"(&\$%# #\$"`XRVUTUU ! Y W R q T f Find the names of the persons who do not work for company 'FBC'. Relational algebra: H e g Y Q VUVVUV P \$'&\$%# \$!" !'aw # H ' 2y 42y ' ) ( e g Y Q U @\$10 ! @@\$10 !10131( ! 43131\$ !'&a`VUVVUV P H h g % H S' &\$XU P q Q \$t S'D"\$# \$" ! 1&\$%# @%\$%V( # & P v H h g # ' &% #! 4 e Find the names of the persons who live in the same city and on the same street as their managers. Relational algebra: @' P [email protected]\$10 @[email protected]\$1b\$%# #\$"aw HH h g q T g T f T ' 2y 4 y0 ( & ! H q rg P [email protected]\$y10 x&\$%# #\$"s P VUp T v T ' 2 ! q T g T f T d Find the names of the persons who live and work in the same city. Relational algebra: SQL: SQL: SQL: Select person_name From works Where person_name not in (Select person_name From works Where company_name = 'FBC') 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 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 2 VXV rg P X w g g Y Y W q T rg P X' 5 @( 5 41\$# 23\$%1&\$"( [email protected]\$1VXV H q T 98 &% # !0 y0 g g Y Y W Relational algebra (another solution): H e ' &% 2#%&!0 pX c V P UT I S\$# 3\$1\$" 27rg P XS&\$%# 23#\$1&\$"aw H q T ' % !0 rg P X 2 S P &VUV q pQ UV qr Q i DpX c V P UT I q T q T g T f Y W e P T Y g P e VXV H )S\$# 3\$1\$"a S P VVYSXX eVq pQ UV r Q i g g Y Y W C' &% 2#%&!0 q T g T f W P T Y q g P rg P X' 5 @( 5 41\$# 23\$%1&\$"( [email protected]\$1VXV H q T 98 &% # !0 y0 g g Y Y W Relational algebra: h Find the names of the companies that is located in every city where company 'SBC' is located in. SQL (another solution): SQL: SQL: Select company_name From located_in t Where not exists (Select * From located_in s 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') Select person_name From works Where salary > all (Select salary From works Where company_name = 'SBC') Where s.company_name = 'SBC' and s.city not in (Select city From located_in l Where l.company_name = t.company_name)) 3 ...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern