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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:23:57+00:00 2026-06-14T11:23:57+00:00

Table structure: uid : integer answer_id : integer I need to run a query

  • 0

Table structure:

uid       : integer
answer_id : integer

I need to run a query which will show me which uid’s have the same answers as other uid’s. So for example, here’s some test data:

answer_id   uid
1           555
4           555
7           555
10          555
1           123
5           123
7           123
10          123

So we can see from this data that they each answered 3/4 of the questions the same way.

I’m struggling with how to write a query which would show me which uid’s match 3/4 or 4/4 of the same answers. Basically I’m trying to find users with 75% (3/4) or greater (4/4) similar answers.

This is part of a Ruby on Rails application, so I have the models all built [User, UserAnswers etc..] but I’m assuming this will just be a SQL query, not necessarily a part of ActiveRecord

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

    This query show the number of answers every user has in common with each other:

    declare @uid int
    
    select
      ans1.uid as user1,
      ans2.uid as user2,
      count(*)
    from 
      ans ans1 inner join ans ans2
      on ans1.answer_id = ans2.answer_id
         and ans1.uid <> ans2.uid
    where uid = @uid
    group by user1, user2
    having count(*)>0
    

    This also shows the number of questions each user has answered:

     select
      ans1.uid as user1,
      ans2.uid as user2,
      count(distinct ans1.answer_id) as total1,
      count(distinct ans2.answer_id) as total2,
      sum(case when ans1.answer_id = ans2.answer_id then 1 else 0 end) as common
    from 
      ans ans1 inner join ans ans2 on ans1.uid <> ans2.uid
    group by user1, user2
    having count(*)>0
    

    (this second query can be very slow)

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

Sidebar

Related Questions

I have this table structure: CREATE TABLE users ( uid bigint NOT NULL, first_name
I have got a mysql table structure: uid company value Description: uid -> unical
I have a table with the following structure: itemId | direction | uid |
I have a table with such structure: uid sid all integers. This table simulates
I have a table with the following structure uid sid eid Key value 1
I have following table structure in Mysql DB Table: Mytable -------------------------------- | uid |
I have MySQL table with structure: | uid | itemid | ------------------- | 1001
I have following table structure: uid | product_code --------------------- 00001 | 'tb' 00001 |
I have table with following structure id int auto increment, primary key uid int
I have the following table structure: uid | ... | created | ... int(10)

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.