0
0
PostgreSQLquery~5 mins

CASE expression in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the CASE expression in PostgreSQL?
The CASE expression lets you perform conditional logic inside SQL queries, returning different values based on conditions, similar to an IF-THEN-ELSE in programming.
Click to reveal answer
beginner
Write the basic syntax of a simple CASE expression in PostgreSQL.
CASE expression syntax:<br>CASE input_expression<br> WHEN value1 THEN result1<br> WHEN value2 THEN result2<br> ELSE default_result<br>END
Click to reveal answer
intermediate
How does a searched CASE expression differ from a simple CASE expression?
A searched CASE tests multiple boolean conditions directly (WHEN condition THEN result), while a simple CASE compares one expression to multiple values.
Click to reveal answer
beginner
What happens if no WHEN condition matches and there is no ELSE clause in a CASE expression?
If no WHEN matches and ELSE is missing, the CASE expression returns NULL.
Click to reveal answer
beginner
Can CASE expressions be used in SELECT, WHERE, and ORDER BY clauses in PostgreSQL?
Yes, CASE expressions can be used anywhere an expression is allowed, including SELECT, WHERE, ORDER BY, and more.
Click to reveal answer
What does the CASE expression return if no WHEN condition matches and ELSE is provided?
AAn error
BThe first WHEN condition's value
CNULL
DThe value in ELSE clause
Which of the following is a valid use of CASE in PostgreSQL?
AInside SELECT to change output values
BOnly in WHERE clause
COnly in ORDER BY clause
DOnly in UPDATE statements
What keyword starts a searched CASE expression?
AWHEN
BCASE
CIF
DELSE
In a simple CASE expression, what is compared to the WHEN values?
AThe ELSE clause
BBoolean conditions
CAn input expression
DThe SELECT statement
What is the result of a CASE expression without ELSE and no matching WHEN?
ANULL
B0
CError
DFirst WHEN value
Explain how to use a searched CASE expression in PostgreSQL with an example.
Think about checking conditions like age > 18 or status = 'active'.
You got /5 concepts.
    Describe the difference between simple CASE and searched CASE expressions in PostgreSQL.
    One compares values, the other checks conditions.
    You got /4 concepts.