CASE WHEN price >= 300 THEN 'premium' ELSE 'standard' END creates a new category column.
SUM(CASE WHEN status = 'refunded' THEN 1 ELSE 0 END) counts refunds inside a grouped query, which makes shares and rates easy.
Worked example
Label each product as premium (€300 or more) or standard.
SELECT name, CASE WHEN price >= 300 THEN 'premium' ELSE 'standard' END AS tier FROM products ORDER BY price DESC LIMIT 6;
| name | tier |
|---|---|
| 14-inch laptop | premium |
| Mini PC | premium |
| Standing desk | premium |
| Ergonomic chair | premium |
| 27-inch monitor | premium |
| Noise-cancelling headphones | standard |
Common mistakes
- Forgetting ELSE, which leaves NULL for every unmatched row.
- Overlapping conditions: CASE uses the first one that matches.