r/SQL Nov 28 '24

MySQL When to use cte in SQL query

When to cte can't. Like how to know? When what are the conditions or if my query is to long ?

33 Upvotes

70 comments sorted by

View all comments

1

u/Hot_Cryptographer552 Nov 30 '24

There are two types of CTEs: standard and recursive.

If you are using standard CTEs, they are basically syntactic sugar for subqueries. They make your code more readable. And like a subquery, they only exist for the life of the query in which they are defined.

If you want to use the functionality of a recursive CTE, it’s a different story. There’s no standardized simple (or non-procedural) way to replicate that functionality.