CASE expression
SQL's if-then-else: it returns one value when a condition holds and another otherwise.
In more detail
It turns a value into a label that can then be grouped on, such as 'out' when stock is zero and 'in' otherwise. Conditions are tried in order and the first true one wins; if none is true and there is no fallback, the result is NULL.
Example
CASE WHEN stock = 0 THEN 'out' ELSE 'in' END AS stock_stateRelated terms
- GROUP BY — The clause that splits rows into groups sharing the same values, so aggregates return one result per group.
- predicate — A function or condition that answers true or false about a value, used to filter, search or check data.
Taught in
- Grouping By Multiple Columns — PostgreSQL, Indexing & Relational Data Modeling