Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8217335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:25:32+00:00 2026-06-07T12:25:32+00:00

Bear with me, I’m new to intermediate. My question is – When should I

  • 0

Bear with me, I’m new to intermediate.

My question is – When should I use a CTE? How do I decide if I should use a CTE?

When should I use this:

;with cteTesting as
(
    select  *
    from    testing.first_table
)
select  *
from    testing.second_table s
        inner join cteTesting t
            on s.key = t.key

Over this:

select  *
from    testing.second_table s
        inner join
        (
            select  *
            from    testing.first_table
        ) t
        on s.key = t.key

And why? Is this just for code flow, code readability – or is there something more technical? Will one yield better execution plans under some circumstances?

edit: Just realized my example code example is very poor. I was trying to highlight that there are many cases where I can use a select in the from statement instead of a CTE – how do I decide which one I should be using?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:25:33+00:00Added an answer on June 7, 2026 at 12:25 pm

    For simple examples, it doesn’t make much difference. If you need to use the Recursive features to build up a hierarchy, then you haven’t much choice – you need to use a CTE.

    Another case where it probably doesn’t make much performance difference, but does for readability, is when you need to join the same subquery multiple times. If you’re using subqueries, you have to repeat the entire expression, whereas with CTEs, you just use the name twice:

    ;With NamedExpression as (
        select t1.ID,t2.ID as ID2,SUM(t3.Value) as Val
        from
          Table1 t1
            left join
          Table2 t2 on t1.id = t2.t1id
             inner join
          Table3 t3 on t3.col = t1.id or t3.col2 = t2.id
        group by
          t1.ID,t2.ID
    )
    select
        *
    from
        NamedExpression ne
            inner join
        NamedExpression ne2
            on
                ne.ID2 = ne2.ID
    

    It should also be noted that if you do the above as subqueries, and the expressions are particularly complex, it can sometimes take time for the reader/maintainer to verify that the two subqueries are in fact identical, and there’s not some subtle difference between the two


    Also, if you have an indenting style that says that subqueries should appear further to the right than their enclosing query, then expressions that build on other expressions can cause all of the code to shift to the right – whereas with CTEs, you stop and move back to the left in building each subexpression (CTE):

    ;WITH CTE1 AS (
        SELECT
        ...
    ), CTE2 as (
        SELECT
        ...
        FROM CTE1
    ), CTE3 as (
        SELECT
        ...
        FROM CTE2
    )
    select * from CTE3
    

    vs:

    select *
    from
       (
            select ...
            from
                 (
                     select ...
                     from
                         (
                              select ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please bear in mind that I'm totally new to Rails when answering this.My question
I'm new to Lua so please bear with this simple question :) I'm simply
Bear with me, I'm new to NUnit. I come from the land of Rails,
Please bear with me for this novice question. I am calling a RESTful web
Please bear with me, I am new to Uniscribe so I hope this isn't
Please bear with me, I come from .net and am new to the java
Please bear with me on this question. I'm looking to create a relatively large
Please bear with me if this question isn't well formulated. Not knowing is part
Please bear with me on this as I'm new to this. I have an
Bear with me, as I'm quite new to using regular expressions. I have regexp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.