Leetcode Problem 2720. Popularity Percentage

2720. Popularity Percentage

Leetcode Solutions

CTE, UNION ALL, and Group BY

  1. Create a CTE named all_friendships that selects all friendships from the Friends table, including both directions (user1 to user2 and user2 to user1) using UNION ALL.\n2. Create another CTE named user_counts to calculate the total number of distinct users.\n3. Select the user and calculate the popularity percentage by counting the distinct number of friends for each user, dividing by the total number of users, and multiplying by 100.\n4. Use the ROUND function to format the result to 2 decimal places.\n5. Order the final result by user1 in ascending order.
erDiagram\n    Friends {\n        int user1 PK\n        int user2 PK\n    }

Simple GROUP BY

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...