0
0
PostgreSQLquery~20 mins

CTE materialization behavior in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CTE Mastery Badge
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 with aggregation
Consider the following PostgreSQL query using a CTE (Common Table Expression):

WITH sales_summary AS (SELECT product_id, SUM(quantity) AS total_qty FROM sales GROUP BY product_id) SELECT * FROM sales_summary WHERE total_qty > 100;

What will be the output if the sales table has the following data?

product_id | quantity
-----------+---------
1          | 50
1          | 60
2          | 30
3          | 120
3          | 10
PostgreSQL
WITH sales_summary AS (SELECT product_id, SUM(quantity) AS total_qty FROM sales GROUP BY product_id) SELECT * FROM sales_summary WHERE total_qty > 100;
A
product_id: 1, total_qty: 110
product_id: 2, total_qty: 30
product_id: 3, total_qty: 130
B
product_id: 1, total_qty: 110
product_id: 3, total_qty: 130
Cproduct_id: 3, total_qty: 130
D
product_id: 2, total_qty: 30
product_id: 3, total_qty: 130
Attempts:
2 left
💡 Hint
Sum the quantities per product and then filter those with total quantity greater than 100.
🧠 Conceptual
intermediate
1:30remaining
CTE Materialization Behavior in PostgreSQL
In PostgreSQL versions before 12, how does the database handle CTEs by default?

Choose the correct description of CTE materialization behavior.
ACTEs are inlined into the main query and optimized together with it.
BCTEs are executed multiple times, once per each row of the main query.
CCTEs are always materialized as temporary result sets before the main query runs.
DCTEs are ignored and treated as subqueries without optimization.
Attempts:
2 left
💡 Hint
Think about whether the CTE is executed once or multiple times and if its result is stored.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in a CTE query
Which of the following CTE queries will cause a syntax error in PostgreSQL?
AWITH cte AS SELECT id FROM users SELECT * FROM cte;
BWITH cte AS (SELECT id FROM users) SELECT * FROM cte;
CWITH cte AS (SELECT id FROM users) SELECT id FROM cte WHERE id > 10;
DWITH cte AS (SELECT id FROM users) SELECT COUNT(*) FROM cte;
Attempts:
2 left
💡 Hint
Check the syntax of the CTE definition, especially the parentheses.
optimization
advanced
2:00remaining
Effect of MATERIALIZED and NOT MATERIALIZED on CTE performance
Given a CTE used multiple times in a query, which option best describes the effect of adding the keyword NOT MATERIALIZED in PostgreSQL 12+?
AThe CTE is inlined and optimized with the main query, potentially executed multiple times.
BThe CTE is executed once and its result stored temporarily for reuse.
CThe CTE is ignored and replaced by a dummy empty result set.
DThe CTE is executed multiple times regardless of usage.
Attempts:
2 left
💡 Hint
Consider how NOT MATERIALIZED affects query planning and execution.
🔧 Debug
expert
2:30remaining
Diagnose unexpected query performance due to CTE materialization
A query uses a CTE that filters a large table and then joins it multiple times. The query runs slower than expected.

Which of the following is the most likely cause related to CTE materialization in PostgreSQL versions before 12?
AThe CTE is materialized once, so the filtered result is reused efficiently.
BThe CTE is ignored and the join uses the original large table multiple times.
CThe CTE is inlined and optimized with the main query, causing repeated filtering.
DThe CTE is materialized once, but the large intermediate result causes slow performance.
Attempts:
2 left
💡 Hint
Think about how materialization affects intermediate result size and reuse.