Interview Question

What is CTE?

A CTE gives a temporary name to a query used by one statement.

💡 Concept ✅ Quick Revision 🗃️ SQL

Answer

A common table expression is a named auxiliary query defined by WITH for one statement. • Later parts of the statement can reference its name. • Multiple CTEs can make a complex query easier to read. • Materialization and optimization behavior can depend on the database and query.

💡 SQL Example

WITH active_employees AS (SELECT * FROM employees WHERE active = true) SELECT COUNT(*) FROM active_employees;

Result

5

⚡ Quick Revision

A CTE gives a temporary name to a query used by one statement.