65.7k views
2 votes
Write an SQL query that makes recommendations using the pages that your friends liked. Assume you have two tables: a two-column table of users and their friends, and a two-column table of users and the pages they liked. It should not recommend pages you already like.

User Chloe
by
8.2k points

1 Answer

6 votes

Final answer:

To make recommendations using your friends' liked pages in SQL, use the JOIN and NOT EXISTS statements.

Step-by-step explanation:

To recommend pages using the pages your friends liked, you can use a combination of the SQL JOIN and NOT EXISTS statements.

Here's an example query:

SELECT DISTINCT pl.page_id, pl.page_name
FROM page_likes pl
JOIN friend_relationships fr ON fr.friend_id = pl.user_id
WHERE fr.user_id = 'your_user_id'
AND NOT EXISTS (
SELECT 1
FROM page_likes pl2
WHERE pl2.user_id = 'your_user_id'
AND pl2.page_id = pl.page_id
)

In this query, 'your_user_id' should be replaced with the ID of the user for whom you want to make recommendations.

User Lotok
by
8.8k points