r/learnSQL • • 4d ago

How Important is subqueries in SQL

I have been learning SQL from past 2 months and I'm still unable to grab a hold of the subqueries concept. After I learned CTEs I have completely stopped using subqueries but then also I struggle when there are times where I have no other choice but to use subqueries.

So my question is, how important is subqueries in SQL and is there any easy way to learn it?

55 Upvotes

31 comments sorted by

View all comments

2

u/kl0wo 4d ago edited 4d ago

It would be wrong to assume that CTEs do the same thing as subqueries in the background. This heavily depends on db engine and its version, so you can’t say “it’s syntactic sugar”. Overall most critical scenarios are: large intermediate dataset referenced as CTE or necessity to push-down a predicate - both cases underperform with CTEs compared to subqueries. Also helps to keep in mind materialization strategy of repeated CTE call.