Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query ordered alphabetically

I have a table as below,

ID    Description
--------------------
1     Bacteria
2     Cell Lines
3     Compounds
4     Virus
5     Others
6     AntiBody

What I want is a single SQL query, ordered alphabetically but have 'Other' (ID 5) as the last record. Is that even possible?

Any help would greatly appreciated. Thanks.

like image 839
MadushM Avatar asked Dec 14 '11 22:12

MadushM


2 Answers

SELECT ID, Description
    FROM YourTable
    ORDER BY CASE WHEN ID = 5 THEN 1 ELSE 0 END,
             Description
like image 174
Joe Stefanelli Avatar answered Oct 08 '22 22:10

Joe Stefanelli


SELECT ID, Description
FROM yourtable
ORDER BY CASE WHEN Description = 'Others' THEN 1 ELSE 0 END, Description
like image 22
Mark Byers Avatar answered Oct 08 '22 23:10

Mark Byers