Download presentation
Presentation is loading. Please wait.
Published byJoleen French Modified over 8 years ago
1
1 Relational Algebra & SQL Query Formulation Exercise
2
2 Exercise 4: Relational Algebra and SQL Given relational schema: Frequent (D, P) Serves (P, B) Likes (D, B) Attributes: P (pub), B (beer), D (drinker) 1) The pubs that serve a beer that Jefferson likes. 2) Drinkers that frequent at least one pub that serves “Bud” or “Becks”. 3) Drinkers that frequent only pubs that serve some beer they like 4) Drinkers that frequent only pubs that serve no beer they like.
3
3 1) Algebra Solution 1: Solution 2: Solution 3: Solution 4:
4
4 2) SQL Solution 1 : Solution 2: SELECT p FROM S, L WHERE S.b=L.b AND L.d=‘Jefferson’ SELECT d FROM F WHERE F.p in ( SELECT p FROM S WHERE b IN (‘Bud”, ‘Becks’))
5
5 2) SQL Solution 3 : SELECT d FROM F EXCEPT SELECT d FROM ( SELECT * FROM F EXCEPT SELECT d,p FROM S, L WHERE S.b = L.b)
6
6 2) SQL Solution 4 : SELECT d FROM F EXCEPT SELECT d FROM ( SELECT * FROM F INTERSECT SELECT d,p FROM S, L WHERE S.b = L.b)
7
7 2) SQL Solution 4 (alternative solution) : SELECT d FROM F WHERE ( SELECT COUNT (DISTINCT p) FROM S, L WHERE F.p=S.p AND S.b=L.b AND L.d=F.d) = 0
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.