A derived table is a subquery used inside the FROM clause that acts like a temporary table for the main query to use.
Click to reveal answer
beginner
Why do we use subqueries in the FROM clause?
We use subqueries in the FROM clause to create temporary tables that simplify complex queries or to prepare data before filtering or joining.
Click to reveal answer
beginner
How do you give a name to a derived table?
You give a derived table an alias right after the closing parenthesis of the subquery, like this: (SELECT ...) AS alias_name.
Click to reveal answer
intermediate
Can a derived table be joined with other tables?
Yes, a derived table can be joined with other tables just like a regular table in the FROM clause.
Click to reveal answer
intermediate
Write a simple example of a derived table that calculates average sales per product.
SELECT avg_sales.product_id, avg_sales.avg_amount FROM (SELECT product_id, AVG(amount) AS avg_amount FROM sales GROUP BY product_id) AS avg_sales;
Click to reveal answer
What keyword is used immediately after a subquery in the FROM clause?
AAS
BWHERE
CJOIN
DGROUP BY
✗ Incorrect
The alias keyword AS is used to name the derived table created by the subquery.
Which clause can contain a derived table?
AFROM
BWHERE
CORDER BY
DHAVING
✗ Incorrect
Derived tables are subqueries placed inside the FROM clause.
Can a derived table be used to filter data before joining?
AOnly in MySQL
BYes
CNo
DOnly with GROUP BY
✗ Incorrect
Derived tables can prepare or filter data before joining with other tables.
What happens if you omit the alias for a derived table?
AQuery runs but returns no rows
BDerived table is ignored
CSyntax error
DAlias is automatically assigned
✗ Incorrect
SQL requires an alias for derived tables; omitting it causes a syntax error.
Which of these is a valid use of a derived table?
ASELECT * FROM users GROUP BY (SELECT id FROM users);
BSELECT * FROM users WHERE (SELECT id FROM users);
CSELECT * FROM users JOIN (WHERE id > 5);
DSELECT * FROM (SELECT id FROM users) AS u;
✗ Incorrect
Option D correctly uses a subquery in the FROM clause with an alias.
Explain what a derived table is and why it is useful in SQL queries.
Think about how you might create a small table inside a bigger query.
You got /4 concepts.
Describe the steps to write a query using a subquery in the FROM clause with an alias.
Remember the alias is mandatory for the subquery in FROM.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a subquery in the FROM clause in PostgreSQL?
easy
A. To create a permanent table in the database
B. To update data in multiple tables at once
C. To delete rows from a table based on a condition
D. To create a temporary table that can be used in the main query
Solution
Step 1: Understand the role of subqueries in FROM
Subqueries in the FROM clause act like temporary tables that exist only during the query execution.
Step 2: Compare with other options
Updating or deleting data and creating permanent tables are not done by subqueries in FROM.
Final Answer:
To create a temporary table that can be used in the main query -> Option D
Quick Check:
Subquery in FROM = temporary table [OK]
Hint: Subquery in FROM = think temporary table [OK]
Common Mistakes:
Thinking subqueries in FROM create permanent tables
Confusing subqueries with UPDATE or DELETE statements
Forgetting subqueries need an alias
2. Which of the following is the correct syntax for using a subquery in the FROM clause in PostgreSQL?
easy
A. SELECT * FROM (SELECT id FROM users) AS sub;
B. SELECT * FROM users WHERE (SELECT id FROM users);
C. SELECT * FROM users JOIN (SELECT id FROM users);
D. SELECT * FROM users (SELECT id FROM users) AS sub;
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 either misuse WHERE, JOIN without ON, or lack alias.
Final Answer:
SELECT * FROM (SELECT id FROM users) AS sub; -> Option A
Quick Check:
Subquery in FROM needs parentheses + alias [OK]
Hint: Always alias subqueries in FROM with AS [OK]
Common Mistakes:
Omitting alias after subquery
Using subquery in WHERE without proper syntax
Trying to JOIN without ON condition
3. Given the tables: users(id, name) orders(id, user_id, amount) What will the following query return?
SELECT sub.name, sub.total FROM (SELECT u.name, SUM(o.amount) AS total FROM users u LEFT JOIN orders o ON u.id = o.user_id GROUP BY u.name) AS sub WHERE sub.total > 100;
medium
A. Syntax error due to missing GROUP BY
B. All users with their total order amount including those with zero
C. Names of users with total order amount greater than 100
D. Empty result because no user has orders
Solution
Step 1: Analyze the subquery
The subquery calculates total order amount per user using LEFT JOIN and GROUP BY user name.
Step 2: Apply the outer WHERE filter
The outer query filters users whose total order amount is greater than 100.
Final Answer:
Names of users with total order amount greater than 100 -> Option C
Quick Check:
Subquery sums orders, outer filters total > 100 [OK]
SELECT sub.name, sub.order_count FROM (SELECT name, COUNT(*) FROM users GROUP BY name) sub;
medium
A. No error, query is correct
B. Missing alias for COUNT(*) column
C. Incorrect table name 'users'
D. Subquery missing parentheses
Solution
Step 1: Check subquery SELECT list
COUNT(*) needs an alias to be referenced outside the subquery.
Step 2: Understand outer query references
Outer query uses sub.order_count, but COUNT(*) column has no alias, causing error.
Final Answer:
Missing alias for COUNT(*) column -> Option B
Quick Check:
Aggregate columns need aliases in subqueries [OK]
Hint: Always alias aggregate columns in subqueries [OK]
Common Mistakes:
Forgetting to alias aggregate functions
Assuming subquery columns auto-named
Ignoring parentheses around subquery
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 sub.user_id, sub.avg_amount FROM (SELECT user_id, AVG(amount) AS avg_amount, COUNT(*) AS order_count FROM orders GROUP BY user_id) AS sub WHERE sub.order_count > 3;
B. SELECT user_id, AVG(amount) FROM orders WHERE COUNT(*) > 3 GROUP BY user_id;
C. SELECT user_id, AVG(amount) FROM (SELECT * FROM orders WHERE COUNT(*) > 3) AS sub 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, COUNT(*) AS order_count FROM orders GROUP BY user_id) AS sub WHERE sub.order_count > 3; uses a subquery to calculate average and count per user, then filters users with order_count > 3 outside. Others misuse COUNT in WHERE or HAVING incorrectly.
Final Answer:
SELECT sub.user_id, sub.avg_amount FROM (SELECT user_id, AVG(amount) AS avg_amount, COUNT(*) AS order_count FROM orders GROUP BY user_id) AS sub WHERE sub.order_count > 3; -> Option A
Quick Check:
Subquery calculates aggregates; outer filters by count [OK]
Hint: Filter aggregates in outer query after subquery [OK]
Common Mistakes:
Using COUNT(*) in WHERE clause directly
Misplacing HAVING clause conditions
Not aliasing subquery columns properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.