0
0
MySQLquery~20 mins

Common Table Expressions (WITH) in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CTE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
ARows with product_id and total_qty where total_qty is greater than 100
BAll rows from sales table without aggregation
CSyntax error due to missing alias
DEmpty result set because no product has quantity over 100
Attempts:
2 left
💡 Hint
Think about what the CTE does before the main SELECT.
📝 Syntax
intermediate
2: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;
AIncorrect alias name for CTE
BMissing comma between CTE and main query
CMissing parentheses around the CTE query
DNo error, query is valid
Attempts:
2 left
💡 Hint
Check the syntax for defining a CTE in MySQL.
optimization
advanced
3:00remaining
Optimizing repeated subqueries with CTE
Given this query:

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;
AWITH OrderCounts AS (SELECT user_id, COUNT(*) AS cnt FROM orders GROUP BY user_id) SELECT u.id, oc.cnt FROM users u JOIN OrderCounts oc ON u.id = oc.user_id WHERE oc.cnt > 5;
BWITH OrderCounts AS (SELECT user_id, COUNT(*) AS cnt FROM orders) SELECT u.id, oc.cnt FROM users u JOIN OrderCounts oc ON u.id = oc.user_id WHERE oc.cnt > 5;
CWITH OrderCounts AS (SELECT user_id, COUNT(*) AS cnt FROM orders GROUP BY user_id) SELECT u.id, oc.cnt FROM users u LEFT JOIN OrderCounts oc ON u.id = oc.user_id WHERE oc.cnt > 5;
DWITH OrderCounts AS (SELECT user_id, COUNT(*) AS cnt FROM orders GROUP BY user_id) SELECT u.id FROM users u WHERE u.id IN (SELECT user_id FROM OrderCounts WHERE cnt > 5);
Attempts:
2 left
💡 Hint
Avoid repeating the same subquery multiple times by calculating once in a CTE.
🔧 Debug
advanced
3:00remaining
Debugging recursive CTE for factorial calculation
This recursive CTE is intended to calculate factorial of 5:

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;
A24
B120
CSyntax error due to missing RECURSIVE keyword
DEmpty result set
Attempts:
2 left
💡 Hint
Trace the recursive steps from n=1 to n=5.
🧠 Conceptual
expert
2:00remaining
Understanding CTE scope and visibility
Consider these two queries:

1. WITH cte AS (SELECT 1 AS val) SELECT * FROM cte;
2. SELECT * FROM cte;

Why does the second query fail?
AThe second query is missing a FROM clause
BCTEs require explicit global declaration to be reused
CCTEs are only supported in SELECT statements with JOINs
DCTEs exist only within the query they are defined and are not visible outside
Attempts:
2 left
💡 Hint
Think about the lifetime of a CTE in SQL execution.