0
0
PostgreSQLquery~5 mins

CASE in PL/pgSQL in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the CASE statement in PL/pgSQL?
The CASE statement lets you choose between different actions based on conditions, like a traffic light deciding what to do next.
Click to reveal answer
beginner
Write the basic syntax of a simple CASE statement in PL/pgSQL.
CASE expression WHEN value1 THEN result1 WHEN value2 THEN result2 ELSE default_result END;
Click to reveal answer
intermediate
How does a searched CASE differ from a simple CASE in PL/pgSQL?
A searched CASE checks conditions that can be any boolean expressions, while a simple CASE compares one expression to fixed values.
Click to reveal answer
beginner
Can you use CASE inside a PL/pgSQL function to return different results?
Yes, CASE is often used inside functions to return different outputs based on input or other conditions.
Click to reveal answer
intermediate
What happens if no WHEN condition matches and there is no ELSE in a CASE statement?
The CASE returns NULL if no WHEN matches and there is no ELSE clause.
Click to reveal answer
Which keyword starts a CASE statement in PL/pgSQL?
ACASE
BIF
CSWITCH
DSELECT
In a simple CASE, what does the expression after CASE do?
AIt defines the ELSE result
BIt is ignored
CIt must be a boolean
DIt is compared to each WHEN value
What does ELSE do in a CASE statement?
AEnds the CASE statement
BDefines the default result if no WHEN matches
CStarts a new CASE
DIs required for CASE to work
Which of these is a searched CASE example?
ACASE WHEN x > 10 THEN 'big' ELSE 'small' END
BCASE x WHEN 1 THEN 'one' ELSE 'other' END
CCASE x END
DCASE WHEN THEN END
What result does CASE return if no WHEN matches and ELSE is missing?
AFirst WHEN result
BError
CNULL
DEmpty string
Explain how to use a simple CASE statement in PL/pgSQL with an example.
Think of CASE as a way to pick a result based on one value.
You got /3 concepts.
    Describe the difference between simple CASE and searched CASE in PL/pgSQL.
    One compares values directly, the other checks conditions.
    You got /3 concepts.