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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:44:05+00:00 2026-05-19T15:44:05+00:00

I really need help getting this query right. I can’t share actual table and

  • 0

I really need help getting this query right. I can’t share actual table and column names, but will try my best to layout the problem simply.

Assume the following tables. The tables and keys CANNOT be changed. Period. I don’t care if you think it’s a bad design, this question isn’t a design question, it’s on SQL syntax.

  • Table A – Primary key named id1
  • Table B – Contains two foreign keys, TableA.id1 and Foo.id2(ignore Foo, it doesn’t matter for this)
  • Table C – Contains two foreign keys, TableA.id1 and Foo.id2, additional interesting
    columns.

Constraints:

  1. The SQL gets a set of id1s passed in as an argument.
  2. It must return a list of Table C rows.
  3. It must only return Table C rows where a Table B row exists with a matching TableA.id1 and Foo.id2 – There ARE rows in Table C that don’t match Table B
  4. A row MUST be returned for every id1 passed in, even if no Table C row exists.

At first I tried a Left Outer Join from Table A to Table B then an Inner Join to Table C. That violates the 4th rule above, as the Inner Join drops out those rows.

Next I tried two Left Outer joins. This is closer, but has the side effect of including rows that match the Table A join to Table B, but don’t have a corresponding Table C entry, which isn’t what I want.

So, here’s what I came up with.

SELECT
  a.id1,
  c.*
FROM
  TableB b
INNER JOIN 
  TableC c USING (id1,id2)
RIGHT OUTER JOIN
  TableA a USING (id1)
WHERE
  a.id1 in (x,y,z)

I’m a bit wary of a Right Outer Join, as the documentation I’ve read says it can be replaced with a Left Outer, but it doesn’t appear so for this case. It also seems a bit rare, which is making other devs nervous, so I’m being cautious.

So, three questions in one.

  1. Is this correct?
  2. Did I use the Right Outer Join correctly?
  3. Is there a cleaner way to achieve the same thing?

EDIT: DB is MySQL

  • 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-19T15:44:05+00:00Added an answer on May 19, 2026 at 3:44 pm

    You can rewrite it as a LEFT OUTER JOIN by using parentheses. In pseudo-SQL change this:

    SELECT ...
    FROM b
    INNER JOIN c ON ...
    RIGHT OUTER JOIN a ON ...
    

    to this:

    SELECT ...
    FROM a
    LEFT OUTER JOIN (
        b INNER JOIN c ON ...
    ) ON ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been having a heck of a time getting this query right, so I'm
I really need help with interfaces in general... Any resources that you guys would
I really need your help in my issue very quickly and it's too close
I've some questions .. and I really need your help. I have an application.
I'm not a beginner at C# but I really need to increase my understanding,
Hey there. I would really appreciate your help in creating a query that would
I am outputting a query but need to specify the first row of the
Do we really need a server side architecture to create a RIA application? My
I have decided that I really need to get some flowcharts for reverse engineering
I am trying to understand difference between those two and really need a explanatory

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.