Home / Learn Secure SQL / Common Table Expressions (WITH)
Common Table Expressions (WITH)
Naming a subquery for clarity.
Quick summaryNaming a subquery for clarity.
How to think about this
What it is
A CTE, written with WITH, names a subquery so the main query can refer to it by name, making long queries far more readable.
Why it matters
Readable queries are easier to review for correctness and safety; a reviewer who cannot follow the query cannot spot its problems.
Mechanically speaking
Write WITH name AS (SELECT ...) before the main query, then use name like a table inside it.
Shows up in
- Web application code review
- Backend development
- Database administration
A CTESQL: edit and run
The WITH block is defined once and used in the FROM/JOIN below it, instead of being nested and harder to read.
Check yourself
1. A CTE mainly improves...
Clarity for the reader/reviewer.