187

From this post How to use ROW_NUMBER in the following procedure?

There are two versions of answers where one uses a sub-query and the other uses a CTE to solve the same problem.

Now then, what is the advantage of using a CTE (Common Table Expression) over a 'sub-query`(thus, more readable what the query is actually doing)

The only advantage of using a CTE over sub-select is that I can actually name the sub-query. Are there any other differences between those two when a CTE is used as a simple (non-recursive) CTE?

Archmede
  • 1,592
  • 2
  • 20
  • 37
dance2die
  • 35,807
  • 39
  • 131
  • 194
  • Derivative question with good discussion: http://stackoverflow.com/q/11169550/781695 – user Dec 24 '14 at 10:54
  • 12
    IMO, anyone who thinks a CTE is *less* readable that a gigantic blob of interwoven subqueries hasn't seen the garbage pile of confusing saw-teeth-shaped queries in use across the majority of enterprise data management systems. Large, non-trivial queries are typically dramatically easier to read later or by new eyes than subqueries, and at least in the case of Postgres magically perform *much* better in many cases. ([For reasons I have yet to understand[(http://stackoverflow.com/questions/33731068/postgres-cte-vs-subquery-performance-difference-why), as the opposite seems more likely.) – zxq9 Nov 16 '15 at 08:45

10 Answers10

126

In the sub-query 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, and that would be specific to your setup (so we can't tell you the answer in full).

In general; A CTE can be used recursively; a sub-query cannot. This makes them especially well suited to tree structures.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Sorry, I should have been more clearer in the my question. What would be the difference between CTE and Subquery in the context where CTE is used LIKE subquery? – dance2die Apr 01 '09 at 19:22
  • 2
    @Marc Gravell: We can do more than that though, as the behavior of the profiler is not guaranteed, vs the behavior of the CTE, which is (in terms of evaluation). – casperOne Apr 01 '09 at 19:32
  • 3
    Not sure how much this statement make sense for people looking at CTS and subquery difference - `A CTE can be used recursively; a sub-query cannot`. An example would have been great. – Aniket Thakur Dec 28 '17 at 10:43
107

The main advantage of the Common Table Expression (when not using it for recursive queries) is encapsulation, instead of having to declare the sub-query in every place you wish to use it, you are able to define it once, but have multiple references to it.

However, this does not mean that it is executed only once (as per previous iterations of this very answer, thank you to all those that have commented). The query definitely has the potential to be executed multiple times if referenced multiple times; the query optimizer ultimately makes the decision as to how the CTE should be interpreted.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
casperOne
  • 73,706
  • 19
  • 184
  • 253
  • 1
    "Think of a CTE as a temp table variable" does that mean CTE is stored in disk or in memory? – dance2die Apr 01 '09 at 19:26
  • 1
    You cannot use the CTE or subquery in multiple queries, by definition. I'm pretty sure that the optimizer handles the subquery the same way it would handle the CTE (evaluating the result set only once, regardless of how many times it is used within the 1 query) – AlexCuse Apr 01 '09 at 19:28
  • @AlexCuse: I think I've clarified the context of the CTE enough, but I added more to try and clarify more. – casperOne Apr 01 '09 at 19:31
  • @AlexCuse: There is also no implication that the CTE or subquery can be used in multiple places. The difference between the CTE and optimizer though is that the behavior of the CTE is guaranteed, whereas the behavior of the optimizer is not. – casperOne Apr 01 '09 at 19:31
  • and I will concede that there could be some edge cases where the optimizer chokes and the subquery is evaluated more than once, I have not run into any though. Then again, I use CTE's wherever I can ;) – AlexCuse Apr 01 '09 at 19:36
  • @AlexCuse: By multiple places, I mean outside of the query that the subquery or the CTE is attached to. In other words, the CTE is only applicable for ONE query. – casperOne Apr 01 '09 at 19:43
18

CTE's are most useful for recursion:

WITH hier(cnt) AS (
        SELECT  1
        UNION ALL
        SELECT  cnt + 1
        FROM    hier
        WHERE   cnt < @n
        )
SELECT  cnt
FROM    hier

will return @n rows (up to 101). Useful for calendars, dummy rowsets etc.

They are also more readable (in my opinion).

Apart from this, CTE's and subqueries are identical.

Gabriel GM
  • 6,391
  • 2
  • 31
  • 34
Quassnoi
  • 413,100
  • 91
  • 616
  • 614
  • In MSSQL, you need to add a semicolon (;) before WITH, order wise you will get an error. it should be `;WITH blabla AS ...)` – Obinna Nnenanya Dec 06 '18 at 18:01
  • 3
    @ObinnaNnenanya: only if it's not the first statement in the batch. Terminating your statements with semicolons is a good idea anyway, even though SQL Server does not enforce it in the current versions other than before `WITH`, `MERGE` and similar – Quassnoi Dec 06 '18 at 18:10
14

One difference that hasn't been mentioned is a single CTE can be referenced in the several parts of a union

user340140
  • 628
  • 6
  • 10
10

One important fact that nobody has mentioned is that (at least in postgres), CTEs are optimization fences:

https://blog.2ndquadrant.com/postgresql-ctes-are-optimization-fences/

That is, they will be treated as their own atomic query, rather than folded into the whole query plan. I lack the expertise to give a better explanation, but you should check the semantics for the version of sql you are using; for advanced users, being able to create an optimization fence can help performance if you are expert level in controlling query planner; in 99% of cases, however, you should avoid trying to tell the query planner what to do, because what you think will be faster is likely worse than what it thinks will be faster. :-)

Ajax
  • 2,465
  • 23
  • 20
8

Unless I'm missing something, you can name CTE's and subqueries just as easily.

I guess the main difference is readability (I find the CTE more readable because it defines your subquery up front rather than in the middle).

And if you need to do anything with recursion, you are going to have a bit of trouble doing that with a subquery ;)

AlexCuse
  • 18,008
  • 5
  • 42
  • 51
  • 1
    I'm not sure there is *any* non-aesthetic difference (though I expect that in certain situations there may be slight differences in execution plan). Care to enlighten me? – AlexCuse Feb 08 '10 at 20:06
  • 2
    You can *name* CTEs, but you can only *alias* subqueries. The difference is, you can reuse CTEs with multiple aliases (cf. @Michael Petito's example in his comment to casperOne). I don't know of any way to do that with subqueries. – kmote Jan 12 '16 at 18:39
6

Adding to others' answers, if you have one and the same subquery used several times, you can replace all these subqueries with one CTE. This allows you to reuse your code better.

A-K
  • 16,804
  • 8
  • 54
  • 74
4

One thing that you need to understand also is that in older versions of SQL Server (yes many people still need to support SQL Server 2000 databases), CTEs are not allowed and then the derived table is your best solution.

HLGEM
  • 94,695
  • 15
  • 113
  • 186
2

HINT: (MAXRECURSION n)

you can limit the number of recursion levels allowed for a specific statement by using the MAXRECURSION hint and a value between 0 and 32,767 in the OPTION clause

For example, you could try:

OPTION 
      (MAXRECURSION 150)

GO
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Basic_
  • 25
  • 7
2
  1. With a CTE, you can use recursion.

  2. With a CTE, you only need to write it once, but you can reference it in multiple places within the query. It can therefore allow you to avoid repeating yourself, and might also make the query easier to read and interpret (even in cases where the query only references it once).

  3. A CTE appears to provide metadata about itself to the query optimiser, such that if a CTE is referenced more than once in the same query (for example, if it joins to itself), the query optimiser could potentially use that metadata to improve the overall query execution plan (this does not appear to occur with subqueries).

So, in summary, if you want to use recursion, or you think it would make your code more presentable and easier to interpret, or you're using the same subquery more than once, use a CTE.

Chris Mack
  • 5,148
  • 2
  • 12
  • 29