Quick revision
The short answer
A derived table is a subquery in the FROM clause, with an alias, that the outer query uses like a temporary table for the duration of that statement. A CTE, written with WITH, gives such a subquery a name at the top of the statement so it can be read more easily and referenced more than once, and recursive queries need the WITH RECURSIVE form. A view is a named query stored in the schema and reusable by any statement. A derived table is needed whenever we must filter on an aggregate or window result, such as ranking rows and keeping rank <= 3.
What to remember
In this answer, we saw that a derived table is an aliased subquery in FROM, that a CTE names such a subquery at the top of one statement for readability and reuse and is required for recursion, and that a view is a stored, shared query in the schema. A derived table or CTE is needed to filter on a window result, such as rank <= 3, because window functions run after WHERE. We also saw that PostgreSQL 12 and later inline non-recursive CTEs used once unless we write MATERIALIZED.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A derived table is a subquery in the FROM clause, with an alias, that the outer query uses like a temporary table for the duration of that statement. A CTE, written with WITH, gives such a subquery a name at the top of the statement so it can be read more easily and referenced more than once, and recursive queries need the WITH RECURSIVE form. A view is a named query stored in the schema and reusable by any statement. A derived table is needed whenever we must filter on an aggregate or window result, such as ranking rows and keeping rank <= 3.
Why this question is asked
Derived tables, CTEs and views all wrap a query so another query can use its result, and beginners often treat them as interchangeable. They differ in scope, in how long they live, and in how the optimizer treats them, and those differences show up in readability, reuse and performance. Interviewers ask this to check that we understand each tool and can pick one for a reason. In this answer, we will define each one with an example, show a case where a derived table is required, and look at how PostgreSQL optimises CTEs since version 12.
1A Quick Recap: Using a Query's Result as a Table
Every SELECT produces a result that has rows and columns, just like a table. SQL lets us use that result where a table is expected. The three tools differ in where the wrapped query is written and how long its name exists: a derived table is written inline and exists only in its place in one statement, a CTE is written at the top of one statement and can be used anywhere in it, and a view is stored in the database and exists until it is dropped.
2Derived Tables: A Subquery in FROM
A derived table is a subquery in the FROM clause. It needs an alias so the outer query can refer to its columns. Here it computes total sales per customer, and the outer query filters on that total.
This example could also be written with HAVING SUM(total) > 1000, so the derived table is not strictly needed here. It becomes necessary when the value we filter on is computed by something that runs after WHERE and HAVING, which is the case for window functions. PostgreSQL historically required the alias as well; recent versions accept a subquery in FROM without one (check your version), but giving it a name is good practice anyway.
3When a Derived Table Is Needed: Filtering on a Rank
Suppose we want the three highest-paid employees in each department. RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) gives each employee a rank within the department. But we cannot write WHERE RANK() OVER (...) <= 3, because SQL evaluates window functions after WHERE, GROUP BY and HAVING. When WHERE runs, the rank does not exist yet. So we compute the rank in a derived table and filter it in the outer query, where the rank is an ordinary column.
- FROM and JOINincluding derived tables
- WHERErow filters
- GROUP BY and HAVINGaggregates
- Window functionsrank, row_number, avg over
- SELECT, DISTINCT, ORDER BY, LIMIT
5 steps in order. Play them, or step through them one at a time.
A window result can only be filtered by an outer query, which is why the derived table is needed.
Notice the tie. RANK gives equal salaries the same rank and then skips a number, so with ties we may get fewer distinct ranks than three and more than three people. ROW_NUMBER would give exactly three rows per department by breaking ties arbitrarily, and DENSE_RANK would give the top three distinct salaries. Choosing among them is a business decision worth asking about.
4CTEs: Naming the Subquery
A CTE writes the same subquery at the top of the statement with WITH and a name. The query reads top to bottom, and each step can refer to the ones before it, which makes long queries far easier to follow than nested derived tables. A CTE can also be referenced more than once in the same statement.
Recursive queries, such as walking an org chart, are only possible with WITH RECURSIVE; a derived table cannot refer to itself. In PostgreSQL, CTEs used to be optimisation fences: before version 12, every CTE was computed once on its own and its result stored, so conditions from the outer query could not be pushed into it. Since PostgreSQL 12, a non-recursive CTE without side effects that is referenced once is inlined into the main query by default, so it is optimised like a derived table. We can still ask for the old behaviour with AS MATERIALIZED, or force inlining of a CTE used twice with AS NOT MATERIALIZED.
5Views: A Stored, Shared Query
A view is created once with CREATE VIEW and stored in the schema. Any query, from any session or service, can select from it as if it were a table. A plain view stores only the query, not the data; each use runs the underlying query, and the planner usually merges it into the outer query, like an inlined CTE.
| Derived table | CTE | View | |
|---|---|---|---|
| Where it is defined | Inline in FROM | WITH at the top of a statement | In the schema |
| Lifetime | One statement, one place | One statement | Until dropped |
| Reuse | None | Several times in the statement | Any statement |
| Recursion | No | WITH RECURSIVE | Via a recursive CTE inside |
| Optimiser (PostgreSQL 12+) | Merged | Inlined unless MATERIALIZED | Merged |
Views are good for sharing a definition across many queries and for giving restricted access to a subset of columns. They are also a contract: changing a view can break every query that uses it, so they need the same care as tables. A materialised view, by contrast, stores the result and must be refreshed, which trades freshness for speed. So, in short, a derived table is local to one spot, a CTE is local to one statement, and a view is shared by everyone.
6Follow-up questions
Is a CTE slower than a derived table?
In PostgreSQL 12 and later, usually not, because a CTE used once is inlined and optimised the same way. On PostgreSQL 11 and earlier, a CTE was always materialised, which could make it slower when outer filters should have been pushed in, or faster when the fence prevented a bad plan. A CTE referenced twice is still materialised by default, which avoids computing it twice. MySQL 8 can merge or materialise CTEs and derived tables based on cost.
Can I update data through a view?
Simple views over one table, without aggregates, DISTINCT, window functions or set operations, are automatically updatable in PostgreSQL and MySQL. Views like top_paid, which use a window function, are not, unless we add INSTEAD OF triggers. WITH CHECK OPTION can make sure rows written through a view still satisfy its WHERE clause.
When would you use a temporary table instead?
When an intermediate result is used by several statements, or when it is large and we want to index it or analyse it for better estimates. A temporary table lives for the session or transaction, can have indexes and statistics, and is visible to all later statements in that session, which none of the three tools above provide.
7Conclusion
In this answer, we saw that a derived table is an aliased subquery in FROM, that a CTE names such a subquery at the top of one statement for readability and reuse and is required for recursion, and that a view is a stored, shared query in the schema. A derived table or CTE is needed to filter on a window result, such as rank <= 3, because window functions run after WHERE. We also saw that PostgreSQL 12 and later inline non-recursive CTEs used once unless we write MATERIALIZED.
Open with
A derived table is a subquery in the FROM clause with an alias, and the outer query uses its result as if it were a table, only within that statement and only in that one place.
Then, if they want more
A CTE is the same idea written with WITH at the top of the statement: it gets a name, the query reads top to bottom, and I can reference it several times in the same statement. Recursive queries, like walking a hierarchy, need WITH RECURSIVE.
A view is a named query stored in the schema, so any statement or service can use it, and it is a shared contract, so it needs the care of a table. A classic case where I need a derived table is filtering on a window function.
If I want the top three earners per department, I compute rank over partition by department order by salary descending in a subquery, and filter rank <= 3 outside, because window functions are evaluated after WHERE and are not allowed there. I would also check whether ties matter, choosing rank, dense_rank or row_number.
On performance, PostgreSQL before version 12 always materialised CTEs, so they were optimisation fences. Since 12, a non-recursive CTE used once is inlined like a derived table, and I can control it with MATERIALIZED or NOT MATERIALIZED. Plain views are also merged into the query, while materialised views store the result and need refreshing.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 12, MySQL 8, PostgreSQL 11
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a derived table, and how does it differ from a CTE and a view? Show an example...": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/17a704fca5/