← PL/SQL Topics

SQL Integration

Explain CTEs (the WITH clause) in Oracle SQL

CTEs (WITH Clause)

Oracle Fusion / EBS · Technical · PL/SQL

GeneralHigh confidence

Names a subquery upfront so the main query can reference it by name, possibly more than once.

Grounded in: Curated Oracle knowledge layer — PL/SQL

How it works

A CTE (the WITH clause) names a subquery upfront so the main query can reference it — potentially more than once — by name, which reads more clearly than deeply nested inline subqueries and can let the optimizer materialize the result once instead of re-evaluating it. Oracle also supports a recursive form (WITH ... AS (... UNION ALL ...)) for hierarchical or graph-style traversal, an alternative to CONNECT BY.

How it works
WITH high_earners AS (
  SELECT employee_id, department_id, salary
  FROM employees
  WHERE salary > 10000
)
SELECT d.department_name, COUNT(*) AS high_earner_count
FROM high_earners h
JOIN departments d ON d.department_id = h.department_id
GROUP BY d.department_name;

Related Questions

Have a follow-up, or a different question?

Continue in Ask Oracle AI