0
0
PostgreSQLquery~20 mins

WITH clause syntax in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WITH Clause Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a simple WITH clause query
What is the output of this query using a WITH clause to calculate doubled values?
PostgreSQL
WITH doubled AS (SELECT 1 AS num, 2 AS val UNION ALL SELECT 2, 4 UNION ALL SELECT 3, 6) SELECT num, val FROM doubled WHERE val > 3 ORDER BY num;
A
num | val
----+-----
  2 |   4
  3 |   6
B
num | val
----+-----
  1 |   2
  2 |   4
  3 |   6
C
num | val
----+-----
  1 |   2
D
num | val
----+-----
  3 |   6
Attempts:
2 left
💡 Hint
Look at the WHERE clause filtering values greater than 3.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in WITH clause usage
Which option contains a syntax error in the WITH clause usage?
PostgreSQL
WITH temp AS SELECT id, name FROM users SELECT * FROM temp;
AWITH temp AS SELECT id, name FROM users; SELECT * FROM temp;
BWITH temp AS (SELECT id, name FROM users) SELECT * FROM temp;
C;pmet MORF * TCELES )sresu MORF eman ,di TCELES( SA pmet HTIW
DWITH temp AS (SELECT id, name FROM users) SELECT * FROM temp
Attempts:
2 left
💡 Hint
Check if the WITH clause has parentheses around the subquery.
optimization
advanced
3:00remaining
Optimizing multiple WITH clauses
Given two WITH clauses that calculate sums separately, which option combines them efficiently into one WITH clause?
PostgreSQL
WITH sum_a AS (SELECT SUM(amount) AS total_a FROM sales WHERE category = 'A'), sum_b AS (SELECT SUM(amount) AS total_b FROM sales WHERE category = 'B') SELECT total_a, total_b FROM sum_a CROSS JOIN sum_b;
AWITH sum_a AS (SELECT SUM(amount) AS total_a FROM sales WHERE category = 'A'), sum_b AS (SELECT SUM(amount) AS total_b FROM sales WHERE category = 'B') SELECT total_a, total_b FROM sum_a JOIN sum_b ON sum_a.total_a = sum_b.total_b;
BWITH sums AS (SELECT SUM(amount) AS total_a FROM sales WHERE category = 'A' UNION ALL SELECT SUM(amount) AS total_b FROM sales WHERE category = 'B') SELECT * FROM sums;
CWITH sums AS (SELECT SUM(CASE WHEN category = 'A' THEN amount ELSE 0 END) AS total_a, SUM(CASE WHEN category = 'B' THEN amount ELSE 0 END) AS total_b FROM sales) SELECT total_a, total_b FROM sums;
DWITH sums AS (SELECT SUM(amount) AS total FROM sales) SELECT total FROM sums;
Attempts:
2 left
💡 Hint
Try to calculate both sums in a single scan of the sales table.
🔧 Debug
advanced
2:00remaining
Debugging recursive WITH clause
What error will this recursive WITH clause produce?
PostgreSQL
WITH RECURSIVE nums(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM nums WHERE n < 3) SELECT * FROM nums;
AEmpty result set
BSyntax error: missing parentheses
CRuntime error: infinite recursion
DReturns rows: 1, 2, 3
Attempts:
2 left
💡 Hint
Check the base case and recursion condition carefully.
🧠 Conceptual
expert
1:30remaining
Understanding scope of WITH clause aliases
Which statement about the scope of table aliases defined in a WITH clause is true?
AAliases defined in a WITH clause become permanent tables in the database.
BAliases defined in a WITH clause are visible only within that query and cannot be referenced outside it.
CAliases defined in a WITH clause can be referenced in any subsequent query in the same session.
DAliases defined in a WITH clause are global and accessible by all users until the database restarts.
Attempts:
2 left
💡 Hint
Think about temporary nature of WITH clause results.