We use IF-ELSIF-ELSE to make decisions in our database code. It helps choose what to do based on different conditions.
IF-ELSIF-ELSE control flow in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
IF condition THEN -- statements ELSIF another_condition THEN -- statements ELSE -- statements END IF;
IF score >= 90 THEN grade := 'A'; ELSIF score >= 80 THEN grade := 'B'; ELSE grade := 'C'; END IF;
IF quantity = 0 THEN status := 'Out of stock'; ELSIF quantity < 10 THEN status := 'Low stock'; ELSE status := 'In stock'; END IF;
This code checks the age and prints the category using IF-ELSIF-ELSE.
DO $$
DECLARE
age INTEGER := 25;
category TEXT;
BEGIN
IF age < 13 THEN
category := 'Child';
ELSIF age < 20 THEN
category := 'Teenager';
ELSE
category := 'Adult';
END IF;
RAISE NOTICE 'Category: %', category;
END $$;Remember to end the IF block with END IF; in PostgreSQL.
ELSIF is used for additional conditions, not ELSE IF.
Use RAISE NOTICE to print messages inside DO blocks or functions.
IF-ELSIF-ELSE helps make choices based on conditions.
Use IF for the first test, ELSIF for more tests, and ELSE for the default.
Always close with END IF; in PostgreSQL.
Practice
What is the purpose of the ELSIF keyword in PostgreSQL's IF control flow?
Solution
Step 1: Understand the role of
TheIFandELSIFIFkeyword tests the first condition. If it is false,ELSIFallows testing another condition.Step 2: Differentiate
ELSIFfrom other keywordsELSIFis not for ending or unconditional execution; it is for additional conditional checks.Final Answer:
To test an additional condition if the previousIFcondition is false -> Option AQuick Check:
ELSIF= additional condition test [OK]
- Thinking ELSIF ends the IF block
- Confusing ELSIF with ELSE
- Using ELSIF without a preceding IF
Which of the following is the correct syntax to close an IF statement in PostgreSQL?
Solution
Step 1: Recall PostgreSQL block ending syntax
PostgreSQL requiresEND IF;to close anIFblock explicitly.Step 2: Compare options
ENDIF;andFINISH IF;are invalid.END;alone closes other blocks but notIF.Final Answer:
END IF; -> Option CQuick Check:
Close IF with END IF; [OK]
- Using END; alone to close IF
- Writing ENDIF; without space
- Forgetting to close IF blocks
Consider this PostgreSQL code snippet inside a function:
IF score >= 90 THEN result := 'A'; ELSIF score >= 80 THEN result := 'B'; ELSIF score >= 70 THEN result := 'C'; ELSE result := 'F'; END IF;
If score is 85, what will be the value of result after execution?
Solution
Step 1: Evaluate conditions in order for score = 85
Check if 85 >= 90? No. Then check 85 >= 80? Yes.Step 2: Assign result based on first true condition
Since 85 >= 80 is true,resultis set to 'B'. Remaining conditions are skipped.Final Answer:
'B' -> Option DQuick Check:
85 >= 80 = true, so result = 'B' [OK]
- Choosing 'A' because 85 is close to 90
- Ignoring order of conditions
- Assigning 'C' or 'F' incorrectly
Identify the error in this PostgreSQL IF block:
IF value > 10 THEN RAISE NOTICE 'Value is large'; ELSIF value < 5 RAISE NOTICE 'Value is small'; ELSE RAISE NOTICE 'Value is medium'; END IF;
Solution
Step 1: Check syntax of each condition
TheELSIFline lacks the requiredTHENkeyword after the condition.Step 2: Confirm other parts are correct
END IF;is present,RAISE NOTICEis valid, andELSEdoes not take a condition.Final Answer:
Missing THEN afterELSIF value < 5-> Option BQuick Check:
ELSIF must have THEN [OK]
- Omitting THEN after ELSIF
- Adding condition after ELSE
- Forgetting END IF;
You want to write a PostgreSQL function that returns 'Positive', 'Negative', or 'Zero' based on an integer input num. Which IF-ELSIF-ELSE block correctly implements this logic?
-- Options: A) IF num > 0 THEN RETURN 'Positive'; ELSIF num < 0 THEN RETURN 'Negative'; ELSE RETURN 'Zero'; END IF; B) IF num > 0 THEN RETURN 'Positive'; ELSEIF num < 0 THEN RETURN 'Negative'; ELSE RETURN 'Zero'; END IF; C) IF num > 0 THEN RETURN 'Positive'; ELSIF num < 0 THEN RETURN 'Negative'; ELSEIF num = 0 THEN RETURN 'Zero'; END IF; D) IF num > 0 THEN RETURN 'Positive'; IF num < 0 THEN RETURN 'Negative'; ELSE RETURN 'Zero'; END IF;
Solution
Step 1: Check keywords and structure
Correct use of IF, ELSIF, ELSE with proper syntax usesIF,ELSIF, andELSEcorrectly with proper endings.Step 2: Identify errors in other options
Uses invalid keyword ELSEIF instead of ELSIF uses invalidELSEIF. Uses ELSEIF after ELSE which is invalid usesELSEIFafterELSE. Nested IF without closing first IF properly nests IF without closing properly.Final Answer:
Correct use of IF, ELSIF, ELSE with proper syntax -> Option AQuick Check:
Use IF, ELSIF, ELSE; no ELSEIF [OK]
- Using ELSEIF instead of ELSIF
- Placing ELSEIF after ELSE
- Improper nesting without END IF;
