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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:13:41+00:00 2026-06-17T09:13:41+00:00

I have a table Foo FOO ——- id name BAR ——- id name FOO_BAR_XREF

  • 0

I have a table Foo

FOO
-------
id
name

BAR
-------
id
name

FOO_BAR_XREF
-------
foo_id
bar_id

I need to select all instances of FOO where foo.id has a record in foo_bar_xref with a bar_id of 1 and exclude records from foo where foo.id has a record n foo_bar_xref with a bar_id of 2

foo -> foo_bar_xref is one to many *emphasized text*foo_bar_xref may contain multiple bar_id’s per foo_id

Is this possible with only joins or do I need to use not exists statements in the where clause?

so far I have

select f.name from FOO f
inner join FOO_BAR_XREF fb_1 on fb_1.foo_id = f.id
inner join FOO_BAR_XREF fb_2 on fb_2.foo_id = f.id

where fb_1.bar_id = 1 and fb_2.bar_id <> 2
group by f.name -- remove dupes - running on sql server and is paged with sql servers hackish over keyword where distinct doesn't work so well.

thats not filtering out the foo’s that have a bar of 2

THIS seems to work but I’m not sure it’s the most efficient

select f.name from FOO f
inner join FOO_BAR_XREF fb_1 on fb_1.foo_id = f.id and fb_1.bar_id = 1
left outer join FOO_BAR_XREF fb_2 on fb_2.foo_id = f.id and fb_2.bar_id = 2

where fb_2.bar_id is null
group by f.name -- remove dupes
  • 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-17T09:13:42+00:00Added an answer on June 17, 2026 at 9:13 am

    To reiterate my comment, I think OP has best solution. I have always preffered using left joins for these scenarios.

    select f.name 
    from FOO f 
    inner join FOO_BAR_XREF fb_1 
      on fb_1.foo_id = f.id and fb_1.bar_id = 1 
    left outer join FOO_BAR_XREF fb_2 
      on fb_2.foo_id = f.id 
      and fb_2.bar_id = 2
    where fb_2.bar_id is null
    group by f.name
    

    And the SQL Fiddle.

    BTW, I think this is the most efficient solution in regards to table scans. Check your execution plans and see what you have going on.

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

Sidebar

Related Questions

Let's say I have SELECT name FROM table which gives me something like foo
I have a table Foo that has a polymorphic belongs_to association called bar .
Let's say I have a table Foo . This table has the columns ID
Suppose we have two tables Foo and Bar. I have an association table Foo_Bar
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I have a MySQL table like so; foo bar 1 21 23 17 31
I have a tables Foo and Bar . Foo has one Bar . When
I have a table foo and a table bar, where each foo might have
I have two tables Table FOO FooUniqueID| Year| Name| Worth| --------------------------- 1 2008 Bob
I have a table foo with (among 20 others) columns bar , baz and

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.