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 9299189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:30:05+00:00 2026-06-18T22:30:05+00:00

Is there a more efficient way to write this query without subqueries? Table Question_Group

  • 0

Is there a more efficient way to write this query without subqueries?

Table Question_Group:

Question_Group_ID int, 
Question_Group_Name nvarchar, 
Question_Group_Indent int,  // 1=parent,2=child,3=grandchild
Question_Group_Order int, 
Question_Parent_ID int

My query:

SELECT 
   parent.Question_Group_Name, parent.Question_Group_Order, 
   L2Child.Question_Group_Name, L2Child.Question_Group_Order 
FROM
   (SELECT 
       Question_Group_ID, Question_Group_Name, Question_Group_Indent, 
       Question_Group_Order, Question_Parent_ID
    FROM
       Question_Groups 
    WHERE
       Question_Group_Indent = 1) parent
LEFT JOIN
   (SELECT
       Question_Group_ID, Question_Group_Name, Question_Group_Indent, 
       Question_Group_Order, Question_Parent_ID
    FROM 
       Question_Groups 
    WHERE 
       Question_Group_Indent = 2) L2Child ON parent.Question_Group_ID = L2Child.Question_Parent_ID
ORDER BY 
    parent.question_group_Order

Results:

Pre-Site            1   NULL                                    NULL
Agency Information  2   Contacts                            1
Agency Information  2   Contracting Services                    2
Agency Information  2   Start-Up Agency                         3
Agency Information  2   Hiring                                  4
Agency Information  2   Budgeted and Actual Sworn Force         5
Agency Information  2   CP Questions                            6
Hiring Grants   3       Per Hiring Grant Questions          1
Non-Hiring Grants   4   Per Non-Hiring Grant Questions          1
  • 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-18T22:30:07+00:00Added an answer on June 18, 2026 at 10:30 pm

    I’m not sure if it is more efficient but you can write it with CTEs to make it more readable.

    WITH parent AS (SELECT Question_Group_ID, Question_Group_Name, Question_Group_Indent, 
                        Question_Group_Order, Question_Parent_ID
                    FROM Question_Groups 
                    WHERE Question_Group_Indent=1),
         L2Child AS (SELECT Question_Group_ID, Question_Group_Name, Question_Group_Indent, 
                        Question_Group_Order, Question_Parent_ID
                    FROM Question_Groups 
                    WHERE Question_Group_Indent=2)
    SELECT parent.Question_Group_Name, parent.Question_Group_Order, 
        L2Child.Question_Group_Name, L2Child.Question_Group_Order 
    FROM parent
    LEFT JOIN L2Child
        ON parent.Question_Group_ID = L2Child.Question_Parent_ID
    ORDER BY parent.question_group_Order
    

    The other option is to re-write it this way

    SELECT parent.Question_Group_Name, parent.Question_Group_Order, 
        L2Child.Question_Group_Name, L2Child.Question_Group_Order 
    FROM Question_Groups AS parent
    LEFT JOIN Question_Groups AS L2Child
        ON parent.Question_Group_ID = L2Child.Question_Parent_ID
        AND L2Child.Question_Group_Indent=2
    WHERE parent.Question_Group_Indent=1
        ORDER BY parent.question_group_Order
    

    Read this to see why I put the “left” table condition in the WHERE clause and the “right” table condition in the ON clause.

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

Sidebar

Related Questions

Is there a better (more efficient) way to write this query? It seems like
Is there a better, more efficient way to write this code? It's a make
Is there a more efficient/elegant way to write this bit of code: $arr1 =
Is there a better / more efficient / shorter way to write this SQL
Is there a more efficient way to write this code? Or with less code?
Is there a more efficient way to list files from a bucket in Amazon
Is there are more efficient way than the following for selecting the third parent?
If I have a standard for loop is there a more efficient way to
Is there a more efficent way of doing this in terms of memory usage
Is there any more efficient,cleaner and shorter form for following: if($x==null or $y==null or

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.