Click here to Skip to main content
15,885,546 members
Articles / Database Development / MySQL

MySQL: Condition Based Sorting

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
9 Oct 2014CPOL2 min read 9.3K   8   3
Sort the result set as per your need

AscDesc

Introduction

Sometimes, we need to sort a result set based on some conditions. Let’s take few real time scenarios and then try to resolve these with the help of CASE WHEN.

Scenario 1

Sometimes, we need an element to be placed at the bottom of the list. For example, a Dropdownlist containing educational qualifications may contain an item “Other”. While showing all the educational qualifications from database, we may need this particular element (i.e, “Other”) to be moved to the bottom of the list. If we try to sort this list using usual ORDER BY keyword, then we may not get the desired sequence. To get the desired result, we need to use CASE WHEN keyword along with the ORDER BY. Let’s illustrate this with an example:

Simple ORDER BY

SQL
SELECT * FROM
(
 SELECT 'I.Sc.' AS Qualification
 UNION
 SELECT 'B.Sc.' AS Qualification
 UNION
 SELECT 'Other' AS Qualification
 UNION
 SELECT 'P.G.' AS Qualification
 UNION
 SELECT 'MCA' AS Qualification
 UNION
 SELECT 'DCA' AS Qualification
 UNION
 SELECT 'P.H.D' AS Qualification
) AS TBL
ORDER BY Qualification

Output

5

ORDER BY with CASE WHEN

SQL
SELECT * FROM
(
 SELECT 'I.Sc.' AS Qualification
 UNION
 SELECT 'B.Sc.' AS Qualification
 UNION
 SELECT 'Other' AS Qualification
 UNION
 SELECT 'P.G.' AS Qualification
 UNION
 SELECT 'MCA' AS Qualification
 UNION
 SELECT 'DCA' AS Qualification
 UNION
 SELECT 'P.H.D' AS Qualification
) AS TBL
ORDER BY CASE WHEN Qualification='Other' 
THEN 'ZZZZZ' ELSE Qualification END

Output

6

Explanation

Here, we have set the value of item “Other” as “ZZZZZ” while the sorting is being done. Generally “ZZZZZ” will be the last item in any real world list and hence will move to the last.

Scenario 2

In some other cases, we may need the desired item to be moved to the top of the result set. An example for this situation can be, a Dropdownlist containing items for Book Category. The list may have an item with text “General”, which is meant for those books whose category is not clearly known. Let’s assume that most of the books are supposed to be of this category. Then, we have to place this item in the first index of the Dropdownlist so that it will be easier for user to pick the item. Now, we need a little modification in the ORDER BY clause as follows:

SQL
ORDER BY CASE WHEN Qualification='General' THEN NULL ELSE Qualification END

Please share your feedback! Thanks!

You may also like:

CodeProject

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Jay Bardeleben22-Dec-14 3:57
professionalJay Bardeleben22-Dec-14 3:57 
QuestionUseful one Pin
Prava-MFS10-Oct-14 3:38
professionalPrava-MFS10-Oct-14 3:38 
AnswerRe: Useful one Pin
Suvendu Shekhar Giri10-Oct-14 18:57
professionalSuvendu Shekhar Giri10-Oct-14 18:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.