Bird
Raised Fist0
SQLquery~10 mins

Subquery in FROM clause (derived table) in SQL - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select all columns from a derived table named 'sub'.

SQL
SELECT [1] FROM (SELECT id, name FROM employees) AS sub;
Drag options to blanks, or click blank then click option'
Aid
Bname
C*
Dsub
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting the alias name instead of columns.
Selecting only one column when all are needed.
2fill in blank
medium

Complete the code to alias the derived table as 'dept_avg'.

SQL
SELECT department, avg_salary FROM (SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department) AS [1];
Drag options to blanks, or click blank then click option'
Adept_avg
Bsalary_avg
Caverage
Demp_avg
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic alias that doesn't match the data.
Forgetting to alias the derived table.
3fill in blank
hard

Fix the error in the code by completing the alias for the derived table.

SQL
SELECT name, total_sales FROM (SELECT name, SUM(sales) AS total_sales FROM sales_data GROUP BY name) [1];
Drag options to blanks, or click blank then click option'
Asales_summary
BBY
CAS
DAS sales_summary
Attempts:
3 left
💡 Hint
Common Mistakes
Including the keyword AS incorrectly after the derived table.
Omitting the alias entirely.
4fill in blank
hard

Fill both blanks to select the average salary and alias the derived table correctly.

SQL
SELECT department, [1] FROM (SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department) [2];
Drag options to blanks, or click blank then click option'
Aavg_salary
BAVG(salary)
Cdept_avg
Daverage_salary
Attempts:
3 left
💡 Hint
Common Mistakes
Using the aggregate function directly in the outer query.
Incorrectly aliasing the derived table.
5fill in blank
hard

Fill all three blanks to create a derived table that calculates total sales per region and select from it.

SQL
SELECT [1], [2] FROM (SELECT region, SUM(sales) AS [3] FROM sales GROUP BY region) sales_totals;
Drag options to blanks, or click blank then click option'
Aregion
Btotal_sales
Dsales_sum
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching the alias names between inner and outer queries.
Selecting columns not present in the derived table.

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

  1. 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.
  2. Step 2: Differentiate from other SQL operations

    Unlike DELETE or UPDATE, subqueries in FROM do not modify data but help organize data for selection.
  3. Final Answer:

    To create a temporary table that can be used by the main query -> Option D
  4. 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

  1. Step 1: Identify correct subquery syntax in FROM

    The subquery must be enclosed in parentheses and given an alias using AS.
  2. 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.
  3. Final Answer:

    SELECT * FROM (SELECT id FROM users) AS sub; -> Option A
  4. 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

  1. Step 1: Understand the subquery

    The subquery calculates total order amount per user by joining users and orders and grouping by user name.
  2. Step 2: Apply the outer WHERE filter

    The outer query filters to only include users whose total order amount is greater than 100.
  3. Final Answer:

    Names of users with total order amount greater than 100 -> Option A
  4. Quick Check:

    Subquery sums orders; outer filters total > 100 [OK]
Hint: Subquery calculates totals; outer query filters results [OK]
Common Mistakes:
  • Ignoring the WHERE filter on total
  • Assuming all users are returned
  • Confusing alias usage
4. Identify the error in this query:
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

  1. 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.
  2. Step 2: Confirm alias and JOIN syntax

    The subquery has an alias 'sub' and JOIN syntax is correct, so these are not errors.
  3. Final Answer:

    Missing GROUP BY clause in subquery -> Option C
  4. 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

  1. Step 1: Understand the requirement

    We need average order amount per user but only for users with more than 3 orders.
  2. 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.
  3. 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
  4. Quick Check:

    Subquery filters users by order count, outer selects average [OK]
Hint: Use HAVING in subquery to filter groups before outer select [OK]
Common Mistakes:
  • Using WHERE with aggregation functions
  • Placing HAVING outside GROUP BY context
  • Not using subquery to filter groups first