Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
✗ Incorrect
The CASE statement always starts with the keyword CASE.
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
✗ Incorrect
The expression after CASE is compared to each WHEN value to find a match.
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
✗ Incorrect
ELSE provides a default result if none of the WHEN conditions match.
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
✗ Incorrect
A searched CASE uses WHEN with conditions like 'x > 10'.
What result does CASE return if no WHEN matches and ELSE is missing?
AFirst WHEN result
BError
CNULL
DEmpty string
✗ Incorrect
Without ELSE, CASE returns NULL if no WHEN matches.
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.
Practice
(1/5)
1. What is the main purpose of using CASE in PL/pgSQL?
easy
A. To choose different actions based on conditions
B. To create loops that repeat actions
C. To define new tables in the database
D. To permanently store data in variables
Solution
Step 1: Understand the role of CASE
CASE is used to select one action from many based on conditions, like a traffic light deciding when to stop or go.
Step 2: Compare with other options
Loops repeat actions, table creation defines structure, and variables store data, none of which is the main role of CASE.
Final Answer:
To choose different actions based on conditions -> Option A
Quick Check:
CASE chooses actions based on conditions [OK]
Hint: CASE picks actions by conditions, not loops or storage [OK]
Common Mistakes:
Confusing CASE with loops
Thinking CASE creates tables
Assuming CASE stores data permanently
2. Which of the following is the correct way to end a CASE block in PL/pgSQL?
easy
A. END;
B. STOP;
C. FINISH CASE;
D. END CASE;
Solution
Step 1: Recall PL/pgSQL syntax for CASE
In PL/pgSQL, a CASE block must be closed with END CASE; to mark its end clearly.
Step 2: Check other options
END; ends blocks like functions, but CASE specifically needs END CASE;. FINISH CASE; and STOP; are invalid keywords.
Final Answer:
END CASE; -> Option D
Quick Check:
CASE ends with END CASE; [OK]
Hint: Always close CASE with END CASE; in PL/pgSQL [OK]
Common Mistakes:
Using END; alone to close CASE
Writing FINISH CASE; which is invalid
Using STOP; which is not a PL/pgSQL keyword
3. Consider this PL/pgSQL snippet:
DECLARE
grade CHAR := 'B';
result TEXT;
BEGIN
CASE grade
WHEN 'A' THEN result := 'Excellent';
WHEN 'B' THEN result := 'Good';
ELSE result := 'Average';
END CASE;
RETURN result;
END;
What will be the returned value?
medium
A. 'Good'
B. 'Average'
C. 'Excellent'
D. NULL
Solution
Step 1: Identify the value of grade
The variable grade is set to 'B'.
Step 2: Match grade in CASE
CASE checks 'B', matches the second WHEN clause, so result becomes 'Good'.
Final Answer:
'Good' -> Option A
Quick Check:
grade 'B' returns 'Good' [OK]
Hint: Match CASE value to WHEN clause for output [OK]
Common Mistakes:
Choosing ELSE when a WHEN matches
Confusing variable assignment inside CASE
Assuming NULL if no ELSE present
4. Identify the error in this PL/pgSQL CASE block:
DECLARE
score INT := 85;
grade TEXT;
BEGIN
CASE
WHEN score >= 90 THEN grade := 'A';
WHEN score >= 80 THEN grade := 'B';
ELSE grade := 'C';
END;
RETURN grade;
END;
medium
A. Incorrect variable declaration
B. Invalid comparison operators
C. Missing END CASE; to close CASE block
D. No ELSE clause present
Solution
Step 1: Check CASE block ending
The CASE block is closed with END; but PL/pgSQL requires END CASE; to close CASE.
Step 2: Verify other parts
Variable declarations and comparisons are correct, and ELSE clause is present.
Final Answer:
Missing END CASE; to close CASE block -> Option C
Quick Check:
CASE must end with END CASE; [OK]
Hint: Close CASE with END CASE;, not just END; [OK]
Common Mistakes:
Using END; instead of END CASE;
Thinking ELSE is optional here
Misreading comparison operators
5. You want to write a PL/pgSQL function that returns 'Pass' if a student's score is 50 or more, 'Fail' if below 50, and 'Invalid' if the score is NULL. Which CASE structure correctly implements this?
hard
A.
CASE score
WHEN NULL THEN RETURN 'Invalid';
WHEN >= 50 THEN RETURN 'Pass';
ELSE RETURN 'Fail';
END CASE;
B.
CASE
WHEN score IS NULL THEN RETURN 'Invalid';
WHEN score >= 50 THEN RETURN 'Pass';
ELSE RETURN 'Fail';
END CASE;
C.
CASE
WHEN score >= 50 THEN RETURN 'Pass';
WHEN score IS NULL THEN RETURN 'Fail';
ELSE RETURN 'Invalid';
END CASE;
D.
CASE score
WHEN score >= 50 THEN RETURN 'Pass';
WHEN score < 50 THEN RETURN 'Fail';
ELSE RETURN 'Invalid';
END CASE;
Solution
Step 1: Handle NULL explicitly
Since NULL cannot be matched by simple WHEN, use WHEN score IS NULL to check NULL values.
Step 2: Order conditions correctly
Check NULL first, then score >= 50 for 'Pass', else 'Fail'. This matches
CASE
WHEN score IS NULL THEN RETURN 'Invalid';
WHEN score >= 50 THEN RETURN 'Pass';
ELSE RETURN 'Fail';
END CASE;
.
Final Answer:
CASE with WHEN score IS NULL, then score >= 50, else Fail -> Option B
Quick Check:
Use IS NULL to check NULL in CASE [OK]
Hint: Use WHEN score IS NULL to test NULL in CASE [OK]