I have table which have following fields with other fields: - State (50 US states) - Cities (All US cities0 - pop_2000 (2000 City population) I have total about 25000 records. I want to write a SQL statement which will select Top 10 Cities from each State based on highest population of the cities. I tried different Query without any success. Anyone has idea how to do this. I am using mySQL version 4.x. Thanks,
I never see mySql. I use sql server. But the following code should work as well on mysql. try and let me know the result select state,cities, pop_2000 from t tou where cities in ( select top 10 Cities from t ti where ti.state=tou.state order by pop_2000)
I am sorry . for highest population it should be select state,cities, pop_2000 from t tou where cities in ( select top 10 Cities from t ti where ti.state=tou.state order by pop_2000 desc) You have huge data, it might be slower on your case