-1

I have these relational schemes:

Participate (FestivalName, Artist)
Attend(ID, FestivalName)
Prefer (ID, FestivalName)
Like (ID, Artist) 

I would like to list the people which only attend festivals that are not of their preference and that at the same time, in one of those festivals, at least two artists that they like participated (so, 2 or more). Finally, show the ID and the festival.

I would like to solve this by using common operators:

selection operator, projection operator, union operator, difference operator and cartesian product

This is an example:

ATTEND TABLE
147|HannaBalusa |
147|FestivalTOP |
147|BestFestival|

PREFER TABLE
147|FestivalTOP|

LIKE TABLE
147|PaulMackarney|
147|BobDeylan    |

PARTICIPATE TABLE
HannaBalusa |PaulMackarney |
HannaBalusa |BobMurley     |
FestivalTOP |BobDeylan     |
BestFestival|PaulMackarney |
BestFestival|BobDeylan     |

So, i should obtain this output:

147|BestFestival

I can solve this situation by using SQL but i'm having trouble using relational algebra.

Can help me?

philipxy
  • 14,867
  • 6
  • 39
  • 83
JustToKnow
  • 785
  • 6
  • 23
  • [ask] [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/3404097) [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/3404097) [What is the policy here on homework?](https://meta.stackexchange.com/q/18242/266284) Show what parts you are able to do. [Re relational querying.](https://stackoverflow.com/a/24425914/3404097) PS There are many RAs (relational algebras). They differ in operators & even what a relation is. Give operator definitions & your reference for yours. – philipxy Jun 25 '20 at 00:45

1 Answers1

1

An idiomatic SQL query that does what you want might look like this, but it uses more SQL capabilities than you listed.

SELECT A.ID, A.FestivalName
FROM Attend A
JOIN Participate P 
ON A.FestivalName = P.FestivalName --Adds rows with artists for each attended festival
JOIN Like L 
ON L.ID = A.ID AND L.Artist = P.Artist --Leaves only rows with `Like`d artists
WHERE NOT EXISTS (
  SELECT 1 
  FROM Prefer Pr 
  WHERE Pr.ID = A.ID AND Pr.FestivalName = A.FestivalName
) --Removes people who have ever attended a prefered festival
GROUP BY A.ID, A.FestivalName --Allows to count liked artists per festival 
HAVING COUNT(L.Artist) >= 2 --Leaves only festivals with 2 or more liked artists 

To do it with your described operations, it might look like that

SELECT DISTINCT S1.ID, S1.FestivalName
FROM (
  SELECT A.ID, A.FestivalName, P.Artist
  FROM Attend A
  CROSS JOIN Participate P  
  CROSS JOIN Like L 
  WHERE A.FestivalName = P.FestivalName --Rows with artists for each attended festival 
  AND L.ID = A.ID AND L.Artist = P.Artist --Leaves only rows with `Like`d artists
) S1
CROSS JOIN ( -- Copy of the first subquery
  SELECT A.ID, A.FestivalName, P.Artist
  FROM Attend A
  CROSS JOIN Participate P  
  CROSS JOIN Like L 
  WHERE A.FestivalName = P.FestivalName
  AND L.ID = A.ID AND L.Artist = P.Artist 
) S2
WHERE S1.ID = S2.ID 
AND S1.FestivalName = S2.FestivalName 
AND S1.Artist != S2.Artist --Removes festivals with only 1 liked artist

MINUS -- Remove all rows with people who ever attended prefered festivals

SELECT ID, FestivalName 
FROM ( --People who attended prefered festivals
  SELECT DISTINCT A.ID
  FROM Attend A
  CROSS JOIN Prefer P
  WHERE A.ID = P.ID AND A.FestivalName = P.FestivalName
) 
CROSS JOIN ( -- All existent festivals
  SELECT FestivalName
  FROM Attend
)
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
  • Hey Alex, thank you very much for replying. Yeah, i got something similar but the problem is that i want to translate that and use relational algebra, that's where i'm having trouble. – JustToKnow Jun 24 '20 at 21:00
  • @Student_new read the update. It's late night for me, so I might have made a mistake somewhere. Had to remember my good old relational algebra classes – Alexey S. Larionov Jun 24 '20 at 21:23
  • Hey Alex; i'm checking your code.. something does not make sense to me.. i use these symbols: Projection (Π), Selection (σ), Union (U), Difference (-) and Cartesian Product (X) – JustToKnow Jun 24 '20 at 21:39
  • 1
    @Student_new You first make R1 = σ(FROM `Attend` X `Participate` X `Like`, WHERE `A.FestivalName = P.FestivalName` AND `L.ID = A.ID` AND `L.Artist = P.Artist`). Then take R2 = Π(R1, COLUMNS: `A.ID`, `A.FestivalName`, `P.Artist`). Then rename R3 = R2, then R4 = σ(FROM R2 X R3, WHERE `R2.ID = R3.ID` AND `R2.FestivalName = R3.FestivalName` AND `R2.Artist != R3.Artist`). Then make R5 = σ(FROM `Attend` X `Prefer`, WHERE `A.ID = P.ID` AND `A.FestivalName = P.FestivalName`), R6 = Π(R5, `A.ID`) and R7 = Π(`Attend`, `FestivalName`). The answer is R4 - (R6 X R7). Might have made some mistakes there too – Alexey S. Larionov Jun 24 '20 at 21:52
  • I'm sorry, Alex: what are trying to do in here: Π(R1, COLUMNS: A.ID, A.FestivalName, P.Artist)? – JustToKnow Jun 24 '20 at 22:41
  • @Student_new to take only listed columns from relation R1 – Alexey S. Larionov Jun 25 '20 at 09:43
  • Alex, have you reviewed your previous comment where you use relational algebra?. Does it look so for you? – JustToKnow Jun 25 '20 at 12:02
  • @Student_new I just translated my code into somewhat resembling relational algebra syntax. You can do so on your own, and adapt it to the syntax accepted in your course – Alexey S. Larionov Jun 25 '20 at 12:09
  • Alex, it helped me a lot!; can i ask you something?:I got this question rolling in my mind How would you do to *list the people which attend 4 festivals where each one has at least 3 artists that they do not like*? Using R.A (Projection (Π), Selection (σ), Union (U), Difference (-) and Cartesian Product (X)) – JustToKnow Jul 01 '20 at 15:20
  • @Student_new that would be painful, I'll just give a direction of thinking: `Attend X Attend X Attend X Attend` gives up to 4 combinations of festivals for different people, select only those there attendee is the same and concerts IDs are all different and not null. This will give you different **ordered combinations** of festivals for each person (note that some of them have the same festivals, just in different order, but since we look for people and we don't execute this gigantic query in real DB, we can ignore it). – Alexey S. Larionov Jul 01 '20 at 16:19
  • Next call `R1 = Participate X Prefer` and find Cartesian Product of `R1 X R1 X R1` and what we have so far, think about it as adding artists and preference info to each 1st, 2nd and 3rd (no 4th) `Attend` in the first product. Select only rows, where each artist in corresponding `Attend` is not prefered. From the remaining you project name of attendee and that's an answer. – Alexey S. Larionov Jul 01 '20 at 16:20
  • Yeah, i thought so but i'm till not getting it at all; doing it using SQL is easier but in R.A; i tried to solvedi t by using other operators: A ⋈θ B (theta join), natural join (A ⋈ B) or even the % (division) operator but i am not getting it :/ – JustToKnow Jul 02 '20 at 17:47
  • I think i'm gonna create a different thread, stay sharp! – JustToKnow Jul 02 '20 at 17:48
  • Pal, take look: https://stackoverflow.com/questions/62745397/logic-applied-in-relational-algebra-sql-related Something does not make sense to me. Is it correct?What would you change? – JustToKnow Jul 05 '20 at 19:35