We want to understand when to use CTEs or subqueries and how they affect speed in PostgreSQL.
CTE vs subquery performance in PostgreSQL
WITH cte_name AS ( SELECT ... ) SELECT ... FROM cte_name; -- vs -- SELECT ... FROM ( SELECT ... ) AS subquery;
A CTE (Common Table Expression) is defined with WITH and can be referenced like a temporary table.
A subquery is a SELECT inside another query, used directly in FROM or WHERE clauses.
WITH recent_orders AS ( SELECT * FROM orders WHERE order_date > '2024-01-01' ) SELECT * FROM recent_orders;
SELECT * FROM ( SELECT * FROM orders WHERE order_date > '2024-01-01' ) AS recent_orders;
This example shows the same filtering using a CTE and a subquery. Both return the same result.
CREATE TEMP TABLE orders (id INT, order_date DATE); INSERT INTO orders VALUES (1, '2024-02-01'), (2, '2023-12-31'); -- Using CTE WITH recent_orders AS ( SELECT * FROM orders WHERE order_date > '2024-01-01' ) SELECT id FROM recent_orders ORDER BY id; -- Using Subquery SELECT id FROM ( SELECT * FROM orders WHERE order_date > '2024-01-01' ) AS recent_orders ORDER BY id;
In PostgreSQL, CTEs act like optimization fences before version 12, meaning they always run separately and can be slower.
From PostgreSQL 12+, CTEs can be inlined like subqueries, improving performance.
Subqueries are often faster because the planner can optimize them better.
CTEs help organize queries but may affect speed depending on PostgreSQL version.
Subqueries can be faster because they allow more optimization.
Test both in your situation to see which is faster.
Practice
Solution
Step 1: Understand CTE behavior in PostgreSQL before v12
CTEs are treated as optimization fences, meaning the planner executes them separately and does not optimize across them.Step 2: Compare with subqueries
Subqueries can be inlined and optimized with the rest of the query, often leading to better performance.Final Answer:
CTEs act as optimization fences, potentially slowing queries. -> Option CQuick Check:
CTEs before v12 block optimization = A [OK]
- Thinking subqueries are always slower
- Assuming CTEs and subqueries perform the same
- Believing subqueries can't be inside SELECT
Solution
Step 1: Recall CTE syntax
CTEs start with WITH, followed by the CTE name, AS, and a query in parentheses.Step 2: Validate each option
WITH cte_name AS (SELECT * FROM table_name) SELECT * FROM cte_name; matches the correct syntax. Options A, B, and C have incorrect order or keywords.Final Answer:
WITH cte_name AS (SELECT * FROM table_name) SELECT * FROM cte_name; -> Option AQuick Check:
CTE syntax starts with WITH name AS (query) [OK]
- Placing WITH after SELECT
- Using CREATE keyword for CTE
- Misplacing parentheses
-- 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;Solution
Step 1: Understand PostgreSQL 11 optimization
In PostgreSQL 11, CTEs act as optimization fences, so Query 1 executes the CTE separately.Step 2: Compare with subquery behavior
Subqueries can be inlined and optimized with the main query, so Query 2 can be faster.Final Answer:
Query 2 runs faster because subqueries allow better optimization in PostgreSQL 11. -> Option BQuick Check:
PostgreSQL 11: subqueries optimize better than CTEs [OK]
- Assuming CTEs always optimize well
- Thinking both queries have same speed
- Believing Query 1 has syntax error
WITH recent_orders AS SELECT * FROM orders WHERE order_date > '2024-01-01'
SELECT * FROM recent_orders;Solution
Step 1: Check CTE syntax
CTE query must be enclosed in parentheses after AS keyword.Step 2: Identify the missing parentheses
The query lacks parentheses around SELECT * FROM orders WHERE ...Final Answer:
Missing parentheses around the CTE query. -> Option AQuick Check:
CTE query must be in parentheses [OK]
- Omitting parentheses around CTE query
- Thinking semicolon is required after CTE
- Believing CTE names can't be simple
Solution
Step 1: Understand PostgreSQL 13 CTE behavior
Starting with PostgreSQL 12, CTEs can be inlined and optimized like subqueries.Step 2: Evaluate optimization strategies
Replacing CTEs with subqueries is not necessary; keeping CTEs maintains readability and performance.Final Answer:
Keep CTEs because PostgreSQL 13 can inline them, improving speed. -> Option DQuick Check:
PostgreSQL 12+ inlines CTEs for better optimization [OK]
- Thinking subqueries are deprecated
- Replacing CTEs blindly without testing
- Assuming temporary tables always improve speed
Ready to prove it?
Pass the test and this pattern moves unknown
