Bird
0
0

What will be the output of this query?

medium📝 query result Q5 of 15
PostgreSQL - Common Table Expressions
What will be the output of this query?
WITH numbers AS (SELECT generate_series(1,3) AS num) SELECT num, num * num AS square FROM numbers;
AEmpty result set
BAn error because generate_series cannot be used in CTE
COnly one row with num=3 and square=9
DRows with numbers 1 to 3 and their squares: (1,1), (2,4), (3,9)
Step-by-Step Solution
Solution:
  1. Step 1: Understand generate_series function in CTE

    generate_series(1,3) produces rows with values 1, 2, and 3.
  2. Step 2: Calculate squares in main query

    The main query selects each number and its square, so output is (1,1), (2,4), (3,9).
  3. Final Answer:

    Rows with numbers 1 to 3 and their squares: (1,1), (2,4), (3,9) -> Option D
  4. Quick Check:

    generate_series in CTE works = D [OK]
Quick Trick: generate_series can be used inside CTEs to create sequences [OK]
Common Mistakes:
  • Assuming generate_series is invalid in CTE
  • Expecting only one row output
  • Thinking result is empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes