Knowledge Guide
HomeDatabasesSQL Practice Problems

Popularity Percentage

Problem

Table: Friends

+-------------+------+
| Column Name | Type |
+-------------+------+
| user1       | int  |
| user2       | int  |
+-------------+------+
(user1, user2) is the primary key (combination of unique values) of this table.
Each row contains information about friendship where user1 and user2 are friends.

Problem Definition

Write a solution to find the popularity percentage for each user on Meta/Facebook. The popularity percentage is defined as the total number of friends the user has divided by the total number of users on the platform, then converted into a percentage by multiplying by 100, rounded to 2 decimal places.

Return the result table ordered by user1 in ascending order.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To determine the popularity percentage of each user on Meta/Facebook, we need to calculate the proportion of friends each user has relative to the total number of users on the platform. The popularity percentage is defined as:

The final result should display each user’s user1 and their corresponding percentage_popularity, rounded to two decimal places. The results must be ordered by user1 in ascending order.

Approach Overview

  1. Transform Friendships to Bidirectional Relationships:

    • Ensure that each friendship is represented in both directions (i.e., if User A is friends with User B, then User B is also friends with User A). This facilitates accurate counting of friends for each user.
  2. Identify All Unique Users:

    • Extract all unique users from both user1 and user2 columns to determine the total number of users on the platform.
  3. Calculate the Number of Friends per User:

    • For each user, count the number of unique friends they have.
  4. Compute Popularity Percentage:

    • For each user, divide the number of friends by the total number of unique users and multiply by 100. Round the result to two decimal places.
  5. Finalize the Results:

    • Present the user1 and their corresponding percentage_popularity in ascending order of user1.

SQL Query

WITH two_way_friendships AS ( SELECT user1, user2 FROM Friends UNION ALL SELECT user2 AS user1, user1 AS user2 FROM Friends ), unique_users AS ( SELECT user1 AS user_id FROM two_way_friendships UNION SELECT user2 AS user_id FROM two_way_friendships ), total_users AS ( SELECT COUNT(*) AS total_count FROM unique_users ) SELECT T.user1, ROUND(100.00 * (COUNT(DISTINCT T.user2) / TU.total_count), 2) AS percentage_popularity FROM two_way_friendships T CROSS JOIN total_users TU GROUP BY T.user1, TU.total_count ORDER BY T.user1;

Step-by-Step Approach

Step 1: Transform Friendships to Bidirectional Relationships (two_way_friendships CTE)

Ensure that each friendship is represented in both directions to accurately count the number of friends each user has.

SQL Snippet:

WITH two_way_friendships AS ( SELECT user1, user2 FROM Friends UNION ALL SELECT user2 AS user1, user1 AS user2 FROM Friends ) SELECT * FROM two_way_friendships;

Explanation:

+-------+-------+ | user1 | user2 | +-------+-------+ | 2 | 1 | | 1 | 3 | | 4 | 1 | | 1 | 5 | | 1 | 6 | | 2 | 6 | | 7 | 2 | | 8 | 3 | | 3 | 9 | | 1 | 2 | | 3 | 1 | | 5 | 1 | | 6 | 1 | | 6 | 2 | | 2 | 7 | | 3 | 8 | | 9 | 3 | +-------+-------+

Step 2: Identify All Unique Users (unique_users CTE)

Determine the total number of unique users on the platform by extracting all distinct users from both user1 and user2 columns.

SQL Snippet:

WITH two_way_friendships AS ( SELECT user1, user2 FROM Friends UNION ALL SELECT user2 AS user1, user1 AS user2 FROM Friends ), unique_users AS ( SELECT user1 AS user_id FROM two_way_friendships UNION SELECT user2 AS user_id FROM two_way_friendships ) SELECT * FROM unique_users;

Explanation:

+---------+ | user_id | +---------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | +---------+

Step 3: Calculate the Total Number of Users (total_users CTE)

Calculate the total number of unique users on the platform to use as the denominator for the popularity percentage calculation.

SQL Snippet:

WITH two_way_friendships AS ( SELECT user1, user2 FROM Friends UNION ALL SELECT user2 AS user1, user1 AS user2 FROM Friends ), unique_users AS ( SELECT user1 AS user_id FROM two_way_friendships UNION SELECT user2 AS user_id FROM two_way_friendships ), total_users AS ( SELECT COUNT(*) AS total_count FROM unique_users ) SELECT * FROM total_users;

Explanation:

+-------------+ | total_count | +-------------+ | 9 | +-------------+

Step 4: Calculate the Number of Friends per User and Compute Popularity Percentage

For each user, count the number of unique friends they have and calculate their popularity percentage based on the total number of users.

SQL Snippet:

WITH two_way_friendships AS ( SELECT user1, user2 FROM Friends UNION ALL SELECT user2 AS user1, user1 AS user2 FROM Friends ), unique_users AS ( SELECT user1 AS user_id FROM two_way_friendships UNION SELECT user2 AS user_id FROM two_way_friendships ), total_users AS ( SELECT COUNT(*) AS total_count FROM unique_users ) SELECT T.user1, ROUND(100.00 * (COUNT(DISTINCT T.user2) / TU.total_count), 2) AS percentage_popularity FROM two_way_friendships T CROSS JOIN total_users TU GROUP BY T.user1, TU.total_count ORDER BY T.user1;

Explanation:

Intermediate Output After Step 4:

+-------+-----------------------+ | user1 | percentage_popularity | +-------+-----------------------+ | 1 | 55.56 | | 2 | 33.33 | | 3 | 33.33 | | 4 | 11.11 | | 5 | 11.11 | | 6 | 22.22 | | 7 | 11.11 | | 8 | 11.11 | | 9 | 11.11 | +-------+-----------------------+

Explanation of Output:

Step 5: Finalize the Results

Present the user1 and their corresponding percentage_popularity in ascending order of user1.

Final Output:

+-------+-----------------------+ | user1 | percentage_popularity | +-------+-----------------------+ | 1 | 55.56 | | 2 | 33.33 | | 3 | 33.33 | | 4 | 11.11 | | 5 | 11.11 | | 6 | 22.22 | | 7 | 11.11 | | 8 | 11.11 | | 9 | 11.11 | +-------+-----------------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Popularity Percentage? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Popularity Percentage** (Databases) and want to truly understand it. Explain Popularity Percentage from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Popularity Percentage** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Popularity Percentage** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Popularity Percentage** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes