Challenge - 5 Problems
CTE Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of nested CTE query
Given the following SQL query with two CTEs where the second references the first, what is the output?
SQL
WITH FirstCTE AS ( SELECT 1 AS num UNION ALL SELECT 2 UNION ALL SELECT 3 ), SecondCTE AS ( SELECT num, num * 10 AS multiplied FROM FirstCTE ) SELECT * FROM SecondCTE ORDER BY num;
Attempts:
2 left
💡 Hint
Look at how the second CTE uses the first CTE's output and multiplies the numbers by 10.
✗ Incorrect
The first CTE produces numbers 1, 2, and 3. The second CTE selects these numbers and multiplies each by 10. The final SELECT returns these results ordered by num.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in CTE referencing
Which option contains a syntax error when defining a CTE that references another CTE?
SQL
WITH A AS (SELECT 1 AS val), B AS (SELECT val + 1 FROM A) SELECT * FROM B;
Attempts:
2 left
💡 Hint
Check the commas separating CTE definitions.
✗ Incorrect
Option D misses the comma between the two CTE definitions, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing multiple CTE references
Consider two CTEs where the second references the first multiple times. Which option best optimizes the query to avoid repeated computation?
SQL
WITH BaseCTE AS ( SELECT id, value FROM data_table WHERE value > 10 ), AggCTE AS ( SELECT id, COUNT(*) AS cnt FROM BaseCTE GROUP BY id ) SELECT * FROM AggCTE;
Attempts:
2 left
💡 Hint
Reusing filtered data avoids repeating expensive operations.
✗ Incorrect
Referencing the first CTE once in the second CTE allows reuse of the filtered data, improving performance by avoiding repeated filtering.
🔧 Debug
advanced2:00remaining
Debugging incorrect CTE reference
Given this SQL snippet, what error will it raise?
WITH First AS (SELECT 1 AS x),
Second AS (SELECT y FROM First)
SELECT * FROM Second;
Attempts:
2 left
💡 Hint
Check the column names used in the second CTE.
✗ Incorrect
The first CTE defines column 'x', but the second CTE tries to select 'y' which does not exist, causing a column not found error.
🧠 Conceptual
expert2:00remaining
Understanding CTE evaluation order
Which statement best describes how CTEs referencing other CTEs are evaluated in SQL?
Attempts:
2 left
💡 Hint
Think about how SQL processes WITH clauses step-by-step.
✗ Incorrect
CTEs are evaluated in the order they are defined. Later CTEs can reference earlier ones, but not vice versa.