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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:17:58+00:00 2026-05-25T14:17:58+00:00

I have the following SQL query that performs a subquery and joins two tables

  • 0

I have the following SQL query that performs a subquery and joins two tables which is then joined with a primary table:

SELECT a.id, cgso.sf_guard_user_id as cgso, cgal.sf_guard_user_id as cgal
FROM table_a a
JOIN (  SELECT cgso.sf_guard_user_id, cgso.speciality_id
        FROM table_c g
        JOIN  table_b as  cgso
            ON g.user_id = cgso.sf_guard_user_id and g.group_id = 2) as cgso
    ON a.speciality_id = cgso.speciality_id
JOIN (  SELECT cgal.sf_guard_user_id, cgal.speciality_id
        FROM table_c g
        JOIN  table_b as  cgal
            ON g.user_id = cgal.sf_guard_user_id and g.group_id = 1) as cgal
    ON a.speciality_id = cgal.speciality_id

The output of the query is:

id | cgso | cgal
----------------
 1 |  2   | 54

The output is fine and as intended, however is there a more efficient way to get the same output? Any tips or advice would be much appreciated.

Thank You

  • 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-05-25T14:17:58+00:00Added an answer on May 25, 2026 at 2:17 pm

    You should be able to just simplify the joins…

    SELECT a.id, cgso.sf_guard_user_id as cgso, cgal.sf_guard_user_id as cgal
    FROM table_a a
    INNER JOIN table_c g2 ON g1.group_id = 2
    INNER JOIN table_b cgso ON g2.user_id = cgso.sf_guard_user_id  AND cgso.specialty_id = a.specialty_id
    INNER JOIN table_c g1 ON g1.user_id = 1
    INNER JOIN table_b cgal ON g1.user_id = cgal.sf_guard_user_id AND cgal.specialty_id = a.specialty_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL query that selects some results from my table: select avg(c3),
I have a fairly complex query in SQL that performs a count across two
I have the following sql query that fetches images from an establishment table. The
I have the following SQL query that performs horribly due to the select count(1)
I have the following Sql Query that returns the type of results that I
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have the following Linq to SQL query, in which I'm trying to do
I have the following which works in SQL Query Analyzer . select oh.* from
I have the following query in iSeries SQL which I output to a file.
I have a sql query that performs the type of select I'm after: select

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.