Challenge - 5 Problems
Derived Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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);
Attempts:
2 left
💡 Hint
Remember the subquery groups sales by category and sums them, then the outer query orders by total_sales descending.
✗ Incorrect
The subquery sums sales per category: Electronics (200+300=500), Books (100+150=250), Clothing (50). The outer query orders by total_sales descending, so Electronics first, then Books, then Clothing.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in derived table usage
Which option contains a syntax error when using a derived table in the FROM clause?
Attempts:
2 left
💡 Hint
Derived tables must have an alias in the FROM clause.
✗ Incorrect
Option C is missing an alias for the derived table, which is required in SQL. Options A, C, and D correctly alias the subquery as dt.
❓ optimization
advanced2: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;
Attempts:
2 left
💡 Hint
Think about indexing and aggregation order for performance.
✗ Incorrect
Option A is best because aggregating separately then joining is efficient, and adding indexes on product_id speeds up joins. Option A aggregates after join, which can be slower due to larger intermediate data. Option A improves readability but not performance. Option A is correct but not optimized.
🔧 Debug
advanced2: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;Attempts:
2 left
💡 Hint
Check the columns selected in the derived tables referenced in the WHERE clause.
✗ Incorrect
The first derived table dt1 selects only 'category' and does not have 'avg_price'. The WHERE clause references 'dt1.avg_price', which does not exist, causing a 'column does not exist' error. Option A is incorrect because no aggregation is needed for dt1 here. Option A is wrong as dt2 has an alias. Option A is wrong because dt1.category is properly selected.
🧠 Conceptual
expert3:00remaining
Understanding scope and aliasing in nested derived tables
Consider this query:
Which statement is true about the scope and aliasing in 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?
Attempts:
2 left
💡 Hint
Think about how aliases define scope inside nested queries.
✗ Incorrect
Alias inner_dt is defined inside the outer derived table and is only visible there. The outer derived table alias outer_dt is necessary to reference the subquery in the outer SELECT. The WHERE clause correctly filters on inner_dt.total which exists. Nested derived tables are allowed in PostgreSQL.