Order by COUNT per value

Enkay picture Enkay · Feb 17, 2010 · Viewed 79.9k times · Source

I have a table which stores IDs and the city where the store is located.

I want to list all the stores starting with the stores that are in the city where there are the most stores.

TABLE

ID CITY
1  NYC
2  BOS
3  BOS
4  NYC
5  NYC

The output I want is the following since I have the most stores in NYC, I want all the NYC location to be listed first.

1  NYC
4  NYC
5  NYC
2  BOS
3  BOS

Answer

MindStalker picture MindStalker · Feb 17, 2010
SELECT count(City), City
FROM table
GROUP BY City
ORDER BY count(City);

OR

SELECT count(City) as count, City
FROM table
GROUP BY City
ORDER BY count;

Ahh, sorry, I was misinterpreting your question. I believe Peter Langs answer was the correct one.