In this article, you will learn how to use the PostgreSQL CASE conditional expression to form conditional queries.
The PostgreSQL CASE expression is the same as IF/ELSE statement in other programming languages.
General Usage:
1 2 3 4 5 6 | CASE WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2 [WHEN ...] [ELSE else_result] END |
-If the lenght of the film is between 0 and 60 minutes, ‘The lenght of the film seems a bit short’.
-If the lenght of the film is between 60 and 120 minutes, ‘The lenght of the film is good’.
-If the movie is longer, ‘Movie lenght is too long’.
Let’s write our plgpsql command according to the above conditions.