Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I want my query to return the rows of the table where a column contains a specific value first, and then return the rest of the rows alphabetized.

If I have a table something like this example:

 - Table   :   Users

 - id - name -  city

 - 1    George  Seattle

 - 2    Sam     Miami

 - 3    John    New York

 - 4    Amy     New York

 - 5    Eric    Chicago

 - 6    Nick    New York

And using that table I want my query to return the rows which contain New York first, and then the rest of the rows alphabetized by the city. Is this possible to do using only one query?

1 Answer

0 votes
by (40.7k points)

On SQL Server, DB2, Oracle and many other database systems, you can use the below code:

ORDER BY CASE WHEN city = 'New York' THEN 1 ELSE 2 END, city    

Related questions

0 votes
1 answer
asked Jul 17, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...