/*
Meta/Facebook SQL Interview Question

The "famous" table contains two columns: user_id and follower_id.
It represents the follower relationships among users, where each follower_id is also a user on the platform.  

Calculate the famous percentage for each user using the formula:  
Famous Percentage = (Number of followers a user has) / (Total number of users on the platform)
*/

-- Schema Setup:

CREATE TABLE famous (user_id INT, follower_id INT);

INSERT INTO famous VALUES
(1, 2), (1, 3), (2, 4), (5, 1), (5, 3), 
(11, 7), (12, 8), (13, 5), (13, 10), 
(14, 12), (14, 3), (15, 14), (15, 13);

-- Solution

WITH total_users AS
(
 SELECT
    user_id AS users
 FROM famous
 UNION
 SELECT
    follower_id AS users
 FROM famous
)
SELECT
   user_id,
   COUNT(follower_id) * 100.0 / (SELECT COUNT(*) FROM total_users) AS famous_percentage
FROM famous
GROUP BY user_id;

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: