Bird
0
0

Given the query:

medium📝 query result Q4 of 15
PostgreSQL - Common Table Expressions
Given the query:
WITH cte AS MATERIALIZED (SELECT generate_series(1,3) AS num) SELECT num FROM cte, generate_series(1,2) AS gs(n);

What is the output?
ARows: (1,1), (1,2), (2,1), (3,1), (3,2)
BRows: (1,1), (2,1), (3,1)
CRows: (1,1), (1,2), (2,1), (2,2), (3,1), (3,2)
DRows: (1,1), (2,2), (3,1), (3,2)
Step-by-Step Solution
Solution:
  1. Step 1: Understand CTE materialization and cross join

    The CTE generates numbers 1 to 3 and is materialized once. The main query cross joins with generate_series(1,2).
  2. Step 2: Calculate output rows

    Each num (1,2,3) pairs with gs.n (1,2), producing 6 rows: (1,1), (1,2), (2,1), (2,2), (3,1), (3,2).
  3. Final Answer:

    Rows: (1,1), (1,2), (2,1), (2,2), (3,1), (3,2) -> Option C
  4. Quick Check:

    Materialized CTE cross join = all combinations [OK]
Quick Trick: Materialized CTE runs once; cross join multiplies rows [OK]
Common Mistakes:
  • Assuming CTE runs multiple times
  • Ignoring cross join effect
  • Counting fewer rows than actual

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes