Percentage of Users Attended a Contest - Problem
Imagine you're managing a competitive coding platform where users can register for various programming contests. You need to generate analytics reports showing how popular each contest is among your user base.
Given two tables:
- Users: Contains information about all registered users on your platform
- Register: Records which users have signed up for which contests
Your task is to calculate the percentage of total users who registered for each contest. The results should be:
- Rounded to 2 decimal places
- Ordered by percentage in descending order (most popular contests first)
- In case of ties, ordered by
contest_idin ascending order
This will help identify which contests generate the most interest and engagement among your user community!
Input & Output
example_1.sql โ Basic Contest Analytics
$
Input:
Users table:
| user_id | user_name |
|---------|----------|
| 6 | Alice |
| 2 | Bob |
| 7 | Alex |
Register table:
| contest_id | user_id |
|------------|--------|
| 215 | 6 |
| 209 | 2 |
| 208 | 2 |
| 210 | 6 |
| 208 | 6 |
| 209 | 7 |
| 209 | 6 |
| 215 | 7 |
| 208 | 7 |
| 210 | 2 |
| 207 | 2 |
| 209 | 2 |
โบ
Output:
| contest_id | percentage |
|------------|------------|
| 208 | 100.00 |
| 209 | 100.00 |
| 210 | 66.67 |
| 215 | 66.67 |
| 207 | 33.33 |
๐ก Note:
Total users = 3. Contest 208 and 209 have all 3 users (100%), contests 210 and 215 have 2 users each (66.67%), contest 207 has 1 user (33.33%). Results are sorted by percentage DESC, then contest_id ASC for ties.
example_2.sql โ Single Contest Scenario
$
Input:
Users table:
| user_id | user_name |
|---------|----------|
| 1 | John |
| 2 | Jane |
| 3 | Mike |
| 4 | Sara |
Register table:
| contest_id | user_id |
|------------|--------|
| 101 | 1 |
| 101 | 3 |
โบ
Output:
| contest_id | percentage |
|------------|------------|
| 101 | 50.00 |
๐ก Note:
Only one contest with 2 out of 4 total users registered, giving 50% participation rate.
example_3.sql โ Multiple Contests with Ties
$
Input:
Users table:
| user_id | user_name |
|---------|----------|
| 1 | User1 |
| 2 | User2 |
| 3 | User3 |
| 4 | User4 |
| 5 | User5 |
Register table:
| contest_id | user_id |
|------------|--------|
| 301 | 1 |
| 301 | 2 |
| 302 | 1 |
| 302 | 3 |
| 303 | 5 |
โบ
Output:
| contest_id | percentage |
|------------|------------|
| 301 | 40.00 |
| 302 | 40.00 |
| 303 | 20.00 |
๐ก Note:
Contests 301 and 302 both have 40% participation (tied), so they're ordered by contest_id ASC. Contest 303 has 20% participation.
Constraints
- 1 โค Users table size โค 104
- 0 โค Register table size โค 105
- 1 โค user_id โค 109
- 1 โค contest_id โค 109
- It is guaranteed that each user_id in Register table exists in Users table
- Each (contest_id, user_id) pair in Register table is unique
- Results must be rounded to exactly 2 decimal places
Visualization
Tap to expand
Understanding the Visualization
1
Count All Users
Get total number of registered users (denominator for percentage)
2
Group Registrations
Group register entries by contest_id and count participants per contest
3
Calculate Percentages
For each contest: (participants / total_users) * 100, rounded to 2 decimals
4
Sort Results
Order by percentage DESC, then contest_id ASC for ties
Key Takeaway
๐ฏ Key Insight: Use SQL aggregation with GROUP BY to count participants per contest, then calculate percentages using the total user count as denominator. The ORDER BY clause handles both primary (percentage) and secondary (contest_id) sorting criteria.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code