Exercise 23:1892.Page Recommendations II

1.Description

Table: Friendship

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user1_id      | int     |
| user2_id      | int     |
+---------------+---------+
(user1_id, user2_id) is the primary key for this table.
Each row of this table indicates that the users user1_id and user2_id are friends.

Table: Likes

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| user_id     | int     |
| page_id     | int     |
+-------------+---------+
(user_id, page_id) is the primary key for this table.
Each row of this table indicates that user_id likes page_id.

You are implementing a page recommendation system for a social media website. Your system will recommend a page to user_id if the page is liked by at least one friend of user_id and is not liked by user_id.

Write an SQL query to find all the possible page recommendations for every user. Each recommendation should appear as a row in the result table with these columns:

  • user_id: The ID of the user that your system is making the recommendation to.

  • page_id: The ID of the page that will be recommended to user_id.

  • friends_likes: The number of the friends of user_id that like page_id.

Return result table in any order.

The query result format is in the following example:

2.Create Table and insert into values

User 1 is friends with users 2, 3, 4, and 6:

user1_id = 1,user2_id =(2,3,4) and user2_id=1, user1_id =6 , it means for user1, he's friend =(2,3,4,6).

SO the first step, we have to do data processing on Friendship table.

Last updated