Database
SUBQUERIES and CTEs
Elizabeth Njoroge DEV Community
1 views
SUBQUERIES
Subquery is a query inside a query and is used when one statement needs a result from another. It is usually inside a clause like 'where' :
'from'
or 'select'.
CTE
A CTE(Common Table Expression) is a temporary named result set that one can reference within single query to reuse in the next query.
Advantages of CTE
CTEs tend to be preferred for readability and structuring complex structure.
A thing with CTEs is it is reusable in that you can reuse the definition without rewriting it.
Difference between subquery and CTE
CTEs support recursion while subqueries do not.
Subqueries can be correlated while CTE cannot.
conclusion
When writing and reading SQL the style I prefer and will pick is usually CTEs as they are easier to read and follow through. Also the fact that it takes less rework when going back to look or want to retrieve information.
Read original: https://dev.to/elizabeth_njoroge_7c850b9/subqueries-and-ctes-2e5n
← Previous
How do you keep a bigger Spring Boot application modular without creating more and more layers?
Next →
A Systems Engineer’s Guide to FTTP (Fibre to the Premises) Latency Baselines
Related
SQL Joins in PostgreSQL: Patients, Doctors, and the Rows That Go Missing
Database
6
Dev.to (EN Zone)
How To Design a Database for an E-commerce App
Database
7
Dev.to (EN Zone)
From a Synthetic AHP Event to a Real Base Sepolia Anchor
Database
9
DEV Community
Postgres 19 loses SQL/PGQ (and its release date)
Database
0
PostgreSQL Weekly
Comments0
No comments yet — be the first