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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:08:02+00:00 2026-05-26T00:08:02+00:00

Say I have three tables, A, B and C. Conceptually A (optionally) has one

  • 0

Say I have three tables, A, B and C. Conceptually A (optionally) has one B, and B (always) has one C.

Table A:
a_id
... other stuff

Table B:
a_fk_id (foreign key to A.a_id, unique, primary, not null)
c_fk_id (foreign key to C.c_id, not null)
... other stuff

Table C:
c_id
... other stuff

I want to select All records from A as well as their associated records from B and C if present. However, the B and C data must only occur in the result if both B and C are present.

I feel like I want to do:

SELECT *
FROM
    A
    LEFT JOIN B on A.a_id=B.a_fk_id
    INNER JOIN C on B.c_fk_id=C.c_id

But Joins seem to be left associative (the first join happens before the second join), so this will not give records from A that don’t have an entry in C.

AFAICT I must use sub queries, something along the lines of:

SELECT *
FROM
    A
    LEFT JOIN (
        SELECT * FROM B INNER JOIN C ON B.c_fk_id=C.c_id
    ) as tmp ON A.id = tmp.a_fk_id

but once I have a couple of such relationships in a query (in reality I may have two or three nested), I’m worried both about code complexity and about the query optimizer.

Is there a way for me to specify the join order, other than this subquery method?

Thanks.

  • 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-26T00:08:02+00:00Added an answer on May 26, 2026 at 12:08 am

    In SQL Server you can do

    SELECT *
    FROM   a
           LEFT JOIN b
                     INNER JOIN c
                       ON b.c_fk_id = c.c_id
             ON a.id = b.a_fk_id  
    

    The position of the ON clause means that the LEFT JOIN on b logically happens last. As far as I know this is standard (claimed to be ANSI prescribed here) but I’m sure the downvotes will notify me if it doesn’t work in MySQL!

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

Sidebar

Related Questions

Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
Say I have three tables A, B, and C. A is the active table,
let's say I have three tables (MySQL), one for users, one for tags and
let's say i have three tables, each one relates to another, when i need
Say I have these three tables: Table: Baskets id | name 1 Sale 2
Let's say you have three tables named Item, Event, and Seat, constructed as such:
Let's say I have the following three strings: (section1){stuff could be any character, i
Say I have a query like this: one two three, if I replace with
Lets say I have one row with three columns - some buttons on left
I have three tables, say: (1) Name: Groups Columns: gid, groupname (2) Name: Profiles

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.