0
0
PostgreSQLquery~20 mins

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

Choose your learning style9 modes available
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.