0
0
PostgreSQLquery~20 mins

Multiple CTEs in one query 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 multiple CTEs with aggregation
Consider the following SQL query using two CTEs. What will be the output rows?
PostgreSQL
WITH sales AS (
  SELECT 'Alice' AS salesperson, 100 AS amount
  UNION ALL
  SELECT 'Bob', 150
  UNION ALL
  SELECT 'Alice', 200
),
summary AS (
  SELECT salesperson, SUM(amount) AS total_sales
  FROM sales
  GROUP BY salesperson
)
SELECT salesperson, total_sales FROM summary ORDER BY salesperson;
A[{"salesperson": "Alice", "total_sales": 100}, {"salesperson": "Bob", "total_sales": 150}]
B[{"salesperson": "Alice", "total_sales": 200}, {"salesperson": "Bob", "total_sales": 150}]
C[{"salesperson": "Alice", "total_sales": 300}, {"salesperson": "Bob", "total_sales": 150}]
D[{"salesperson": "Alice", "total_sales": 300}, {"salesperson": "Bob", "total_sales": 250}]
Attempts:
2 left
💡 Hint
Sum the amounts for each salesperson from the sales CTE.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in multiple CTEs
Which option contains a syntax error in defining multiple CTEs?
PostgreSQL
WITH cte1 AS (SELECT 1 AS num),
cte2 AS (SELECT 2 AS num)
SELECT * FROM cte1 UNION ALL SELECT * FROM cte2;
AWITH cte1 AS (SELECT 1 AS num), cte2 AS (SELECT 2 AS num) SELECT * FROM cte1, cte2;
BWITH cte1 AS (SELECT 1 AS num), cte2 AS (SELECT 2 AS num) SELECT * FROM cte1 UNION ALL SELECT * FROM cte2;
CWITH cte1 AS (SELECT 1 AS num), cte2 AS (SELECT 2 AS num); SELECT * FROM cte1 UNION ALL SELECT * FROM cte2;
DWITH cte1 AS (SELECT 1 AS num) cte2 AS (SELECT 2 AS num) SELECT * FROM cte1 UNION ALL SELECT * FROM cte2;
Attempts:
2 left
💡 Hint
Check the commas and placement between CTE definitions.
optimization
advanced
2:00remaining
Optimizing multiple CTEs for performance
Given two CTEs where the second depends on the first, which option optimizes the query to avoid repeated scans?
PostgreSQL
WITH cte1 AS (
  SELECT * FROM orders WHERE order_date >= '2024-01-01'
),
cte2 AS (
  SELECT customer_id, COUNT(*) AS order_count FROM cte1 GROUP BY customer_id
)
SELECT * FROM cte2 WHERE order_count > 5;
AAdd an index on orders(order_date) and keep the CTEs as is.
BUse MATERIALIZED keyword explicitly for cte1 to force materialization.
CReplace cte1 with a subquery inside cte2 to avoid materializing cte1 separately.
DRewrite cte2 as a JOIN on orders instead of using cte1.
Attempts:
2 left
💡 Hint
Indexes help speed up filtering on large tables.
🔧 Debug
advanced
2:00remaining
Debugging unexpected output from multiple CTEs
This query returns no rows, but you expect some. What is the cause?
PostgreSQL
WITH cte1 AS (
  SELECT * FROM products WHERE price > 100
),
cte2 AS (
  SELECT * FROM cte1 WHERE price < 50
)
SELECT * FROM cte2;
AThe query needs DISTINCT in cte2 to return rows.
BThe second CTE filters price < 50 but cte1 only has price > 100, so no rows match.
CThe CTEs are not chained properly; cte2 should join cte1 instead of filtering.
DMissing semicolon after cte1 causes cte2 to fail silently.
Attempts:
2 left
💡 Hint
Check the filter conditions in both CTEs carefully.
🧠 Conceptual
expert
2:00remaining
Understanding execution order of multiple CTEs
In a query with multiple CTEs where cte3 depends on cte2 and cte2 depends on cte1, which statement is true about execution order?
ACTEs are executed lazily only when referenced, so cte3 triggers cte2, which triggers cte1.
BAll CTEs are executed simultaneously and results merged before the main query.
CCTEs are executed in the order they are defined, from cte1 to cte3, before the main query runs.
DCTEs are materialized in temporary tables in reverse order, starting from cte3.
Attempts:
2 left
💡 Hint
Think about how PostgreSQL handles CTEs and dependencies.