0
0
PostgreSQLquery~20 mins

Subqueries in FROM (derived tables) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Derived Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a simple derived table query
What is the output of this query?

SELECT dt.category, dt.total_sales FROM (SELECT category, SUM(sales) AS total_sales FROM sales_data GROUP BY category) AS dt ORDER BY dt.total_sales DESC;
PostgreSQL
CREATE TABLE sales_data (category TEXT, sales INT);
INSERT INTO sales_data VALUES
('Books', 100), ('Books', 150), ('Electronics', 200), ('Electronics', 300), ('Clothing', 50);
A
category | total_sales
---------|------------
Books | 250
Electronics | 500
Clothing | 50
B
category | total_sales
---------|------------
Electronics | 500
Books | 250
Clothing | 50
C
category | total_sales
---------|------------
Clothing | 50
Books | 250
Electronics | 500
D
category | total_sales
---------|------------
Electronics | 300
Books | 150
Clothing | 50
Attempts:
2 left
💡 Hint
Remember the subquery groups sales by category and sums them, then the outer query orders by total_sales descending.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in derived table usage
Which option contains a syntax error when using a derived table in the FROM clause?
ASELECT dt.category, dt.total FROM (SELECT category, COUNT(*) AS total FROM products GROUP BY category) dt;
BSELECT dt.category, dt.total FROM (SELECT category, COUNT(*) AS total FROM products GROUP BY category) dt WHERE dt.total > 5;
CSELECT dt.category, dt.total FROM (SELECT category, COUNT(*) AS total FROM products GROUP BY category);
DSELECT dt.category, dt.total FROM (SELECT category, COUNT(*) AS total FROM products GROUP BY category) AS dt;
Attempts:
2 left
💡 Hint
Derived tables must have an alias in the FROM clause.
optimization
advanced
2:00remaining
Optimizing a query with multiple derived tables
Given two derived tables that calculate sales and returns per product, which query is more efficient and why?
PostgreSQL
SELECT s.product_id, s.total_sales, r.total_returns
FROM (SELECT product_id, SUM(amount) AS total_sales FROM sales GROUP BY product_id) s
JOIN (SELECT product_id, SUM(amount) AS total_returns FROM returns GROUP BY product_id) r ON s.product_id = r.product_id;
AAggregate sales and returns separately, then join on product_id, but add indexes on product_id columns.
BReplace derived tables with CTEs (WITH clauses) to improve readability but no performance gain.
CJoin sales and returns tables first, then aggregate in a single derived table.
DUse the query as is; derived tables are necessary for aggregation before join.
Attempts:
2 left
💡 Hint
Think about indexing and aggregation order for performance.
🔧 Debug
advanced
2:00remaining
Debugging a query with nested derived tables
This query returns an error. What is the cause?

SELECT dt1.category, dt2.avg_price FROM (SELECT category FROM products) dt1 JOIN (SELECT category, AVG(price) AS avg_price FROM products GROUP BY category) dt2 ON dt1.category = dt2.category WHERE dt1.avg_price > 100;
AThe WHERE clause references dt1.avg_price which is not available at that scope.
BThe alias dt2 is missing in the second derived table.
CThe join condition is invalid because dt1.category is not selected properly.
DThe first derived table dt1 lacks aggregation and causes ambiguity.
Attempts:
2 left
💡 Hint
Check the columns selected in the derived tables referenced in the WHERE clause.
🧠 Conceptual
expert
3:00remaining
Understanding scope and aliasing in nested derived tables
Consider this query:

SELECT outer_dt.category, outer_dt.total FROM (SELECT inner_dt.category, inner_dt.total FROM (SELECT category, COUNT(*) AS total FROM orders GROUP BY category) AS inner_dt WHERE inner_dt.total > 10) AS outer_dt;

Which statement is true about the scope and aliasing in this query?
AThe query will fail because nested derived tables cannot be used in PostgreSQL.
BThe alias outer_dt is unnecessary and can be removed without error.
CThe WHERE clause filters on a column not available in inner_dt, causing an error.
DThe alias inner_dt is only visible inside the outer derived table and cannot be referenced outside it.
Attempts:
2 left
💡 Hint
Think about how aliases define scope inside nested queries.