Bird
0
0

What will be the result of this query?

medium📝 query result Q4 of 15
SQL - Common Table Expressions (CTEs)
What will be the result of this query?
WITH RECURSIVE sequence AS (SELECT 2 AS val UNION ALL SELECT val * 2 FROM sequence WHERE val < 8) SELECT * FROM sequence ORDER BY val;
A2, 4, 6, 8
B2, 4, 8
C2, 4, 8, 16
D2, 4
Step-by-Step Solution
Solution:
  1. Step 1: Analyze anchor member

    Starts with val = 2.
  2. Step 2: Analyze recursive member

    Each next val is val * 2, but only if val < 8.
  3. Step 3: Generate sequence

    2 (anchor), then 4 (2*2), then 8 (4*2). Next would be 16 (8*2), but 8 < 8 is false, so recursion stops.
  4. Final Answer:

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

    Multiply by 2 until val < 8 [OK]
Quick Trick: Multiply val by 2 until less than 8 [OK]
Common Mistakes:
  • Including 16 despite stopping condition
  • Assuming linear increments instead of multiplication
  • Misreading stopping condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes