Challenge - 5 Problems
CTE Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple CTE query with aggregation
Consider the following PostgreSQL query using a CTE (Common Table Expression):
What will be the output if the sales table has the following data?
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;
Attempts:
2 left
💡 Hint
Sum the quantities per product and then filter those with total quantity greater than 100.
✗ Incorrect
The CTE calculates total quantity per product. Product 1 has 50+60=110, product 2 has 30, product 3 has 120+10=130. Filtering total_qty > 100 returns products 1 and 3.
🧠 Conceptual
intermediate1: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.
Choose the correct description of CTE materialization behavior.
Attempts:
2 left
💡 Hint
Think about whether the CTE is executed once or multiple times and if its result is stored.
✗ Incorrect
Before PostgreSQL 12, CTEs are always materialized, meaning their result is computed and stored temporarily before the main query uses it. This can affect performance.
📝 Syntax
advanced1:30remaining
Identify the syntax error in a CTE query
Which of the following CTE queries will cause a syntax error in PostgreSQL?
Attempts:
2 left
💡 Hint
Check the syntax of the CTE definition, especially the parentheses.
✗ Incorrect
Option A is missing parentheses around the CTE's SELECT statement, which is required syntax in PostgreSQL.
❓ optimization
advanced2: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+?
Attempts:
2 left
💡 Hint
Consider how NOT MATERIALIZED affects query planning and execution.
✗ Incorrect
NOT MATERIALIZED tells PostgreSQL to inline the CTE into the main query, allowing better optimization but possibly multiple executions.
🔧 Debug
expert2: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?
Which of the following is the most likely cause related to CTE materialization in PostgreSQL versions before 12?
Attempts:
2 left
💡 Hint
Think about how materialization affects intermediate result size and reuse.
✗ Incorrect
In PostgreSQL before 12, CTEs are materialized once. If the filtered result is still large, storing it can slow down the query despite reuse.