Bird
Raised Fist0
PostgreSQLquery~20 mins

RETURN and RETURN NEXT in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Master of RETURN and RETURN NEXT
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this function using RETURN NEXT?
Consider the following PostgreSQL function that returns a set of integers using RETURN NEXT. What will be the output when calling SELECT * FROM generate_numbers();?
PostgreSQL
CREATE OR REPLACE FUNCTION generate_numbers() RETURNS SETOF INTEGER AS $$
DECLARE
  i INTEGER := 1;
BEGIN
  WHILE i <= 3 LOOP
    RETURN NEXT i;
    i := i + 1;
  END LOOP;
END;
$$ LANGUAGE plpgsql;
A1, 2, 3
B3
CEmpty result
D1
Attempts:
2 left
💡 Hint
RETURN NEXT adds a row to the result set without ending the function.
query_result
intermediate
2:00remaining
What happens if RETURN is used inside a loop?
Given this PostgreSQL function, what will be the output of SELECT * FROM return_in_loop();?
PostgreSQL
CREATE OR REPLACE FUNCTION return_in_loop() RETURNS SETOF INTEGER AS $$
DECLARE
  i INTEGER := 1;
BEGIN
  WHILE i <= 3 LOOP
    RETURN i;
    i := i + 1;
  END LOOP;
END;
$$ LANGUAGE plpgsql;
A1, 2, 3
B1
C3
DEmpty result
Attempts:
2 left
💡 Hint
RETURN immediately ends the function and returns a single value.
📝 Syntax
advanced
2:00remaining
Which function definition correctly uses RETURN NEXT to return multiple rows?
Select the function that correctly returns multiple rows using RETURN NEXT in PostgreSQL.
ACREATE FUNCTION f() RETURNS SETOF INTEGER AS $$ BEGIN RETURN NEXT 1; RETURN NEXT 2; RETURN; END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION f() RETURNS SETOF INTEGER AS $$ BEGIN RETURN 1; RETURN 2; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION f() RETURNS INTEGER AS $$ BEGIN RETURN NEXT 1; RETURN NEXT 2; END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION f() RETURNS SETOF INTEGER AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
RETURN NEXT is used to add rows to a set-returning function.
🔧 Debug
advanced
2:00remaining
Why does this function return no rows?
This function is intended to return numbers 1 to 3 using RETURN NEXT, but returns no rows. Why?
PostgreSQL
CREATE OR REPLACE FUNCTION faulty_function() RETURNS SETOF INTEGER AS $$
DECLARE
  i INTEGER := 1;
BEGIN
  WHILE i <= 3 LOOP
    RETURN NEXT i;
    RETURN;
    i := i + 1;
  END LOOP;
END;
$$ LANGUAGE plpgsql;
ARETURN NEXT cannot be used inside loops.
BThe variable i is not incremented, causing infinite loop.
CThe RETURN statement after RETURN NEXT ends the function on first iteration.
DThe function lacks a RETURN QUERY statement.
Attempts:
2 left
💡 Hint
RETURN ends the function immediately, stopping further output.
🧠 Conceptual
expert
2:00remaining
How do RETURN and RETURN NEXT differ in set-returning functions?
In PostgreSQL set-returning functions, what is the key difference between RETURN and RETURN NEXT?
ARETURN adds a row to the result set; RETURN NEXT ends the function immediately.
BRETURN is used only in SQL functions; RETURN NEXT only in PL/pgSQL.
CBoth RETURN and RETURN NEXT add rows to the result set but differ in syntax.
DRETURN ends the function and returns a single value; RETURN NEXT adds a row to the result set and continues.
Attempts:
2 left
💡 Hint
Think about when the function stops returning rows.

Practice

(1/5)
1. What does the RETURN statement do in a PostgreSQL function?
easy
A. It loops through all rows in a table.
B. It adds a row to the output but keeps the function running.
C. It sends back one result and stops the function.
D. It creates a new table in the database.

Solution

  1. Step 1: Understand the purpose of RETURN

    The RETURN statement immediately sends a single result back to the caller and ends the function execution.
  2. Step 2: Compare with RETURN NEXT

    Unlike RETURN NEXT, which adds rows and continues, RETURN stops the function after sending one result.
  3. Final Answer:

    It sends back one result and stops the function. -> Option C
  4. Quick Check:

    RETURN = sends one result and stops [OK]
Hint: RETURN sends one result and stops function immediately [OK]
Common Mistakes:
  • Confusing RETURN with RETURN NEXT
  • Thinking RETURN returns multiple rows
  • Assuming RETURN continues function execution
2. Which of the following is the correct syntax to return multiple rows from a PostgreSQL function?
easy
A. Use RETURN NEXT inside a loop to add each row to the output.
B. Use RETURN ROW to return multiple rows.
C. Use RETURN ALL to return all rows at once.
D. Use RETURN inside a loop to return each row.

Solution

  1. Step 1: Identify how to return multiple rows

    RETURN NEXT is used inside loops to add each row to the output without stopping the function.
  2. Step 2: Check syntax options

    RETURN alone stops the function after one row; RETURN ALL and RETURN ROW are not valid PostgreSQL syntax.
  3. Final Answer:

    Use RETURN NEXT inside a loop to add each row to the output. -> Option A
  4. Quick Check:

    RETURN NEXT = add rows, keep running [OK]
Hint: Use RETURN NEXT in loops to return multiple rows [OK]
Common Mistakes:
  • Using RETURN instead of RETURN NEXT for multiple rows
  • Assuming RETURN ALL or RETURN ROW exist
  • Not placing RETURN NEXT inside a loop
3. Consider this PostgreSQL function snippet:
FOR i IN 1..3 LOOP
  RETURN NEXT i;
END LOOP;
RETURN;

What will be the output when this function is called?
medium
A. Syntax error due to RETURN NEXT usage
B. [3]
C. No output, function ends without returning
D. [1, 2, 3]

Solution

  1. Step 1: Analyze the loop with RETURN NEXT

    The loop runs from 1 to 3, and each iteration adds the current number to the output using RETURN NEXT.
  2. Step 2: Understand the final RETURN

    The final RETURN ends the function after all rows have been added, so the output is all numbers collected.
  3. Final Answer:

    [1, 2, 3] -> Option D
  4. Quick Check:

    RETURN NEXT adds rows; final RETURN stops function [OK]
Hint: RETURN NEXT inside loop collects rows; final RETURN stops [OK]
Common Mistakes:
  • Thinking only last value is returned
  • Confusing RETURN NEXT with RETURN
  • Expecting syntax error from RETURN NEXT
4. You wrote this function:
CREATE FUNCTION test_func() RETURNS SETOF integer AS $$
DECLARE
  i integer := 1;
BEGIN
  RETURN i;
  RETURN NEXT i + 1;
END;
$$ LANGUAGE plpgsql;

What is the problem with this function?
medium
A. RETURN NEXT is used after RETURN, so it never executes.
B. RETURN cannot be used in functions returning SETOF.
C. Variable i is not initialized properly.
D. Function lacks a LOOP to return multiple rows.

Solution

  1. Step 1: Check order of RETURN and RETURN NEXT

    RETURN immediately ends the function, so RETURN NEXT after it never runs.
  2. Step 2: Understand function behavior

    Because RETURN is first, only one row is returned and the rest is ignored.
  3. Final Answer:

    RETURN NEXT is used after RETURN, so it never executes. -> Option A
  4. Quick Check:

    RETURN stops function; code after it is skipped [OK]
Hint: RETURN stops function; code after it won't run [OK]
Common Mistakes:
  • Assuming RETURN NEXT runs after RETURN
  • Thinking RETURN can't be used in SETOF functions
  • Believing variable initialization causes error
5. You want to create a function that returns all even numbers from 1 to 10. Which of these function bodies correctly uses RETURN NEXT and RETURN to achieve this?
hard
A.
FOR i IN 1..10 LOOP
  IF i % 2 = 0 THEN
    RETURN i;
  END IF;
END LOOP;
B.
FOR i IN 1..10 LOOP
  IF i % 2 = 0 THEN
    RETURN NEXT i;
  END IF;
END LOOP;
RETURN;
C.
FOR i IN 1..10 LOOP
  RETURN NEXT i;
END LOOP;
RETURN;
D.
RETURN NEXT 2;
RETURN 4;
RETURN NEXT 6;
RETURN NEXT 8;
RETURN NEXT 10;
RETURN;

Solution

  1. Step 1: Identify correct use of RETURN NEXT in loop with condition

    FOR i IN 1..10 LOOP
      IF i % 2 = 0 THEN
        RETURN NEXT i;
      END IF;
    END LOOP;
    RETURN;
    loops 1 to 10, adds only even numbers with RETURN NEXT, then ends with RETURN.
  2. Step 2: Check other options for errors

    FOR i IN 1..10 LOOP
      IF i % 2 = 0 THEN
        RETURN i;
      END IF;
    END LOOP;
    uses RETURN inside loop, stopping after first even number.
    FOR i IN 1..10 LOOP
      RETURN NEXT i;
    END LOOP;
    RETURN;
    returns all numbers, not just even.
    RETURN NEXT 2;
    RETURN 4;
    RETURN NEXT 6;
    RETURN NEXT 8;
    RETURN NEXT 10;
    RETURN;
    mixes RETURN NEXT and RETURN; RETURN 4 adds 4 and stops, returning only [2,4].
  3. Final Answer:

    FOR i IN 1..10 LOOP IF i % 2 = 0 THEN RETURN NEXT i; END IF; END LOOP; RETURN; -> Option B
  4. Quick Check:

    RETURN NEXT adds rows conditionally; RETURN ends function [OK]
Hint: Use RETURN NEXT inside loop with condition; end with RETURN [OK]
Common Mistakes:
  • Using RETURN inside loop stopping early
  • Returning all numbers instead of filtering
  • Mixing RETURN and RETURN NEXT causing early termination