Challenge - 5 Problems
CTE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple CTE query
What is the output of this query?
WITH SalesCTE AS (SELECT product_id, SUM(quantity) AS total_qty FROM sales GROUP BY product_id) SELECT * FROM SalesCTE WHERE total_qty > 100;MySQL
WITH SalesCTE AS (SELECT product_id, SUM(quantity) AS total_qty FROM sales GROUP BY product_id) SELECT * FROM SalesCTE WHERE total_qty > 100;
Attempts:
2 left
💡 Hint
Think about what the CTE does before the main SELECT.
✗ Incorrect
The CTE calculates total quantity per product. The main query filters products with total quantity over 100, so it returns those rows.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this CTE
Which option correctly identifies the syntax error in this query?
WITH cte AS SELECT id, name FROM users SELECT * FROM cte;MySQL
WITH cte AS SELECT id, name FROM users SELECT * FROM cte;
Attempts:
2 left
💡 Hint
Check the syntax for defining a CTE in MySQL.
✗ Incorrect
In MySQL, the CTE query must be enclosed in parentheses after the AS keyword.
❓ optimization
advanced3:00remaining
Optimizing repeated subqueries with CTE
Given this query:
Which rewritten query using a CTE is more efficient?
SELECT u.id, (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count FROM users u WHERE (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) > 5;Which rewritten query using a CTE is more efficient?
MySQL
SELECT u.id, (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count FROM users u WHERE (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) > 5;
Attempts:
2 left
💡 Hint
Avoid repeating the same subquery multiple times by calculating once in a CTE.
✗ Incorrect
Option A calculates order counts once in the CTE and joins to filter users efficiently. Option A misses GROUP BY causing error. Option A uses LEFT JOIN but filters on oc.cnt > 5 which excludes NULLs, similar to INNER JOIN but less clear. Option A returns only user ids, missing order_count column.
🔧 Debug
advanced3:00remaining
Debugging recursive CTE for factorial calculation
This recursive CTE is intended to calculate factorial of 5:
What is the output?
WITH RECURSIVE fact(n, f) AS (SELECT 1, 1 UNION ALL SELECT n+1, f*(n+1) FROM fact WHERE n < 5) SELECT f FROM fact WHERE n = 5;What is the output?
MySQL
WITH RECURSIVE fact(n, f) AS (SELECT 1, 1 UNION ALL SELECT n+1, f*(n+1) FROM fact WHERE n < 5) SELECT f FROM fact WHERE n = 5;
Attempts:
2 left
💡 Hint
Trace the recursive steps from n=1 to n=5.
✗ Incorrect
The CTE starts at n=1 with factorial 1, then recursively multiplies up to n=5, resulting in 120.
🧠 Conceptual
expert2:00remaining
Understanding CTE scope and visibility
Consider these two queries:
1.
2.
Why does the second query fail?
1.
WITH cte AS (SELECT 1 AS val) SELECT * FROM cte;2.
SELECT * FROM cte;Why does the second query fail?
Attempts:
2 left
💡 Hint
Think about the lifetime of a CTE in SQL execution.
✗ Incorrect
CTEs are temporary named result sets valid only for the single query they are defined in. They cannot be referenced in separate queries.