Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra and SQL

Similar presentations


Presentation on theme: "Relational Algebra and SQL"— Presentation transcript:

1 Relational Algebra and SQL
Given relational schema: Frequent (D, P) Serves (P, B) Likes (D, B) Attributes: P (pub), B (beer), D (drinker) The pubs that serve a beer that Jefferson likes. Drinkers that frequent at least one pub that serves “Bud” or “Becks”. Drinkers that frequent only pubs that serve some beer they like Drinkers that frequent only pubs that serve no beer they like.

2 1) Algebra Solution 1: Solution 2: Solution 3: Solution 4: 16

3 2) SQL Solution 1: Solution 2: FROM S WHERE b IN (‘Bud”, ‘Becks’))
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’))

4 2) SQL Solution 3: EXCEPT SELECT d FROM (SELECT * SELECT d,p FROM S, L
FROM F EXCEPT SELECT d FROM (SELECT * SELECT d,p FROM S, L WHERE S.b = L.b)

5 2) SQL Solution 4: Solution 4 (alternative solution): SELECT d FROM F
EXCEPT SELECT d FROM (SELECT * INTERSECT SELECT d,p FROM S, L WHERE S.b = L.b) 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


Download ppt "Relational Algebra and SQL"

Similar presentations


Ads by Google