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 products GROUP BY category) AS dt ORDER BY dt.total_sales DESC;
MySQL
CREATE TABLE products (id INT, category VARCHAR(20), sales INT); INSERT INTO products VALUES (1, 'Books', 100), (2, 'Books', 150), (3, 'Electronics', 200), (4, 'Clothing', 50);
Attempts:
2 left
💡 Hint
Think about how the subquery groups sales by category and sums them, then the outer query orders by total_sales descending.
✗ Incorrect
The subquery groups products by category and sums their sales. The outer query selects these results and orders them by total sales from highest to lowest. So Books with 250 comes first, then Electronics with 200, then Clothing with 50.
📝 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?
MySQL
SELECT dt.category, dt.total FROM (SELECT category, SUM(amount) AS total FROM sales GROUP BY category) dt;
Attempts:
2 left
💡 Hint
Remember that every derived table must have an alias.
✗ Incorrect
Option A is missing the alias for the derived table, which is required in SQL syntax. Options A and B correctly alias the derived table, and D adds a WHERE clause but still aliases correctly.
❓ optimization
advanced2:00remaining
Optimizing a query with multiple derived tables
Given two derived tables joined on a common key, which option is the most efficient way to write the query to avoid unnecessary computation?
MySQL
SELECT a.category, a.total_sales, b.total_returns FROM (SELECT category, SUM(sales) AS total_sales FROM products GROUP BY category) AS a JOIN (SELECT category, SUM(returns) AS total_returns FROM products GROUP BY category) AS b ON a.category = b.category;
Attempts:
2 left
💡 Hint
Think about how many times the table is scanned in each approach.
✗ Incorrect
Option B scans the products table once and calculates both sums together, which is more efficient. Option B scans the table twice. Option B shifts load to application and may increase latency. Option B adds overhead of managing temp tables.
🔧 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, COUNT(*) AS cnt FROM products GROUP BY category) dt1 JOIN (SELECT category, AVG(price) AS avg_price FROM products GROUP BY category) dt2 ON dt1.category = dt2.category WHERE dt1.cnt > 10;
Attempts:
2 left
💡 Hint
Check if referencing derived table columns in WHERE is allowed after the JOIN.
✗ Incorrect
The query is valid. The WHERE clause filters after the JOIN and can reference columns from derived tables. The JOIN condition is valid. No alias is missing in WHERE.
🧠 Conceptual
expert3:00remaining
Understanding scope and visibility of derived tables
Consider this query:
What is the role of the derived table dt2 in the subquery?
SELECT category, total_sales FROM (SELECT category, SUM(sales) AS total_sales FROM products GROUP BY category) AS dt WHERE total_sales > (SELECT AVG(total_sales) FROM (SELECT category, SUM(sales) AS total_sales FROM products GROUP BY category) AS dt2);
What is the role of the derived table dt2 in the subquery?
Attempts:
2 left
💡 Hint
Think about how the subquery calculates the average and how it relates to the outer query.
✗ Incorrect
dt2 is a derived table inside the subquery that calculates total sales per category. The subquery then computes the average of these totals. The outer query compares each category's total_sales to this average to filter results.