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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:12:29+00:00 2026-05-22T21:12:29+00:00

Complex for me as I am newbie in SQL. I have three tables –

  • 0

Complex for me as I am newbie in SQL.

I have three tables – Peoples, Interests and Peoples_Interests (many-to-many) – that are connected in the following way:

People has many Interests through Peoples_Interests
Interest has many Peoples through Peoples_Interests

I need to propose suggestions to Peoples with most similar to them Peoples, which is based on amount of similar Interests. So for example:

I am interested in baseball, football and volley. I should get suggestion with another user that have as many similar interests as possible. People with 3/3 occurrences should be what I need if they exist (if not – 2/3 and such).

So I need a query that output will consist of sorted by interests similarity Peoples.

UPDATE:
Db structure:

Interests
id
name – string

Peoples
id
email

Peoples_Interests
interests_id
peoples_id

Thank you.

  • 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-22T21:12:29+00:00Added an answer on May 22, 2026 at 9:12 pm

    Something like this.

    Select people.id, people.name, count(interest.id)
    from people
    left join people_interests on people.id = people_interests.peopleid 
    left join interests on people_interests.interestid = interests.interest.id
    where interests.id in (select id from interests where interests.peopleid = @inputuserid)
    group by people.id, people.name
    order by count(interest.id)
    

    In english (which may or may not make it clearer.)

    • Select the person’s name and the number of interests they share
    • From the people table
    • Join the interests table such that that table
    • Is only the interests of the person we are trying to match.
    • (group by people
    • and order by the number of interests that match.)

    Updated without the sub query but less clear

    Select people.id, people.name, count(interest.id)
    from people
    left join people_interests on people.id = people_interests.peopleid 
    left join interests on people_interests.interestid = interests.interest.id
    inner join interest i2 on (interests.id = i2.id and i2.people_id = @inputuserid)
    group by people.id, people.name
    order by count(interest.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex set of bindings that include many private bindings to solve
I have a complex .NET Remoting server app that provides a couple of services.
I have a complex command that I'd like to make a shell/bash script of.
I have a BackgroundWorker that runs a lengthy and semi-complex task. In this BackgroundWorker,
I have a very complex MySQL query that includes use of the same subquery
I have couple of complex SQL and need to convert it into an ActiveRecord
I have a complex query that's executed on every page and whose results rarely
working on a highly complex WordPress theme that features many different custom post types
I have a rather complex query that pretty much mimics a test query I
I have a complex query with group by and order by clause and I

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.