0
0
MySQLquery~20 mins

Subqueries in FROM clause (derived tables) in MySQL - 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 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);
A[{"category": "Clothing", "total_sales": 50}, {"category": "Books", "total_sales": 250}, {"category": "Electronics", "total_sales": 200}]
B[{"category": "Books", "total_sales": 100}, {"category": "Books", "total_sales": 150}, {"category": "Electronics", "total_sales": 200}, {"category": "Clothing", "total_sales": 50}]
C[{"category": "Books", "total_sales": 250}, {"category": "Electronics", "total_sales": 200}, {"category": "Clothing", "total_sales": 50}]
D[{"category": "Electronics", "total_sales": 200}, {"category": "Books", "total_sales": 250}, {"category": "Clothing", "total_sales": 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.
📝 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?
MySQL
SELECT dt.category, dt.total FROM (SELECT category, SUM(amount) AS total FROM sales GROUP BY category) dt;
ASELECT dt.category, dt.total FROM (SELECT category, SUM(amount) AS total FROM sales GROUP BY category);
BSELECT dt.category, dt.total FROM (SELECT category, SUM(amount) AS total FROM sales GROUP BY category) AS dt;
CSELECT dt.category, dt.total FROM (SELECT category, SUM(amount) AS total FROM sales GROUP BY category) dt;
DSELECT dt.category, dt.total FROM (SELECT category, SUM(amount) AS total FROM sales GROUP BY category) AS dt WHERE dt.total > 100;
Attempts:
2 left
💡 Hint
Remember that every derived table must have an alias.
optimization
advanced
2: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;
AKeep the two derived tables and join them as is, since it is clearer and easier to read.
BUse a single derived table that calculates both sums in one pass: SELECT category, SUM(sales) AS total_sales, SUM(returns) AS total_returns FROM products GROUP BY category;
CUse two separate queries and combine results in application code to reduce database load.
DReplace derived tables with temporary tables to speed up the join.
Attempts:
2 left
💡 Hint
Think about how many times the table is scanned in each approach.
🔧 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, 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;
AThe query is valid and should run without errors.
BThe WHERE clause cannot reference columns from derived tables; it should be in HAVING.
CThe JOIN condition is invalid because it uses columns from different derived tables.
DThe alias dt1 is missing in the WHERE clause.
Attempts:
2 left
💡 Hint
Check if referencing derived table columns in WHERE is allowed after the JOIN.
🧠 Conceptual
expert
3:00remaining
Understanding scope and visibility of derived tables
Consider this query:

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?
Adt2 filters categories with total_sales above average before the outer query runs.
Bdt2 is redundant and can be removed without affecting the query result.
Cdt2 is the same as dt and causes a naming conflict leading to an error.
Ddt2 provides the average total sales across all categories to compare each category's total_sales against.
Attempts:
2 left
💡 Hint
Think about how the subquery calculates the average and how it relates to the outer query.