Skip to content
Advertisement

Find users with most number of common pages they liked

I am trying to find pairs of users who liked the same pages and list the ones who have the most common page likes at the top.

For simplicity I am considering the following table schema

I am trying to find pairs of users with most number of common page likes ordered descending. E.g User1 and User2 have liked 3 pages in common.

I would to have the resulting set of the query to be

I am guessing it would need aggregation, join and aliases however I needed to rename a table twice using AS which did not work for me.

Any tip would be appreciated in MySQL, or SQL Server.

Advertisement

Answer

In SQL Server and MySQL 8+, you can use a CTE which JOINs the Likes and LikeDetail table, and then self-JOIN that where PageID is the same but UserID is not, and then grouping on the two userID values:

In versions of MySQL prior to 8.0, you need to repeat the CTE defintion twice in a JOIN to achieve the same result:

Note that we use < in the UserID comparison rather than != to avoid getting duplicate rows (e.g. for (UserID1, UserID2) = (1, 2) and (UserID1, UserID2) = (2, 1).

I’ve made a small demo on dbfiddle which demonstrate the queries.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement