🔀
Intermediate About 20 min

CASE Expressions

Add if-else logic to queries — bucket values, conditionally aggregate, and create custom sort rules.

What you'll learn

  • Add if-else logic to queries with CASE WHEN … THEN … ELSE … END
  • Bucket continuous values into categories for reporting
  • Use CASE inside aggregate functions for conditional counting
  • Replace CASE-based aggregation with PostgreSQL FILTER clause
  • Create custom sort orders with CASE in ORDER BY