SQL TutorialLesson 41Free preview
SQL Case
This lesson is part of SQL · All levels
Summary
CASE gives branching logic within a query, similar to if-else in a programming language. The result could be a new column that displays the label under the conditions it meets. There is a simple form of case when and a system form with case expression when values.
Key points
- Case when... then... else... and then yield value based on the condition.
- The evaluation of the branch is done from top to bottom; the first appropriate branch is used.
- ELSE is optional; without ELSE the result is when there is no suitable branch.
- It can be used in SELECT, WHERE, ORDER BY, or GROUP BY.
- CASE form expression: CASE status when 1 THEN 'active'... END.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.