0

list days appearing in reservation where only red boats have been reserved using COUNT aggregate function. Here is the table

servant table:

sname rating
Joe 4
Bob 2
Tim 9
Mike 1
Lewis 5

boat table:

bname color rating
Ace orange 6
Bethany red 5
Cruiser green 9
WindySea red 8

reservation table:

sname bname day
Bob  Ace Monday
Bob  Bethany Wednesday
Bob  WindySea Saturday
Tim  Ace Sunday
Tim  Bethany Wednesday
Tim  Cruiser Wednesday
Mike Ace Monday
1
  • A description of how to convert NOT EXISTS to COUNT would also be helpful. Commented Oct 24, 2016 at 20:54

1 Answer 1

1
SELECT r.day 
FROM reservation r
JOIN boat b
  ON r.bname = b.bname
GROUP BY r.day 
HAVING COUNT(CASE WHEN b.color <> 'red' THEN 1 ELSE NULL END) = 0
   AND COUNT(CASE WHEN b.color = 'red' THEN 1 ELSE NULL END) > 0 -- optional
Sign up to request clarification or add additional context in comments.

2 Comments

Sean ELSE NULL is the default if you didnt included.
I see, this is a very clean of working this out. I tried other ways but always ran into an extra column that shouldn't have been included. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.