Customized sorting order
suggest changeTo 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 |
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents