0
0
SQLquery~20 mins

CTE referencing another CTE in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CTE Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{"num":1,"multiplied":1},{"num":2,"multiplied":2},{"num":3,"multiplied":3}]
B[{"num":1,"multiplied":10},{"num":2,"multiplied":20},{"num":3,"multiplied":30}]
CSyntaxError
D[]
Attempts:
2 left
💡 Hint
Look at how the second CTE uses the first CTE's output and multiplies the numbers by 10.
📝 Syntax
intermediate
2: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;
AWITH A AS (SELECT 1 AS val), B AS (SELECT val + 1 FROM A); SELECT * FROM B;
BWITH A AS (SELECT 1 AS val), B AS (SELECT val + 1 FROM A) SELECT * FROM B;
C;B MORF * TCELES )A MORF 1 + lav TCELES( SA B ,)lav SA 1 TCELES( SA A HTIW
DWITH 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.
optimization
advanced
2: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;
AUse a single CTE and repeat the filtering logic inside the second CTE instead of referencing the first.
BReference BaseCTE multiple times in AggCTE to ensure fresh data each time.
CReference BaseCTE once in AggCTE to reuse the filtered data and avoid recalculations.
DAvoid using CTEs and write the entire query as a nested subquery.
Attempts:
2 left
💡 Hint
Reusing filtered data avoids repeating expensive operations.
🔧 Debug
advanced
2: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;
AColumn 'y' does not exist error
BSyntax error near 'Second'
CNo error, returns one row with value 1
DTable 'First' does not exist error
Attempts:
2 left
💡 Hint
Check the column names used in the second CTE.
🧠 Conceptual
expert
2:00remaining
Understanding CTE evaluation order
Which statement best describes how CTEs referencing other CTEs are evaluated in SQL?
ACTEs are evaluated in the order they are defined, with later CTEs able to reference earlier ones.
BEach CTE is evaluated only when referenced, starting from the last defined CTE backwards.
CAll CTEs are evaluated simultaneously before the main query runs.
DCTEs are materialized and stored permanently in the database for reuse.
Attempts:
2 left
💡 Hint
Think about how SQL processes WITH clauses step-by-step.