0
0
PostgreSQLquery~5 mins

Subqueries in FROM (derived tables) in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a derived table in SQL?
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
Which clause can contain a derived table?
AFROM
BWHERE
CORDER BY
DHAVING
Can a derived table be used to filter data before joining?
AOnly in MySQL
BYes
CNo
DOnly with GROUP BY
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
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;
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.