When would you choose a CTE over a subquery, and what are the trade-offs for analytical queries?

Quick Overview

Asks when to write a common table expression instead of a subquery in analytical SQL. Tests reasoning about readability, reuse within a query, recursion, and how query optimizers inline or materialize CTEs and what that does to performance.

When would you choose a CTE over a subquery, and what are the trade-offs for analytical queries?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

When would you choose a CTE over a subquery, and what are the trade-offs in readability, reusability, and performance for analytical queries? ```hint Check what the optimizer does Readability is easy to argue. For performance, ask whether the engine inlines the CTE into the main query or computes it separately, and what each choice does to filters in the outer query. ``` ### What a Strong Answer Covers - When a CTE is the better choice and when an inline subquery is - How each form affects the readability of multi-step analytical logic - What reuse inside one query means, and the one thing only a CTE can express - How engines treat CTEs (inlining or materialization), including the effect on predicate pushdown, and how to check the plan ### Follow-up Questions - In PostgreSQL 12 and later, when is a CTE materialized, and how do `MATERIALIZED` and `NOT MATERIALIZED` change that? - A CTE is referenced three times in one query. Is it computed once or three times on the engine you use, and how would you find out? - When would a temporary table or a view be a better choice than either a CTE or a subquery? - Write a recursive CTE that returns every employee under a given manager, with each one's depth in the hierarchy.

Overview: Asks when to write a common table expression instead of a subquery in analytical SQL. Tests reasoning about readability, reuse within a query, recursion, and how query optimizers inline or materialize CTEs and what that does to performance.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

When would you choose a CTE over a subquery, and what are the trade-offs in readability, reusability, and performance for analytical queries?

What a Strong Answer Covers Guidance

  • When a CTE is the better choice and when an inline subquery is
  • How each form affects the readability of multi-step analytical logic
  • What reuse inside one query means, and the one thing only a CTE can express
  • How engines treat CTEs (inlining or materialization), including the effect on predicate pushdown, and how to check the plan

Follow-up Questions Guidance

  • In PostgreSQL 12 and later, when is a CTE materialized, and how do MATERIALIZED and NOT MATERIALIZED change that?
  • A CTE is referenced three times in one query. Is it computed once or three times on the engine you use, and how would you find out?
  • When would a temporary table or a view be a better choice than either a CTE or a subquery?
  • Write a recursive CTE that returns every employee under a given manager, with each one's depth in the hierarchy.