Leetcode Problem 1264. Page Recommendations

1264. Page Recommendations

Leetcode Solutions

Using Subqueries and Set Operations

  1. Create a subquery to select all user2_id where user1_id is 1 (the user for whom we are recommending pages).
  2. Create a second subquery to select all user1_id where user2_id is 1.
  3. Combine the results of the two subqueries using the UNION operator to get a complete list of friends without duplicates.
  4. Create a third subquery to select all page_id that user 1 already likes.
  5. Write the main query to select distinct page_id from the Likes table where the user_id is in the list of friends obtained from step 3 and the page_id is not in the list of pages user 1 already likes from step 4.
  6. Return the page_id as recommended_page.
erDiagram
    Friendship {
        int user1_id
        int user2_id
    }
    Likes {
        int user_id
        int page_id
    }
    Friendship ||--o{ Likes : "has"

Using Common Table Expressions (CTEs) and JOIN Operations

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...