Database
SUBQUERIES and CTEs
Elizabeth Njoroge DEV Community
22 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
Postgres RLS in Symfony: three green isolation tests, and the UPDATE that moves tenant 1's invoices into tenant 2's books
Database
35
Dev.to (EN Zone)
Turning Mermaid ER diagrams into shareable 3D schema tours
Database
31
DEV Community
finally understand why redis blocks on one slow command, wish I knew this earlier
Database
28
Reddit r/webdev
Tracing Which Internal Consumer Read Each Webhook Event (One Registration, One Queue)
Database
30
DEV Community
Comments0
No comments yet — be the first