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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:58:18+00:00 2026-06-16T11:58:18+00:00

Pretty simple question, just cannot wrap my head around it. Example: 3 tables. Owner

  • 0

Pretty simple question, just cannot wrap my head around it.
Example:
3 tables. Owner <-> OwnerAnimal <-> Animal

Animal can have multiple owners, and owner can have multiple animals.
Now, given a specific owner, find other owners that have animals in common with the given owner.

What I think is that we need to do join on the same table multiple times, like this:

select distinct  
o2.Owner_Id,   
o2.Name  
from Owner o  
left join OwnerAnimal  oa  
on o.Owner_Id = oa.Owner_Id   
left join OwnerAnimal oa2   
on oa.Animal_id = oa2.Animal_Id   
left join Owner o2   
on. oa2.Owner_Id = o2.Animal_Id   
Where o.Owner_Id = 100 and o2.Owner_Id <> 100 --To exclude current owner from the list

But I’m not sure if this is a right approach.

  • 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-16T11:58:19+00:00Added an answer on June 16, 2026 at 11:58 am

    If you want any overlap of animals, the following is the way that I think of it:

    select distinct ao.owner
    from AnimalOwners ao
    where ao.animal in (select animal from AnimalOwners ao1 and ao1.owner = 100) and
          ao.owner <> 100
    

    You can rewrite this as a join, but the in seems to make more sense.

    If you want all animals to be the same, then you need to do a join.

    with a as (select distinct animal from AnimalOwners where ao.owner = 100)
    select ao.owner
    from AnimalOwners ao left outer join
         a
         on ao.animal = a.animal
    where ao.owner <> 100
    group by ao.owner
    having count(disinct ao.animal) = (select count(*) from a) and
           count(*) = count(a.animal)
    

    The idea is to do a set comparison using the having clause. The first subclause guarantees that the number of animals for the second owner is the same as the number for 100. The second guarantees that there is no animal owned by the second owner that is not owned by the original owner. The left outer join keeps all animals.

    The use of the distinct keyword is for the situations where an animal could appear twice for an owner. It is unclear whether this is allowed.

    If you want owners that own the same animals as the original owner, but could own others, then change the left outer join to a join in the previous query.

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

Sidebar

Related Questions

I have a question that's so simple I cannot believe I can't answer it
I have a pretty simple question. Im just learning Maps and multimaps and want
Pretty simple question this time around. I have an application that communicates with another
i have a pretty simple question and i'm just not sure why the code
pretty simple question, can't quite fig. it out. I have 2 js array's that
Pretty simple question. I'm quite certain I have the Class, method, codebehind, etc linked
Pretty simple question. I have a few ASP RequiredFieldValdators checking some text boxes. Out
I have a pretty simple question which perhaps someone familiar with Server/Client design &
i have pretty simple simple question (i hope so). How do i change the
hopefully a pretty simple question this time. I have a Select method in a

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.