Example
To sort this table Employee
by department, you would use ORDER BY Department
.
However, if you want a different sort order that is not alphabetical, you have to map the Department
values into different values that sort correctly; this can be done with a CASE expression:
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 |