Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using Subquery in FROM Clause (Derived Table)
📖 Scenario: You work for a small bookstore that keeps track of book sales. You have a table called sales that records each sale with the book's book_id and the quantity sold.Your manager wants to see the total quantity sold for each book, but only for books that sold more than 10 copies in total.
🎯 Goal: Create a SQL query that uses a subquery in the FROM clause (a derived table) to calculate total sales per book, then filters to show only books with total sales greater than 10.
📋 What You'll Learn
Create a table called sales with columns book_id and quantity.
Insert the exact sales data provided.
Write a subquery in the FROM clause that sums quantity per book_id.
Filter the results to show only books with total quantity sold greater than 10.
💡 Why This Matters
🌍 Real World
Derived tables help organize complex queries by breaking them into smaller parts, making it easier to analyze grouped data like sales totals.
💼 Career
Knowing how to use subqueries in the FROM clause is useful for data analysts and developers who write reports or dashboards that summarize data.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns book_id (integer) and quantity (integer). Then insert these exact rows: (1, 5), (2, 8), (1, 7), (3, 3), (2, 5), (3, 10).
SQL
Hint
Use CREATE TABLE to make the table, then INSERT INTO to add the rows exactly as given.
2
Write a subquery to sum quantity per book
Write a subquery named total_sales in the FROM clause that selects book_id and the sum of quantity as total_quantity from sales, grouped by book_id.
SQL
Hint
Use SELECT book_id, SUM(quantity) AS total_quantity FROM sales GROUP BY book_id inside the subquery.
3
Use the subquery as a derived table in the FROM clause
Write a query that selects book_id and total_quantity from a derived table named total_sales which is the subquery summing quantity per book from sales.
SQL
Hint
Wrap the subquery in parentheses and give it the alias total_sales. Then select from it.
4
Filter to show only books with total sales greater than 10
Add a WHERE clause to the outer query to show only rows where total_quantity is greater than 10.
SQL
Hint
Use WHERE total_quantity > 10 after the derived table to filter results.
Practice
(1/5)
1. What is the main purpose of using a subquery in the FROM clause in SQL?
easy
A. To update values in a table
B. To permanently store data in the database
C. To delete rows from a table
D. To create a temporary table that can be used by the main query
Solution
Step 1: Understand the role of subqueries in FROM clause
Subqueries in the FROM clause act like temporary tables that the main query can use to simplify complex operations.
Step 2: Differentiate from other SQL operations
Unlike DELETE or UPDATE, subqueries in FROM do not modify data but help organize data for selection.
Final Answer:
To create a temporary table that can be used by the main query -> Option D
Quick Check:
Subquery in FROM = temporary table [OK]
Hint: Subquery in FROM creates a temp table for main query use [OK]
Common Mistakes:
Thinking subquery stores data permanently
Confusing subquery with DELETE or UPDATE commands
Forgetting subquery is temporary, not permanent
2. Which of the following is the correct syntax for using a subquery in the FROM clause?
easy
A. SELECT * FROM (SELECT id FROM users) AS sub;
B. SELECT * FROM users WHERE (SELECT id FROM users);
C. SELECT * FROM users AS (SELECT id FROM users);
D. SELECT * FROM users (SELECT id FROM users);
Solution
Step 1: Identify correct subquery syntax in FROM
The subquery must be enclosed in parentheses and given an alias using AS.
Step 2: Check each option
SELECT * FROM (SELECT id FROM users) AS sub; correctly uses parentheses and alias. Others misuse WHERE, alias placement, or parentheses.
Final Answer:
SELECT * FROM (SELECT id FROM users) AS sub; -> Option A
Quick Check:
Subquery in FROM needs parentheses + alias [OK]
Hint: Subquery in FROM needs parentheses and alias [OK]
Common Mistakes:
Omitting alias for subquery
Placing subquery in WHERE instead of FROM
Incorrect alias placement
3. Given the tables: users(id, name) orders(id, user_id, amount) What will this query return?
SELECT sub.name, sub.total FROM (SELECT u.name, SUM(o.amount) AS total FROM users u JOIN orders o ON u.id = o.user_id GROUP BY u.name) AS sub WHERE sub.total > 100;
medium
A. Names of users with total order amount greater than 100
B. All users with their total order amount
C. Syntax error due to missing alias
D. Empty result because no users have orders
Solution
Step 1: Understand the subquery
The subquery calculates total order amount per user by joining users and orders and grouping by user name.
Step 2: Apply the outer WHERE filter
The outer query filters to only include users whose total order amount is greater than 100.
Final Answer:
Names of users with total order amount greater than 100 -> Option A
Quick Check:
Subquery sums orders; outer filters total > 100 [OK]
SELECT sub.name, sub.total FROM (SELECT name, SUM(amount) AS total FROM users JOIN orders ON users.id = orders.user_id) sub;
medium
A. Missing alias for subquery
B. Incorrect JOIN syntax
C. Missing GROUP BY clause in subquery
D. Using subquery in WHERE clause instead of FROM
Solution
Step 1: Analyze the subquery aggregation
The subquery uses SUM(amount) but does not group by name, which is required when selecting non-aggregated columns.
Step 2: Confirm alias and JOIN syntax
The subquery has an alias 'sub' and JOIN syntax is correct, so these are not errors.
Final Answer:
Missing GROUP BY clause in subquery -> Option C
Quick Check:
Aggregation needs GROUP BY for non-aggregated columns [OK]
Hint: Aggregation needs GROUP BY for other selected columns [OK]
Common Mistakes:
Forgetting GROUP BY with aggregation
Confusing alias requirement
Misreading JOIN syntax
5. You want to find the average order amount per user but only for users who have placed more than 3 orders. Which query correctly uses a subquery in the FROM clause to achieve this?
hard
A. SELECT user_id, AVG(amount) FROM (SELECT * FROM orders WHERE COUNT(*) > 3) AS sub GROUP BY user_id;
B. SELECT sub.user_id, sub.avg_amount FROM (SELECT user_id, AVG(amount) AS avg_amount FROM orders GROUP BY user_id HAVING COUNT(*) > 3) AS sub;
C. SELECT user_id, AVG(amount) FROM orders WHERE COUNT(*) > 3 GROUP BY user_id;
D. SELECT user_id, AVG(amount) FROM orders GROUP BY user_id HAVING AVG(amount) > 3;
Solution
Step 1: Understand the requirement
We need average order amount per user but only for users with more than 3 orders.
Step 2: Analyze each option
SELECT sub.user_id, sub.avg_amount FROM (SELECT user_id, AVG(amount) AS avg_amount FROM orders GROUP BY user_id HAVING COUNT(*) > 3) AS sub; correctly uses a subquery to group orders by user_id, filters users with more than 3 orders using HAVING, then calculates average amount.
Final Answer:
SELECT sub.user_id, sub.avg_amount FROM (SELECT user_id, AVG(amount) AS avg_amount FROM orders GROUP BY user_id HAVING COUNT(*) > 3) AS sub; -> Option B
Quick Check:
Subquery filters users by order count, outer selects average [OK]
Hint: Use HAVING in subquery to filter groups before outer select [OK]