Skip to content
Advertisement

SQL non alphabetical order in WHERE IN

Let’s say I have this query:

the result is:

which is ordered by the alphabetical order

How can I get a result order by the index of appearance in the list?

basically I want this as a result:

Advertisement

Answer

This is quite a popular approach to sort things in SQL, so I’ve blogged about this example here. You would have to explicitly order by those values in your list, e.g. using a CASE expression:

Example on SQLFiddle

If you want to avoid repeating those literals, you could resort to this trick:

Example on SQLFiddle

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement