Query
SELECT id, status, date, total
FROM orders
ORDER BY
CASE status
WHEN 'pending' THEN 1
WHEN 'shipped' THEN 2
WHEN 'cancelled' THEN 3
ELSE 4
END,
date DESC,
id DESC
LIMIT 8;The status priority is evaluated first. Then, within each priority, date DESC puts the most recent orders at the top and id DESC breaks ties.
How it works
CASE produces a value. Because ORDER BY can sort by expressions, you can translate text categories into numbers that reflect a specific priority.
The number does not need to appear in the output. It only decides which rows come first.
Cases that change the result
Simple CASE or searched CASE
When you compare a single column with specific values, the simple form is usually easier to read:
CASE status
WHEN 'pending' THEN 1
WHEN 'shipped' THEN 2
ELSE 3
ENDIf the priority depends on ranges or on several columns, use full conditions:
CASE
WHEN status = 'pending' AND total >= 200 THEN 1
WHEN status = 'pending' THEN 2
ELSE 3
ENDInclude ELSE for unexpected categories
If you omit ELSE, a row that matches nothing gets NULL as its sort key. Because the default position of NULL varies between engines, that row may end up somewhere unexpected. An explicit ELSE removes that ambiguity.
Avoid this confusion
Sorting only by the CASE
Several rows can share a priority. Add a date, a name, an id or another consistent key to get a stable order within each group.
Turning a business rule into magic numbers
1, 2 and 3 must come with readable branches. Do not bury the intent in an expression that is hard to maintain.
A business priority needs a complete order
The priority expression orders the categories, not every row within them. If two incidents are urgent, both get the same weight and you still need a rule to decide which goes first. The example adds the id as a tiebreaker; a support system might prefer the opening date followed by a unique key.
Include an unexpected category and a null value in your tests. Without a fallback branch, their weight may be null and land in a different position depending on the engine. Decide explicitly whether an unknown category should go last or fail a validation. Sorting does not fix invalid data: it only decides where it is shown.
When the priority list changes often, a table of categories with weights can be easier to maintain than editing queries scattered around. Keep one row per category so that joining does not multiply incidents. With pagination, a sort with a stable tiebreaker stops equal rows from changing places at random; even so, concurrent writes can shift later pages and need a separate consistency decision.
Self-contained case to check in SQLite
Two incidents share a priority. The id orders the tie and ELSE sends the unknown category to the end.
WITH issues(id, priority) AS (
VALUES (4, 'normal'), (2, 'urgent'), (1, 'urgent'), (3, 'other')
)
SELECT id, priority FROM issues
ORDER BY CASE priority WHEN 'urgent' THEN 1 WHEN 'normal' THEN 2 ELSE 3 END, id;| id | priority |
|---|---|
| 1 | urgent |
| 2 | urgent |
| 4 | normal |
| 3 | other |
The weight groups the priorities in the desired order, and the second key decides the position of the two urgent rows.
Check your interpretation: Which part of the rule would be incomplete if you removed id from the ORDER BY?
Show answer
The order between rows with the same priority: the two urgent ones (1 and 2) could come out in any order and change between runs.
Technical questions: PostgreSQL: SELECT, columns and aliases (opens in a new tab).