1

I've seen many examples of SQL with complex nested subqueries (and subsubqueries and subsubsubqueries and...). Is there ever any reason to write complex queries like this instead of using WITH and CTEs, just as one would use variables in a programming language to break up complex expressions?

In particular, is there a performance advantage?

2
  • 3
    Depending on dialect CTE could be inlined or materialized so yes, you could get performance gain. Second WITH could not be nested in some dialects where subquery can. But in general CTEs are easier to maintain and read. Commented Oct 27, 2018 at 8:28
  • 1
    CTEs are optimized differently for different DBMS. Your question can't really be answered without knowing which DBMS product you use.
    – user330315
    Commented Oct 27, 2018 at 9:07

3 Answers 3

2

Any query that you can write using only subqueries in the FROM clause and regular joins can use CTEs with direct substitution.

Subqueries are needed for:

  • Correlated subqueries (which are generally not in the FROM clause).
  • Lateral joins (in databases that support LATERAL or APPLY keywords in the FROM clause).
  • Scalar subqueries.

Sometimes, a query could be rewritten to avoid these constructs.

Subqueries in the FROM clause (except for lateral joins) can be written using CTEs.

Why are subqueries used and not CTEs? The most important reason is that CTEs are a later addition to the SQL language. With the exception of recursive CTEs, they are not really needed. They are really handy when a subquery is being referenced more than one time, but one could argue that a view serves the same purpose.

As mentioned in the comments, CTEs and subqueries might be optimized differently. This could be a performance gain or loss, depending on the query and the underlying indexes and so on.

1
  • Don't expect them to be optimised differently. CTEs are inlined.
    – J. Mini
    Commented Oct 18, 2023 at 20:37
1

Unless your query plan tells you that subquery performance is better than CTE otherwise I would use CTE instead of a subquery.

In particular, is there a performance advantage?

subquery vs simple (non-recursive) CTE versions, they are probably very similar. You would have to use the profiler and actual execution plan to spot any differences.

There are some reason I would use CTE

  1. In general, CTE can be used recursively but subquery cannot make it, which can help you make a calendar table or especially well suited to tree structures.
  2. CTE will be easier to maintain and read as (@Lukasz Szozda comment), because you can break up complex queries into several CTEs and give them good names, which will be very comfortable when writing in the main query.
0

Without performance considerations: CTEs are more readable as sql code, meaning easier to maintain and debug. Subqueries (at the FROM clause) are good as long as there are few, small and simple, thus converting to CTE would actually make it more difficult to read. There is also the option of views which mostly prevents sql code duplication. With performance considerations: CTEs may screw up the more complex they become. If so, they become too risky to be trusted with some teaks and changes and may lead to a more aggressive performance approach like converting all CTEs to temps (#). Subqueries behave as good as views and little better than CTEs in most cases. Still becoming too complex may hinter performance and turn performance optimization difficult. Eventually someone may need to tweak them or even extract the heavier(s) out to temps to lighten the main select. Views are slightly better on increased complexity as long as they are composed of plain tables and simple views, they have elegant SQL and possible filters are linked wherever possible within view's joins. Still joining two complex views will get you to the same situation as complex CTEs or subqueries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.