Customized sorting order

suggest change

To sort this Employee table by department, you would use ORDER BY Department. If you want an order that is not alphabetical, map the Department values to numbers that sort the way you want. A CASE expression can do that. Rows with the same mapped value are not ordered further; their relative order is not guaranteed.

Name | Department |
—–– | ––––– |
Hasan | IT |
Yusuf | HR |
Hillary | HR |
Joe | IT |
Merry | HR |
Ken | Accountant |

SELECT *
FROM Employee
ORDER BY CASE Department
         WHEN 'HR'         THEN 1
         WHEN 'Accountant' THEN 2
         ELSE                   3
         END;

Name | Department |
—–– | ––––––– |
Yusuf | HR |
Hillary | HR |
Merry | HR |
Ken | Accountant |
Hasan | IT |
Joe | IT |

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents