🙌
Hands-On Leetcode Database Solution with Hive SQL
(Online Course)Learning SQL with meMy BlogAbout meFollow me on FBFollow my IG
  • INTRODUCTION
    • About This Book
    • 作者簡介
  • Exercise 1:185.Department Top Three Salaries
    • Hive Solution
  • Exercise 2:262. Trips and Users
    • Hive Solution
  • Exercise 3:569.Median Employee Salary
    • Hive Solution
  • Exercise 4:571.Find Median Given Frequency of Numbers
    • Hive Solution
  • Exercise 5:579.Find Cumulative Salary Of An Employee Problem
    • Hive Solution
  • Exercise 6:601.Human Traffic of Stadium
    • Hive Solution
  • Exercise 7:615.Average Salary: Departments VS Company
    • Hive Solution
  • Exercise 8:618.Students Report By Geography
    • Hive Solution
  • Exercise 9:1097.Game Play Analysis V
    • Hive Solution
  • Exercise 10:1127.User Purchase Platform
    • Hive Solution
  • Exercise 11:1159.Market Analysis II
    • Hive Solution
  • Exercise 12:1194.Tournament Winners
    • Hive Solution
  • Exercise 13:1225.Report Contiguous Dates
    • Hive Solution
  • Exercise 14:1336.The number of transactions per visit
    • Hive Solution
  • Exercise 15:1369.Get The Second Most Recent Activity
    • Hive Solution
  • Exercise 16:1384.Total Sales Amount by Year
    • Hive Solution
  • Exercise 17:1412.Find the Quiet Students in All Exams
    • Hive Solution
  • Exercise 18:1479. Sales by Day of the Week
    • Hive Solution
  • Exercise 19:1635. Hopper Company Queries I
    • Hive Solution
  • Exercise 20:1645. Hopper Company Queries II
    • Hive Solution
  • Exercise21 :1651. Hopper Company Queries III
    • Hive Solution
  • Exercise 22:1767.Find the Subtasks That Did Not Execute
    • Hive Solution
  • Exercise 23:1892.Page Recommendations II
    • Hive Solution
  • Exercise 24:1917.Leetcodify Friends Recommendations
    • Hive Solution
  • Exercise 25:1919.Leetcodify Similar Friends
    • Hive Solution
  • Exercise 26:1972.First and Last Call On the Same Day
    • Hive Solution
  • Exercise 27:2004.The Number of Seniors and Juniors to Join the Company
    • Hive Solution
  • Exercise 28:2010.The Number of Seniors and Juniors to Join the Company II
    • Hive Solution
Powered by GitBook
On this page
  • 1.Description
  • 2.Create Table and insert into values

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:

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:
+---------+---------+---------------+
| user_id | page_id | friends_likes |
+---------+---------+---------------+
| 1       | 77      | 2             |
| 1       | 23      | 1             |
| 1       | 24      | 1             |
| 1       | 56      | 1             |
| 1       | 33      | 1             |
| 2       | 24      | 1             |
| 2       | 56      | 1             |
| 2       | 11      | 1             |
| 2       | 88      | 1             |
| 3       | 88      | 1             |
| 3       | 23      | 1             |
| 4       | 88      | 1             |
| 4       | 77      | 1             |
| 4       | 23      | 1             |
| 5       | 77      | 1             |
| 5       | 23      | 1             |
+---------+---------+---------------+
Take user 1 as an example:
  - User 1 is friends with users 2, 3, 4, and 6.
  - Recommended pages are 23 (user 2 liked it), 24 (user 3 liked it), 56 (user 3 liked it), 33 (user 6 liked it), and 77 (user 2 and user 3 liked it).
  - Note that page 88 is not recommended because user 1 already liked it.

Another example is user 6:
  - User 6 is friends with user 1.
  - User 1 only liked page 88, but user 6 already liked it. Hence, user 6 has no recommendations.

You can recommend pages for users 2, 3, 4, and 5 using a similar process.

2.Create Table and insert into values

create table if not exists leetcode.ex_1892_friendship
(user1_id string ,  user2_id string ) stored as orc ;

INSERT INTO table leetcode.ex_1892_friendship VALUES
('1','2'),   
('1','3'),    
('1','4'),      
('2','3'),       
('2','4'),     
('2','5'),  
('6','1') ; 

create table if not exists leetcode.ex_1892_likes
(user_id string ,  page_id string ) stored as orc ;

INSERT INTO table leetcode.ex_1892_likes VALUES
('1','88'),  
('2','23'), 
('3','24'),
('4','56'), 
('5','11'),
('6','33'),
('2','77'),
('3','77'),
('6','88') ;

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.

PreviousHive SolutionNextHive Solution

Last updated 3 years ago