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

  • SEARCH
  • Home
  • 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 8742523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:31:08+00:00 2026-06-13T11:31:08+00:00

I have a query that sequentially joins 6 tables from their original data sources.

  • 0

I have a query that sequentially joins 6 tables from their original data sources. Nested, it’s a mess:

SELECT
FROM
(
    SELECT
    FROM
    (
        SELECT
        FROM
        (. . .)
        INNER JOIN
    )
    INNER JOIN
)

I switched to CTE definitions, and each definition is one join on a previous definition, with the final query at the end providing the result:

WITH
Table1 (field1, field2) AS
(
    SELECT
    FROM 
    INNER JOIN
),

Table2 (field2, field3) AS
(
    SELECT
    FROM Table1
    INNER JOIN
), . . . 

SELECT 
FROM Table 6

This is a lot more readable, and dependencies flow downward in logical order. However, this doesn’t seem like the intended use of CTEs (and also why I’m not using Views), since each definition is really only referenced once in order.

Is there any guidance out there on how to construct sequentially nested joins like this that is both readable and logical in structure?

  • 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-13T11:31:09+00:00Added an answer on June 13, 2026 at 11:31 am

    I don’t think there is anything wrong in utilizing CTE to create temporary views.

    1. In a larger shop, there are roles defined that separates the
      responsibility of DBAs versus developers. The CREATE statement, in general, will be the victim of this bureaucracy. Hence, no view. CTE is a very good
      compromise.
    2. If the views are not really reusable anyway, keeping it with the SQL makes it more readable.
    3. CTE is a lot more readable and intuitive than sub-queries (even with
      just one level). If your subqueries are not correlated, I would just suggesting
      converting all of your sub-queries to CTE.
    4. Recursion is the “killer” app for CTE, but it doesn’t mean that you shouldn’t use CTE, otherwise.

    The only con that I can think of is that (depending on your Database Engine) it might confuse or prevent the optimizer from doing what it’s suppose to do. Optimizers are smart enough to rewrite subqueries for you.

    Now, let us discuss abuse of CTE, be careful that you don’t substitute your application developer knowledge for Database Engine optimization. There are a lot of smart developers (smarter that us) that designed this software, just write the query without cte or subqueries as much as possible and let the DB do the work. For example, I often see developers DISTINCT/WHERE every key in a subquery before doing their join. You may think your doing the right thing, but you’re not.

    With regards to your question, most people intend to solve problems and not discuss something theoretical. Hence, you get people scratching their heads on what you are after. I wouldn’t say you didn’t imply that in your text, but perhaps be more forceful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that basically combines tables of actions and selects from them
I have a query that looks like this SELECT * from myTable WHERE Date
I have a query that successfully grabs the unique products from my products table
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,
I have a query that is pulling users who liked a specific object from
I am using EmguCV to query frames sequentially from a capture that I defined
I have a query that uses union to join two sub queries e.g. SELECT
I am using in C# MYsql .I have query that works if I run
I have a query that creates a result set like this: Rank Name 1
I have this query that returns the results by ordering it by focus.name ASC.

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.