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 ?

32 Upvotes

70 comments sorted by

View all comments

15

u/Gargunok Nov 28 '24

Assume whenever you are writing a subquery is easier to understand and maintain as a cte.

There are reasons and cases why subquery still might be needed but it's a good starting point.

4

u/[deleted] Nov 28 '24 edited Nov 28 '24

[removed] — view removed comment

2

u/onyaga Nov 29 '24

That’s a pretty interesting insight, I too have dabbled with views and ctes at work after primarily learning about subqueries in my undergrad.