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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:43:02+00:00 2026-05-21T06:43:02+00:00

Imagine you have two tables, with a one to many relationship. For this example,

  • 0

Imagine you have two tables, with a one to many relationship.

For this example, I will suggest that there are two tables: Person, and Homes.
The person table holds a persons name, and gives them an ID. The homes table, holds the association of homes to a person. PID joins to “Person.ID”

And, in this tiny DB, a person can have no homes, or many homes.

enter image description here

I hope I drew that right.

How do I write a select, that returns everyone with every specified house type?

Let’s say these are valid “Types” in the homes table:

Cottage, Main, Mansion, Spaceport.

I want to return everyone, in the Person table, who has a spaceport and a Cottage.

The best I could come up with was this:

SELECT DISTINCT( p.name ) AS name
FROM person p
    INNER JOIN homes h ON h.pid = p.id
WHERE 'spaceport' in (
    SELECT DISTINCT( type ) AS type
    FROM homes
    WHERE pid = p.id
)
AND 'cottage' in (
    SELECT DISTINCT( type ) AS type
    FROM homes
    WHERE pid = p.id
)

When I wrote that, it works, but I’m pretty sure there has to be a better way.

  • 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-21T06:43:03+00:00Added an answer on May 21, 2026 at 6:43 am

    The HAVING clause here will guarantee that the persons returned have both types, not just one or the other.

    SELECT p.name
        FROM person p
            INNER JOIN homes h
                ON p.id = h.pid
                    AND h.type IN ('spaceport', 'cottage')
        GROUP BY p.name
        HAVING COUNT(DISTINCT h.type) = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine a situation, I have PC with two lan cards, one is connected to
Example: I have two tables in my database called classA and classB , and
I have two tables of concern here: users and race_weeks. User has many race_weeks,
imagine there are two tables. Order +----------------+ | ID | | Name | +----------------+
I have two types of 'owner', each of which can have one 'preference'. This
I have two classes. One is called Employee and the other EmployeeDetails that has
Imagine I have an function which goes through one million/billion strings and checks smth
Imagine I have a process that starts several child processes. The parent needs to
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
When you have a many-to-many relationship ( related_name , not through ) and you

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.