0

The relations are:

Event (etype, description)                     primary key - etype

City (cname, country, population)              primary key - cname

Disaster (cname, dyear, etype, casualties)     primary key - cname, dyear

I need to write a query which tells which country faced all types (etype in relation) of disasters.

How do i do that?

I have this so far:

select country
from city natural join disaster, (select count(etype) as a
                                  from event) as A
group by country, etype
having count(country) = max(A.a)
4
  • So whats wrong with your query? Commented Nov 17, 2018 at 22:53
  • It gives back all countries that faced any disaster that > disaster types in event relation Commented Nov 17, 2018 at 22:56
  • 1
    For your tables, you need to add country into disaster table against the common named cities among the countries. Commented Nov 17, 2018 at 23:25
  • 1
    Consider this db-fiddle.com/f/tgfz1oA9KFPho8ezcQTEDL/0 interesting case for common named cities. Certianly there should be USA but at least one of the other countries shouldn't appear in the list. Commented Nov 17, 2018 at 23:41

1 Answer 1

2

You can always count:

select c.country
from disasters d join
     cities c
     on d.cname = c.cname
group by c.country
having count(distinct d.etype) = (select count(*) from events);
Sign up to request clarification or add additional context in comments.

2 Comments

I cant understand this goup by. You combined both tables together but you group only by table c?
Well, the country and event are not in the same table, so something has to be joined together.

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.