Jump to content

How to express this query..


php_novice2007

Recommended Posts

Hi all,

 

I'm doing a question from my text book and I'm having trouble with the following question:

 

Consider a table schema R(State, City, Popluation, GDP), express the query for "List all cities whose GDP is greater than the average of all State GDPs with population greater than 1 million" Make appropriate assumptions wherever necessary.

 

So far I've coem up with:

 

Select r1.City from R r1 where

r1.GDP >

(select r.State, avg(r.GDP) from R r

where r.Populatioin > 1000000

group by r.State)

 

I know this is wrong since my inner query should be returning a table of state and their average GDPs the the '>' operator is only for a single row... :(

 

Thanks for any help~!

Link to comment
Share on other sites

try this

 

Select city  from R where gdp>(select avg(gdp) from R where state=ANY(select if(sum(population)>100000, state,'xx') from R group by state));

 

 

there might be a better way but this what i got..........

 

why don't u go for some normalization so that it coule be very easy when u need to retrive the data like this...............

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.