Skip navigation

Defining a Custom Sort Order


From the Customers table in the Northwind sample database, I want to return the list of customers by country in the custom sort order that Figure 1 shows. How can I define that sort order?

To define the custom sort order, you can use a CASE expression in the ORDER BY clause, as Listing 1 shows. Alternatively, you can create an auxiliary table in which you store your custom sort order, then join the auxiliary table to the Customers table, as Listing 2 shows.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.