The Basics · CASE WHEN
8. Salary bands
Concept: CASE WHENshow
CASE WHEN turns values into labels: CASE WHEN cond1 THEN 'a' WHEN cond2 THEN 'b' ELSE 'c' END. Postgres checks the WHENs top to bottom and uses the first one that's true, so put the strictest condition first. Without ELSE, unmatched rows get NULL.
Example (different data):
SELECT title, price,
CASE WHEN price >= 100 THEN 'premium'
WHEN price >= 20 THEN 'standard'
ELSE 'budget' END AS tier
FROM products;Your task
HR is publishing pay bands and wants every employee tagged with their band before the comp review.
Return name, salary and pay_band for every employee, where pay_band is 'high' for a salary of at least 150000, 'mid' for at least 100000, and 'entry' otherwise. Sort by salary (highest first), then name.
Solution unlocks after 3 more tries
Starting Postgres in your browser…
query.sql
Output
Write a query and press Run Query (Ctrl/⌘ + Enter).