Bird
0
0

What will be the result of this query?

medium📝 query result Q5 of 15
PostgreSQL - Advanced Features
What will be the result of this query?
WITH RECURSIVE nums AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM nums WHERE n < 3) SELECT * FROM nums;
AAn infinite loop error
BRows with n values: 1, 2, 3
CNo rows returned
DRows with n values: 1 only
Step-by-Step Solution
Solution:
  1. Step 1: Understand recursive CTE behavior

    The CTE starts with n=1, then adds n+1 while n < 3.
  2. Step 2: Trace recursion steps

    n=1, then 2, then 3; recursion stops after n=3.
  3. Step 3: Final output

    Query returns rows with n = 1, 2, and 3.
  4. Final Answer:

    Rows with n values: 1, 2, 3 -> Option B
  5. Quick Check:

    Recursive CTE output = Rows with n values: 1, 2, 3 [OK]
Quick Trick: Recursive CTEs stop when WHERE condition fails [OK]
Common Mistakes:
  • Assuming infinite recursion without stop condition
  • Expecting no rows if recursion misunderstood
  • Confusing UNION ALL with UNION

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes