0
0
SQLquery~20 mins

Subquery in FROM clause (derived table) in SQL - 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 derived table with aggregation
Given the table sales with columns region, product, and amount, what is the output of this query?
SELECT region, total_sales FROM (SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region) AS region_totals ORDER BY total_sales DESC;
SQL
CREATE TABLE sales (region VARCHAR(10), product VARCHAR(10), amount INT);
INSERT INTO sales VALUES
('North', 'A', 100),
('South', 'B', 200),
('North', 'B', 150),
('East', 'A', 50),
('South', 'A', 100);
A[{"region": "East", "total_sales": 50}, {"region": "North", "total_sales": 250}, {"region": "South", "total_sales": 300}]
B[{"region": "South", "total_sales": 300}, {"region": "North", "total_sales": 250}, {"region": "East", "total_sales": 50}]
C[{"region": "North", "total_sales": 250}, {"region": "South", "total_sales": 300}, {"region": "East", "total_sales": 50}]
D[{"region": "South", "total_sales": 200}, {"region": "North", "total_sales": 150}, {"region": "East", "total_sales": 50}]
Attempts:
2 left
💡 Hint
Look at how the SUM aggregates amounts per region and then the outer query orders by total_sales descending.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in derived table usage
Which option contains a syntax error in using a subquery in the FROM clause?
ASELECT region, total FROM (SELECT region, COUNT(*) AS total FROM sales GROUP BY region) AS t;
BSELECT region, total FROM (SELECT region, COUNT(*) AS total FROM sales GROUP BY region) t
CSELECT region, total FROM (SELECT region, COUNT(*) AS total FROM sales GROUP BY region) AS;
DSELECT t.region, t.total FROM (SELECT region, COUNT(*) AS total FROM sales GROUP BY region) t;
Attempts:
2 left
💡 Hint
Check the alias syntax after the subquery in the FROM clause.
optimization
advanced
2:00remaining
Optimizing a query with a derived table
Consider this query:
SELECT d.region, d.avg_amount FROM (SELECT region, AVG(amount) AS avg_amount FROM sales GROUP BY region) d WHERE d.avg_amount > 100;

Which option is the most efficient way to write this query without changing the output?
ASELECT region, AVG(amount) AS avg_amount FROM sales GROUP BY region HAVING AVG(amount) > 100;
BSELECT region, avg_amount FROM sales WHERE avg_amount > 100 GROUP BY region;
CSELECT region, AVG(amount) AS avg_amount FROM sales WHERE amount > 100 GROUP BY region;
DSELECT region, AVG(amount) AS avg_amount FROM sales GROUP BY region WHERE AVG(amount) > 100;
Attempts:
2 left
💡 Hint
Try to use aggregation filtering directly without a derived table.
🔧 Debug
advanced
1:30remaining
Debugging incorrect column reference in derived table
Given the query:
SELECT dt.region, dt.total_sales FROM (SELECT region, SUM(amount) AS total FROM sales GROUP BY region) dt WHERE total_sales > 100;

What error will this query produce?
AColumn 'total_sales' does not exist
BNo error, query runs successfully
CSyntax error near 'WHERE'
DAmbiguous column name 'region'
Attempts:
2 left
💡 Hint
Check the alias names used inside and outside the derived table.
🧠 Conceptual
expert
1:30remaining
Understanding scope and naming in derived tables
Why is it necessary to provide an alias name for a subquery used in the FROM clause?
ABecause alias names automatically create indexes on the derived table
BBecause alias names improve query performance by caching results
CBecause alias names allow the subquery to run independently of the outer query
DBecause SQL requires every derived table to have a name to reference its columns in the outer query
Attempts:
2 left
💡 Hint
Think about how the outer query accesses the results of the subquery.