Solving a relational query: part 3

This chart represents several constituent comp...
Image via Wikipedia

This is starting to depress me, now, because I thought I was full on top of all this material until I tried this question.

So, going back to the database specified in part 1 – I now need to find the relational algebra (not the SQL, I have done that) for: “find the name and email of any user who has made multiple bids for a lot including a bid exceeding £100″.

Various ideas have crossed my mind and some bits are easy eg USER \Join \sigma _{AMOUNT > 100} \rho (BIDDER\_ID -> USER\_ID,\ BID) gets a relation from which we coul;d extract users who bid more than £100 for anything, but how can I get it all to string together.

I think the SQL that works is:

SELECT DISTINCT U.NAME, U.EMAIL
FROM USER U, BID B, LOT L
WHERE U.USER_ID = B.BIDDER_ID AND
B.AMOUNT > 100 AND B.LOT_ID = L.LOT_ID
AND L.LOT_ID IN (
SELECT COUNT(*) > 1 FROM
LOT, BID WHERE
LOT.LOT_ID = L.LOT_ID AND BID.LOT_ID = LOT.LOT_ID
AND BID.BIDDER_ID = U.USER_ID)

About these ads

5 thoughts on “Solving a relational query: part 3”

  1. Methinks the “> 1″ part of the inner query should be in a WHERE clause and not a SELECT field.

    My SQL is rusty, but I believe grouping is your friend here. I think you can select BIDDER_ID, COUNT(BID_ID) and MAX(AMOUNT) in a single SELECT of the appropriate join, with GROUP BY BIDDER_ID and maybe put the count > 1 and max > 100 in the WHERE clause of that query. Then you need to connect the BIDDER_ID field of that query to the other stuff.

Comments are closed.