0
0
PostgreSQLquery~20 mins

CTE vs subquery performance in PostgreSQL - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CTE vs Subquery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of CTE vs Subquery with Aggregation
Given the table sales with columns 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
AEmpty result for Query 1; [{"region": "South", "total": 1200}] for Query 2
B[{"region": "East", "total": 1100}, {"region": "South", "total": 1200}] for both queries
CBoth queries return empty result
D[{"region": "East", "total": 1100}] for Query 1; empty result for Query 2
Attempts:
2 left
💡 Hint
Both CTE and subquery here calculate the same aggregation and filter the same way.
🧠 Conceptual
intermediate
1: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?
ACTEs act as optimization fences and are materialized, which can hurt performance.
BCTEs are always inlined and optimized like subqueries.
CSubqueries are materialized by default, while CTEs are always optimized away.
DBoth CTEs and subqueries are always materialized and never optimized.
Attempts:
2 left
💡 Hint
Think about how PostgreSQL treats CTEs as separate query blocks before version 12.
📝 Syntax
advanced
1:00remaining
Identify the syntax error in CTE usage
Which of the following CTE usages will cause a syntax error in PostgreSQL?
AWITH cte AS (SELECT id FROM users) SELECT id FROM cte;
BWITH cte AS (SELECT * FROM users) SELECT * FROM cte;
CWITH cte AS SELECT * FROM users SELECT * FROM cte;
DWITH cte AS (SELECT * FROM users WHERE active = true) SELECT * FROM cte;
Attempts:
2 left
💡 Hint
Check the parentheses around the CTE query.
optimization
advanced
1: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?
ARepeat the subquery inline multiple times for better optimization.
BAvoid CTEs because they always cause performance degradation.
CUse temporary tables instead of CTEs or subqueries.
DUse a CTE to compute the subquery once and reuse the result.
Attempts:
2 left
💡 Hint
PostgreSQL 12+ can inline CTEs unless marked MATERIALIZED.
🔧 Debug
expert
2:30remaining
Why does this query with CTE run slower than the subquery version?
Consider these two queries on a large table 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?
AThe CTE forces materialization, preventing index usage and causing a full scan.
BThe subquery version has a syntax error and does not run.
CThe CTE query is missing a GROUP BY clause causing incorrect aggregation.
DThe subquery version disables parallelism causing slower execution.
Attempts:
2 left
💡 Hint
Think about how PostgreSQL treats CTEs as optimization fences before version 12 and how that affects index usage.