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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:26:22+00:00 2026-06-04T00:26:22+00:00

I need to ask something that really confusing regarding joins What I think and

  • 0

I need to ask something that really confusing regarding joins

What I think and I am sure this used to work for me in past but seems like am missing some thing:

This query results in data from both tables where client_id is 1 – Working fine

/* gets data for one client_id*/

approach 1A

SELECT * 
FROM clients  LEFT JOIN client_modules 
                ON client_modules.client_id = clients.client_id
WHERE clients.client_id = 1;

Now, this query as what I think should return the same result because, I have used the sub-query in the join to filter the result first ie. get data from modules for client_id 1 already but, for some reason its giving other client_ids data in final result.

/* gets data for one client_id sub-query approach*/

approach 2A

SELECT * 
FROM clients LEFT JOIN (SELECT client_id, module_name 
                        FROM client_modules 
                        WHERE client_modules.client_id = 1) 
             AS client_moduless ON client_moduless.client_id = clients.client_id;



/* gets data for all client_ids */

approach 1B

SELECT * FROM clients
LEFT JOIN client_modules ON client_modules.client_id = clients.client_id;



/* gets data for all client_ids*/

approach 2B

SELECT * 
FROM clients LEFT JOIN (SELECT client_id, module_name 
                        FROM client_modules) AS client_moduless
              ON client_moduless.client_id = clients.client_id;

Questions:

1) Which approach is more efficient to use with large amount of data among xA and xB?

2) Why is the second approach 2A giving results from client_ids other then 1, although running the sub-query in join separately works fine

3) Will the sub-query in 2B execute for every record from parent if not using where clause?

4) what if I change the 1A query to

SELECT * FROM clients
JOIN client_modules ON client_modules.client_id = clients.client_id AND client_modules.client_id = 1

have just removed the where clause on client table and putted that in the join clause on child table is this efficient or the where clause?

Regards

  • 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-04T00:26:23+00:00Added an answer on June 4, 2026 at 12:26 am

    No of them. In my mind i think you should not be using a left join. You should use a join. Like vise:

    SELECT * FROM clients
    JOIN client_modules ON client_modules.client_id = clients.client_id
    WHERE clients.client_id = 1;
    

    That is more strait forward and you are limiting the LEFT JOIN in the where statement so it has the same effect.

    SELECT * 
    FROM clients LEFT JOIN (SELECT client_id, module_name 
                            FROM client_modules 
                            WHERE client_modules.client_id = 1) 
                 AS client_moduless ON client_moduless.client_id = clients.client_id;
    

    With this query you will return all the rows in the client table and for those that it can match the client_moduless.client_id = clients.client_id you will have a that table. But this is not a limiting JOIN it is a left join so that means that when no values is matched it return null. The subquery will be run for every row. To get the same effect you can do it like this:

    SELECT * 
    FROM clients JOIN (SELECT client_id, module_name 
                            FROM client_modules 
                            WHERE client_modules.client_id = 1) 
                 AS client_moduless ON client_moduless.client_id = clients.client_id;
    

    Now this will limit the clients table and you will just get the values that has a match in client_modules. But I cannot see a point of doing that. I would go with the strait forward join instead.

    It also depend on what you are interested in. If you are just in the columns from the clients table. Then you can do this:

    SELECT * 
    FROM clients
    WHERE EXISTS
    (
        SELECT 
            NULL
        FROM
            client_modules=1
            AND client_moduless.client_id = clients.client_id
    )
    

    So if you want all the columns from the clients table and the client_modules go with the join. Otherwise go with the exists

    EDIT

    I think this:

    SELECT * FROM clients
    JOIN client_modules ON client_modules.client_id = clients.client_id 
    AND client_modules.client_id = 1
    

    And this:

    SELECT * FROM clients
    JOIN client_modules ON client_modules.client_id = clients.client_id
    WHERE client_modules.client_id = 1
    

    is the same. This will most cretin result in the same query plan.

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

Sidebar

Related Questions

I know this sounds like a dumb question but I need to ask this.
This may be a stupid question to ask, but still I need to know
i really need help, i can't see something that is obvious, and usually then
This may sound really stupid but I have to ask cause I'm not finding
Don't ask me why but I need to do the following: string ClassName =
Somebody ask me this question today . What is the need of reference in
This is going to be a really dumb question, I just know it, but
not really a programming question, but I cant find anywhere else to ask it
This is the first time I ask something, so if there is something wrong
This may be way out in left field, crazy, but I just need to

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.