Page Recommendations Problem


Description

LeetCode Problem 1264.

Table: Friendship

1
2
3
4
5
6
7
8
+---------------+---------+
| 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 there is a friendship relation between user1_id and user2_id.

Table: Likes

1
2
3
4
5
6
7
8
+-------------+---------+
| 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.

Write an SQL query to recommend pages to the user with user_id = 1 using the pages that your friends liked. It should not recommend pages you already liked.

Return result table in any order without duplicates.

The query result format is in the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1        | 2        |
| 1        | 3        |
| 1        | 4        |
| 2        | 3        |
| 2        | 4        |
| 2        | 5        |
| 6        | 1        |
+----------+----------+
 
Likes table:
+---------+---------+
| user_id | page_id |
+---------+---------+
| 1       | 88      |
| 2       | 23      |
| 3       | 24      |
| 4       | 56      |
| 5       | 11      |
| 6       | 33      |
| 2       | 77      |
| 3       | 77      |
| 6       | 88      |
+---------+---------+

Result table:
+------------------+
| recommended_page |
+------------------+
| 23               |
| 24               |
| 56               |
| 33               |
| 77               |
+------------------+
User one is friend with users 2, 3, 4 and 6.
Suggested pages are 23 from user 2, 24 from user 3, 56 from user 3 and 33 from user 6.
Page 77 is suggested from both user 2 and user 3.
Page 88 is not suggested because user 1 already likes it.


MySQL Solution

1
2
3
4
5
6
7
8
9
10
11
select distinct l.page_id as recommended_page
from Likes l
left join Friendship f1
on f1.user2_id = l.user_id
left join Friendship f2
on f2.user1_id = l.user_id
where (f1.user1_id = 1 or f2.user2_id = 1) and 
    l.page_id not in 
        (select page_id 
         from Likes 
         where user_id = 1)




Related Posts

Page Recommendations Problem

LeetCode 1264. Write an SQL query to recommend pages to...

Find The Start And End Number Of Continuous Ranges Problem

LeetCode 1285. Write an SQL query to find the start...

All People Report To The Given Manager Problem

LeetCode 1270. Write an SQL query to find employee_id of...

Weather Type In Each Country Problem

LeetCode 1294. Write an SQL query to find the type...

Students And Examinations Problem

LeetCode 1280. Write an SQL query to find the number...

Average Selling Price Problem

LeetCode 1251. Write an SQL query to find the average...