Get the names of all players who have won more than one medal.
(It's not clear what this means. Have won than one kind of medal? Or have received more than one medal? Your example answer suggests the latter. Also, it treats "null" as just another kind of medal, not specially as in SQL.)
-- rows where
THERE EXISTS compId,sport,medal,compId1,compId2,medal2 SUCH THAT
in competition [compId] of sport [sport] player [playerName] won [medal]
AND in competition [compId2] of sport [sport2] player [playerName] won [medal2]
AND (compId <> compId2 OR sport <> sport2 OR medal <> medal2)
Using statement shorthand:
-- rows where
THERE EXISTS compId,sport,medal,compId1,compId2,medal2 SUCH THAT
Competition(compId, sport, playerName, medal)
AND Competition(compId2, sport2, playerName, medal2)
AND (compId <> compId2 OR sport <> sport2 OR medal <> medal2)
Rearranging (anticipating the limitations of one comparison per σ and one attribute set per ∪):
-- rows where
THERE EXISTS compId,sport,medal,compId1,compId2,medal2 SUCH THAT
( Competition(compId, sport, playerName, medal)
AND Competition(compId2, sport2, playerName, medal2)
AND compId <> compId2)
OR ( Competition(compId, sport, playerName, medal)
AND Competition(compId2, sport2, playerName, medal2)
AND sport <> sport2)
OR ( Competition(compId, sport, playerName, medal)
AND Competition(compId2, sport2, playerName, medal2)
AND medal <> medal2)
Now to get the algebra replace:
- every statement by its table/relation
- every AND of table/relation by ⋈ (natural join)
- every OR of table/relation (which must have the same columns/attributes) by ∪ (union)
- every AND NOT (which must have the same columns/attributes) by \ (difference)
- every AND comparison by σ comparison (select/restrict)
- every EXISTS names to drop by π names to keep (projection)
every column/attribute renaming by ρ (rename).
π playerName (
σ compId <> compId2 (Competition
⋈ ρ compID2/compID ρ sport2/sport ρ medal2/medal Competition)
∪ σ sport <> sport2 (Competition
⋈ ρ compID2/compID ρ sport2/sport ρ medal2/medal Competition)
∪ σ medal <> medal2 (Competition
⋈ ρ compID2/compID ρ sport2/sport ρ medal2/medal Competition)
)
(For more see this answer.)