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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:36:00+00:00 2026-06-10T13:36:00+00:00

I am wishing to compare two sets of data using SQL wherever possible. Please

  • 0

I am wishing to compare two sets of data using SQL wherever possible. Please consider the following data structures:

tbl_users:
+---------+----------+
| user_id |   avatar |
+---------+----------+
|    1    | test.jpg |
|    2    | 1234.jpg |
+---------+----------+

tbl_shortlists:
+------+--------+
|  id  |  owner |
+------+--------+
|   1  |    1   |
|   2  |    2   |
+------+--------+

tbl_shortlist_items:
+---------+--------------+
| item_id | shortlist_id |
+---------+--------------+
|    1    |       1      |
|    2    |       1      |
|    3    |       1      |
|    1    |       2      |
|    2    |       2      |
+---------+--------------+   

I wish to select the tbl_users.user_id and tbl_users.avatar where the shortlist owned by any user contains two or more of the same item_id as the current owner. So, let’s assume I am looking up the user_id 1, with the above data structure. Since the Shortlist with the ID shares two items with shortlist_id 1 (i.e. user 1’s shortlist), I would like to return user_id = 2 and avatar = 1234.jpg.

I am at a loss as to how to do this in pure SQL. I was thinking that it might be possible to use IN(), but I don’t know if that would work.

Here’s some pseudo-code to hopefully explain a little better what I would like:

Select user_id and avatar
for each shortlist that contains two or more item_ids that are in the shortlist
that is owned by user_id = 1.

  • 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-10T13:36:01+00:00Added an answer on June 10, 2026 at 1:36 pm

    As always with SQL it helps to break it down into small sections. i.e. the first thing you need is all the items in short lists that the current user owns:

    SELECT  Item_ID
    FROM    tbl_ShortList_Items
            INNER JOIN tbl_ShortLists
                ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
    WHERE   Owner = 1 -- CURRENT USER
    

    Next you need all users who have short lists with the same items in. For explanatory purposes I will build up as an IN statement, but an INNER JOIN may perform better.

    SELECT  Owner
    FROM    tbl_ShortList_Items
            INNER JOIN tbl_ShortLists
                ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
    WHERE   Item_ID IN 
            -- RESULTS FROM LAST QUERY START
            (   SELECT  Item_ID
                FROM    tbl_ShortList_Items
                        INNER JOIN tbl_ShortLists
                            ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
                WHERE   Owner = 1 -- CURRENT USER
            )   
            -- RESULTS FROM LAST QUERY END
    AND     Owner != 1 -- CURRENT USER
    

    You then need to limit this to those that have 2 or more, by adding a GROUP BY and HAVING

    SELECT  Owner
    FROM    tbl_ShortList_Items
            INNER JOIN tbl_ShortLists
                ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
    WHERE   Item_ID IN 
            -- RESULTS FROM LAST QUERY START
            (   SELECT  Item_ID
                FROM    tbl_ShortList_Items
                        INNER JOIN tbl_ShortLists
                            ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
                WHERE   Owner = 1 -- CURRENT USER
            )   
            -- RESULTS FROM LAST QUERY END
    AND     Owner != 1 -- CURRENT USER
    GROUP BY Owner
    HAVING  COUNT(DISTINCT tbl_ShortList_Items.Item_ID) > 1
    

    You then need to use tbl_users to get the avatar for these owners

    SELECT  User_ID, Avatar
    FROM    tbl_Users
    WHERE   User_ID IN 
            -- RESULTS FROM LAST QUERY START
            (   SELECT  Owner
                FROM    tbl_ShortList_Items
                        INNER JOIN tbl_ShortLists
                            ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
                WHERE   Item_ID IN 
                        (   SELECT  Item_ID
                            FROM    tbl_ShortList_Items
                                    INNER JOIN tbl_ShortLists
                                        ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
                            WHERE   Owner = 1 -- CURRENT USER
                        )
                AND     Owner != 1 -- CURRENT USER
                GROUP BY Owner
                HAVING  COUNT(DISTINCT tbl_ShortList_Items.Item_ID) > 1
            )
            -- RESULTS FROM LAST QUERY END
    

    As I said, I think rearranging this to JOINs will be optimised better, but have not tested this theory.

    SELECT  User_ID, Avatar
    FROM    tbl_Users
            INNER JOIN
            (   SELECT  Owner
                FROM    tbl_ShortList_Items
                        INNER JOIN tbl_ShortLists
                            ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
                        INNER JOIN
                        (   SELECT  Item_ID
                            FROM    tbl_ShortList_Items
                                    INNER JOIN tbl_ShortLists
                                        ON tbl_ShortLists.ID = tbl_ShortList_Items.ShortList_ID
                            WHERE   Owner = 1 -- CURRENT USER
                        ) CurrentUserItems
                            ON CurrentUserItems.Item_ID = tbl_ShortList_Items.Item_ID
                WHERE   Owner != 1
                GROUP BY Owner
                HAVING  COUNT(DISTINCT tbl_ShortList_Items.Item_ID) > 1
            ) MatchUsers
                ON MatchUsers.Owner = tbl_Users.User_ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wishing I could do something like the following in SQl Server 2005 (which
Possible Duplicates: What is SQL injection? XKCD SQL injection - please explain I own
Is it possible to update a different ODBC database from withing a SQL Server
Im currently wishing to transfer data from a remote server by capturing printed data
I got the following code, wishing to wrap a group of strings nicely in
I have a Silverlight 2 application that validates data OnTabSelectionChanged. Immediately I began wishing
I am wishing to provide a visualisation of groups of data on a website,
I'm using Selenium, and I'm wishing to capture all cookie packets sent during a
Is it possible to get records between a Timespan from a DataTable using the
in my iphone app, I'm wishing to link a contact to a data-piece in

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.