Bird
0
0

You want to write a PL/pgSQL function that returns the sum of all even numbers from 1 to a given integer n. Which approach correctly uses advanced PL/pgSQL features to achieve this?

hard📝 Application Q8 of 15
PostgreSQL - Advanced PL/pgSQL
You want to write a PL/pgSQL function that returns the sum of all even numbers from 1 to a given integer n. Which approach correctly uses advanced PL/pgSQL features to achieve this?
AWrite a single SQL SELECT without any procedural code
BDeclare a variable but do not initialize or update it
CUse a WHILE loop but skip checking for even numbers
DUse a FOR loop with an IF condition inside to check even numbers and accumulate sum
Step-by-Step Solution
Solution:
  1. Step 1: Understand the problem requirements

    We need to sum even numbers from 1 to n, which requires looping and conditional checks.
  2. Step 2: Identify correct PL/pgSQL approach

    A FOR loop with IF condition to check evenness and accumulate sum uses procedural features effectively.
  3. Final Answer:

    Use a FOR loop with an IF condition inside to check even numbers and accumulate sum -> Option D
  4. Quick Check:

    Loop + condition = correct advanced PL/pgSQL use [OK]
Quick Trick: Combine loops and conditions for complex logic [OK]
Common Mistakes:
  • Ignoring condition to check even numbers
  • Using SQL only without procedural logic
  • Not updating sum variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes