0
0
SQLquery~5 mins

Subquery in FROM clause (derived table) in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a subquery in the FROM clause, also known as a derived table?
A subquery in the FROM clause is a query nested inside the FROM part of a main query. It acts like a temporary table that the main query can use to get results.
Click to reveal answer
beginner
Why do we use a subquery in the FROM clause?
We use it to simplify complex queries by breaking them into smaller parts. It helps to organize data before applying filters or joins in the main query.
Click to reveal answer
beginner
How do you give a name to a derived table in SQL?
You give a derived table an alias right after the closing parenthesis of the subquery. This alias is used to refer to the derived table in the main query.
Click to reveal answer
intermediate
Example: What does this SQL do? SELECT dt.category, dt.total_sales FROM (SELECT category, SUM(sales) AS total_sales FROM products GROUP BY category) AS dt WHERE dt.total_sales > 1000;
This query first calculates total sales per category inside the subquery (derived table). Then, the main query selects categories where total sales are more than 1000.
Click to reveal answer
intermediate
Can a derived table be used with JOINs in the main query?
Yes, a derived table can be joined with other tables or derived tables in the main query to combine and filter data effectively.
Click to reveal answer
What is the purpose of a subquery in the FROM clause?
ATo update data in the database
BTo create a temporary table for the main query to use
CTo filter rows after the main query runs
DTo delete rows from a table
How do you refer to a derived table in the main query?
ABy the column names
BBy its original table name
CBy the database name
DBy an alias given after the subquery
Which SQL keyword is used to name a derived table?
AAS
BFROM
CWHERE
DGROUP BY
Can a derived table contain aggregate functions like SUM or COUNT?
AYes, it can perform aggregations
BNo, aggregates are not allowed
COnly COUNT is allowed
DOnly SUM is allowed
Is this SQL valid? SELECT * FROM (SELECT id FROM users) WHERE id > 10;
AYes, it is valid
BNo, subqueries cannot be in FROM
CNo, the derived table needs an alias
DNo, WHERE cannot be used here
Explain what a derived table is and how it is used in SQL queries.
Think about how you can create a small table inside a query to help with filtering or joining.
You got /4 concepts.
    Describe the steps to write a query using a subquery in the FROM clause with an example.
    Start with a simple aggregation inside the subquery, then filter results in the main query.
    You got /5 concepts.