books with columns id (integer), title (text), and price (numeric).price_category that takes a numeric price and returns a text category using IF-ELSIF-ELSE.Jump into concepts and practice - no test required
books with columns id (integer), title (text), and price (numeric).price_category that takes a numeric price and returns a text category using IF-ELSIF-ELSE.books table and insert databooks with columns id (integer), title (text), and price (numeric). Then insert these three books exactly: (1, 'Book A', 5), (2, 'Book B', 15), and (3, 'Book C', 25).Use CREATE TABLE to define the table and INSERT INTO to add the three books with exact values.
price_category with IF-ELSIF-ELSEprice_category that takes a numeric parameter p_price and returns a text. Use IF-ELSIF-ELSE control flow to return 'Cheap' if p_price < 10, 'Moderate' if p_price <= 20, and 'Expensive' otherwise.Use CREATE OR REPLACE FUNCTION with plpgsql language. Use IF, ELSIF, and ELSE to check the price ranges and return the correct category.
title and its price category by calling the price_category function on the price column.Use SELECT with the price_category(price) function to get the category for each book.
price_category function explaining that it classifies book prices into categories using IF-ELSIF-ELSE control flow.Use -- to add a comment above the function definition.
What is the purpose of the ELSIF keyword in PostgreSQL's IF control flow?
IF and ELSIFIF keyword tests the first condition. If it is false, ELSIF allows testing another condition.ELSIF from other keywordsELSIF is not for ending or unconditional execution; it is for additional conditional checks.IF condition is false -> Option AELSIF = additional condition test [OK]Which of the following is the correct syntax to close an IF statement in PostgreSQL?
END IF; to close an IF block explicitly.ENDIF; and FINISH IF; are invalid. END; alone closes other blocks but not IF.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?
result is set to 'B'. Remaining conditions are skipped.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;
ELSIF line lacks the required THEN keyword after the condition.END IF; is present, RAISE NOTICE is valid, and ELSE does not take a condition.ELSIF value < 5 -> Option BYou 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;
IF, ELSIF, and ELSE correctly with proper endings.ELSEIF. Uses ELSEIF after ELSE which is invalid uses ELSEIF after ELSE. Nested IF without closing first IF properly nests IF without closing properly.