Challenge - 5 Problems
CTE vs Subquery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of CTE vs Subquery with Aggregation
Given the table sales with columns
Query 1 uses a CTE:
Query 2 uses a subquery:
Assuming the sales table has:
region and amount, what is the output of these two queries?Query 1 uses a CTE:
WITH regional_totals AS ( SELECT region, SUM(amount) AS total FROM sales GROUP BY region ) SELECT * FROM regional_totals WHERE total > 1000;
Query 2 uses a subquery:
SELECT region, total FROM ( SELECT region, SUM(amount) AS total FROM sales GROUP BY region ) AS sub WHERE total > 1000;
Assuming the sales table has:
region | amount -------+-------- East | 500 East | 600 West | 400 West | 300 South | 1200
Attempts:
2 left
💡 Hint
Both CTE and subquery here calculate the same aggregation and filter the same way.
✗ Incorrect
Both queries compute the sum of amounts grouped by region, then filter regions with totals over 1000. The results are the same: East (1100) and South (1200).
🧠 Conceptual
intermediate1:30remaining
Performance difference between CTE and subquery in PostgreSQL before version 12
Which statement best describes the performance behavior of CTEs compared to subqueries in PostgreSQL before version 12?
Attempts:
2 left
💡 Hint
Think about how PostgreSQL treats CTEs as separate query blocks before version 12.
✗ Incorrect
Before PostgreSQL 12, CTEs are optimization fences and are materialized, meaning the database computes and stores the CTE result before the outer query runs. This can hurt performance compared to subqueries, which can be inlined and optimized.
📝 Syntax
advanced1:00remaining
Identify the syntax error in CTE usage
Which of the following CTE usages will cause a syntax error in PostgreSQL?
Attempts:
2 left
💡 Hint
Check the parentheses around the CTE query.
✗ Incorrect
Option C is missing parentheses around the CTE query, which is required syntax in PostgreSQL.
❓ optimization
advanced1:30remaining
Optimizing repeated subqueries vs CTEs
You have a complex subquery used multiple times in a large query. Which approach is generally better for performance in PostgreSQL 13 and later?
Attempts:
2 left
💡 Hint
PostgreSQL 12+ can inline CTEs unless marked MATERIALIZED.
✗ Incorrect
In PostgreSQL 12 and later, CTEs can be inlined or materialized. Using a CTE to compute a complex subquery once and reuse it can improve performance by avoiding repeated computation.
🔧 Debug
expert2:30remaining
Why does this query with CTE run slower than the subquery version?
Consider these two queries on a large table
Query 1 (CTE):
Query 2 (subquery):
Query 1 runs significantly slower than Query 2. What is the most likely reason?
orders with an index on customer_id:Query 1 (CTE):
WITH recent_orders AS ( SELECT * FROM orders WHERE order_date > '2024-01-01' ) SELECT customer_id, COUNT(*) FROM recent_orders GROUP BY customer_id;
Query 2 (subquery):
SELECT customer_id, COUNT(*) FROM ( SELECT * FROM orders WHERE order_date > '2024-01-01' ) AS recent_orders GROUP BY customer_id;
Query 1 runs significantly slower than Query 2. What is the most likely reason?
Attempts:
2 left
💡 Hint
Think about how PostgreSQL treats CTEs as optimization fences before version 12 and how that affects index usage.
✗ Incorrect
The CTE is materialized as a temporary result, which disables index scans and parallelism on the base table. The subquery can be optimized and use indexes, making it faster.