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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:56:13+00:00 2026-06-07T17:56:13+00:00

I have one table, assume that there are 2 users, user A and user

  • 0

I have one table, assume that there are 2 users, user A and user B. User A and user B have some items; some of these items are the same but some of them are different. I want to count how many items they have common.

For example, user A and user B have 2 common items. user A and user C have only one common item. User B and user C have 2 common items, etc.

How can I do this in SQL?

 Users  Items
  A      C1
  A      C2
  A      C3
  B      C2
  B      C3
  B      C4
  B      C5
  C      C1
  C      C4
  C      C5
  C      C6

This is what I’ve tried so far:

select distinct users, count(items) over (partition by items) 
  from table1
  • 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-07T17:56:15+00:00Added an answer on June 7, 2026 at 5:56 pm

    Okay, this can be done with a self-join:

    select a.username as user1
         , b.username as user2
         , count(distinct a.item) as similar
      from user_items a
      join user_items b
        on a.item = b.item
     where a.username <> b.username
       and a.username < b.username
     group by a.username, b.username
    

    Here’s a SQL Fiddle to demonstrate.

    The join is done on item as this is the column that you want to know if it’s identical. I’ve excluded where the user is the same, but you can include it if you want.

    The most important part is a.username < b.username. If you don’t use this then you get the two identical rows with the username’s reversed. For instance you get A|B|2 and B|A|2.

    I’ve also used distinct item. If you wanted the number of items rather than the number of distinct simply remove this keyword.

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

Sidebar

Related Questions

Assume that I have one big table with three columns: user_name, user_property, value_of_property. Lat's
I have one table that has two fields - ID1 and ID2 ID1 can
I have one table that has sales records and another table that has additional
I have one table containing user sessions and another to indicate violations in the
I have one table, e.g. pricerules, that have stored special prices by article for
Assume that I have a table user_count defined as follows: id primary key, auto
Here is a simple example: Assume I have a customer table that has a
I have one table which contains events and dates, and another which contains the
I have one table having ID and other attributes. How can I get list
I have one table, t1, which has fileds called userid, week and year fields.

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.