xarzu
xarzu

Reputation: 9479

double sort -- sort of -- in sql

I have a database. It has two columns. Let's say one colum is "Country" and the other column is "City".

Now, one can query the database and have the query end with "order by Country" and the output will be such that the rows are sorted alphabetically by Country.

But what if I wanted them to be primarily sorted by country name and then with the rows for each country, the output is sorted secondarily by city name. How would that query look like?

as a test, I have opened sql server and at the end of the select top 1000 query I added "order by country" and there was no syntax error. But if I add "order by Country order by City", it does not like it. Adding a comma does nto help. Adding "and" does not help either.

Upvotes: 1

Views: 8494

Answers (4)

user532231
user532231

Reputation:

It's enough to separate them by a comma ,

SELECT * FROM Table ORDER BY Country, City

Upvotes: 4

Steve Prentice
Steve Prentice

Reputation: 23514

You can specify more than one column to order by. Just separate them with a ,.

SELECT * FROM my_table ORDER BY Country, City;

Upvotes: 5

devdigital
devdigital

Reputation: 34349

Try the following

Order By Country, City

Upvotes: 5

Andomar
Andomar

Reputation: 238086

You can order on multiple columns by separating them with a comma:

order by Country, City

Upvotes: 9

Related Questions