Database
SUBQUERIES and CTEs
Elizabeth Njoroge DEV Community
2 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
Comments0
No comments yet — be the first