PostgreSQL SyntaxLesson 42Free preview
PostgreSQL CASE
This lesson is part of PostgreSQL · All levels
Summary
The CASE expression allows you to write a case-by-case condition in SQL, similar to a switch in a programming language. PostgreSQL provides a simple form of CASE that compares one expression as well as a searched CASE that contains the full condition for each WHEN. The results can be used for SELECT, ORDER BY, GROUP BY, even UPDATE.
Key points
- CASE expresses the logic of the branches within SQL queries.
- Searched CASE uses the condition WHEN, simple CASE to compare a value.
- ELSE is optional; no matches and no ELSE results.
- It can be used on SELECT, ORDER BY, GROUP BY, and UPDATE.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.