1

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.

0

4 Answers 4

9

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

order by Country, City
Sign up to request clarification or add additional context in comments.

1 Comment

You may also specify direction on a per-column basis. For example: 'ORDER BY Country, City DESC
5

Try the following

Order By Country, City

Comments

5

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

SELECT * FROM my_table ORDER BY Country, City;

Comments

4

It's enough to separate them by a comma ,

SELECT * FROM Table ORDER BY Country, City

Comments

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.