Bird
0
0

What will this query output?

medium📝 query result Q5 of 15
SQL - Common Table Expressions (CTEs)
What will this query output?
WITH RECURSIVE seq AS (SELECT 5 AS val UNION ALL SELECT val - 1 FROM seq WHERE val > 2) SELECT * FROM seq ORDER BY val;
A5, 4, 3, 2
B2, 3, 4, 5
C5, 4, 3
D3, 4, 5
Step-by-Step Solution
Solution:
  1. Step 1: Understand base and recursion

    Base returns 5, recursion subtracts 1 while val > 2, so values 5,4,3,2 are generated.
  2. Step 2: Note ORDER BY clause

    Ordering by val ascending gives 2,3,4,5 in output.
  3. Final Answer:

    2, 3, 4, 5 -> Option B
  4. Quick Check:

    Order by val ascending = 2,3,4,5 [OK]
Quick Trick: ORDER BY changes output order, recursion generates values [OK]
Common Mistakes:
  • Ignoring ORDER BY and expecting descending output
  • Stopping recursion too early
  • Misreading recursion condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes