Bird
0
0

Consider this query:

medium📝 query result Q5 of 15
SQL - Common Table Expressions (CTEs)
Consider this query:
WITH cte1 AS (SELECT 'A' AS letter UNION ALL SELECT 'B'), cte2 AS (SELECT letter FROM cte1 WHERE letter = 'B') SELECT * FROM cte2;

What is the output?
ASyntax error due to UNION ALL
BTwo rows with letters 'A' and 'B'
COne row with letter = 'B'
DNo rows returned
Step-by-Step Solution
Solution:
  1. Step 1: Analyze first CTE

    cte1 returns two rows: 'A' and 'B'.
  2. Step 2: Analyze second CTE filtering cte1

    cte2 selects only rows where letter = 'B', so one row with 'B'.
  3. Final Answer:

    One row with letter = 'B' -> Option C
  4. Quick Check:

    Filtering in second CTE = single row 'B' [OK]
Quick Trick: Filter rows in second CTE by condition on first CTE [OK]
Common Mistakes:
  • Expecting both rows in output
  • Thinking UNION ALL causes syntax error
  • Assuming no rows because of filter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes