Page Recommendations II
<p>Table: <code>Friendship</code></p>

<pre>
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user1_id      | int     |
| user2_id      | int     |
+---------------+---------+
(user1_id, user2_id) is the primary key (combination of columns with unique values)&nbsp;for this table.
Each row of this table indicates that the users user1_id and user2_id are friends.
</pre>

<p>&nbsp;</p>

<p>Table: <code>Likes</code></p>

<pre>
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| user_id     | int     |
| page_id     | int     |
+-------------+---------+
(user_id, page_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that user_id likes page_id.
</pre>

<p>&nbsp;</p>

<p>You are implementing a page recommendation system for a social media website. Your system will <strong>recommend</strong> a page to <code>user_id</code> if the page is <strong>liked</strong> by <strong>at least one</strong> friend of <code>user_id</code> and is <strong>not liked</strong> by <code>user_id</code>.</p>

<p>Write a solution&nbsp;to find all the possible <strong>page recommendations</strong> for every user. Each recommendation should appear as a row in the result table with these columns:</p>

<ul>
	<li><code>user_id</code>: The ID of the user that your system is making the recommendation to.</li>
	<li><code>page_id</code>: The ID of the page that will be recommended to <code>user_id</code>.</li>
	<li><code>friends_likes</code>: The number of the friends of <code>user_id</code> that like <code>page_id</code>.</li>
</ul>

<p>Return the result table in <strong>any order</strong>.</p>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> 
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      |
+---------+---------+
<strong>Output:</strong> 
+---------+---------+---------------+
| 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             |
+---------+---------+---------------+
<strong>Explanation:</strong> 
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.
</pre>
Database
# Write your MySQL query statement below
WITH
    S AS (
        SELECT * FROM Friendship
        UNION
        SELECT user2_id, user1_id FROM Friendship
    )
SELECT user1_id AS user_id, page_id, COUNT(1) AS friends_likes
FROM
    S AS s
    LEFT JOIN Likes AS l ON s.user2_id = l.user_id
WHERE
    NOT EXISTS (
        SELECT 1
        FROM Likes AS l2
        WHERE user1_id = l2.user_id AND l.page_id = l2.page_id
    )
GROUP BY user1_id, page_id;
