PostgreSQL Enterprise (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What is a Common Table Expression (CTE) and how do you use the `WITH` clause?
junior LevelCode ExamplepostgresqlBasic
In Simple Words: A CTE creates a temporary, named result set that exists only within the execution scope of a single SQL query:
- Improves code readability by breaking complex multi-step queries into logical stages:
sql
WITH regional_sales AS (
SELECT region, SUM(amount) AS total_sales
FROM orders
GROUP BY region
),
top_regions AS (
SELECT region FROM regional_sales WHERE total_sales > 100000
)
SELECT * FROM top_regions;