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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:59:18+00:00 2026-05-19T01:59:18+00:00

Not quite sure how to ask or define this, but can’t figure it out.

  • 0

Not quite sure how to ask or define this, but can’t figure it out.

I have three tables like this:

persons             person_id, first_name, last_name
hobbies             hobby_id, name
persons_hobbies     person_id, hobby_id

I need to make two lists. Persons that have both hobby A and B, and persons that have hobby A but not B. How can I write these two queries? Can’t figure out how to do this with joining and all…

Say hobby A has id=3 and hobby B has id=7.

  • 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-19T01:59:18+00:00Added an answer on May 19, 2026 at 1:59 am

    Something like this should work:

    -- Persons that have both hobby A and B
    select p.first_name,p.last_name
    from persons p
    inner join persons_hobbies ph1 on ph1.person_id = p.person_id and ph1.hobby_id = 3
    inner join persons_hobbies ph2 on ph2.person_id = p.person_id and ph2.hobby_id = 7;
    
    -- Persons that have hobby A but not B 
    select p.first_name,p.last_name
    from persons p
    inner join persons_hobbies ph1 on ph1.person_id = p.person_id and ph1.hobby_id = 3
    left outer join persons_hobbies ph2 on ph2.person_id = p.person_id and ph2.hobby_id = 7
    where ph2.person_id is null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not quite sure if I can ask this question here or on SuperUser
Not sure if this is possible or not, but I figured I'd ask to
I'm not quite sure how to ask this question. What I'm trying to do
This may sound stupid, but I'm not quite sure how to get the W
I'm not quite sure how best ask this question, so please feel free to
I'm not quite sure how to approach this so thought it best to ask,
I'm not quite sure where to start with all of this, but im assuming
I want to try out heroku, but am not quite sure if I understand
I'm not quite sure if this is possible so that's why I ask you
So I have a question I'm honestly not quite sure how to ask. Essentially

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.