The Page Recommendations II problem extends the first by computing a "friendship score" for each recommended page — the number of friends who liked the page. For each user, recommend pages (not already liked) with their friendship score. This hard SQL problem adds aggregation and scoring to the recommendation query.
Meta asks this hard SQL problem to test advanced aggregation with complex JOINs. It requires computing COUNT of friends who liked each page per user, which involves a correlated multi-table join with GROUP BY and exclusion. The database interview pattern at an advanced level is demonstrated.
Friend-page-count aggregation. For each user, join with Friendship to get friends, join friends with Likes, group by (user_id, page_id), count friends. Exclude pages already liked by each user. Use a CTE or subquery for the exclusion.
WITH FriendPages AS (
SELECT CASE WHEN f.user1_id = u.user_id THEN f.user2_id ELSE f.user1_id END AS friend_id,
u.user_id, l.page_id
FROM Users u
JOIN Friendship f ON u.user_id IN (f.user1_id, f.user2_id)
JOIN Likes l ON ... -- friend's likes
WHERE l.page_id NOT IN (SELECT page_id FROM Likes WHERE user_id = u.user_id)
)
SELECT user_id, page_id, COUNT(*) AS friends_likes FROM FriendPages GROUP BY user_id, page_id
Recommendation with scoring SQL problems require three phases: (1) find connections, (2) aggregate recommended items with scores, (3) exclude already-consumed items. The per-user exclusion is the trickiest part — it must be applied within the context of each user, not globally. Practice building recommendation systems in SQL for music (liked songs of friends), movies, and news articles.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Finding the Topic of Each Post | Hard | Solve | |
| Report Contiguous Dates | Hard | Solve | |
| Human Traffic of Stadium | Hard | Solve | |
| Trips and Users | Hard | Solve | |
| Department Top Three Salaries | Hard | Solve |