Percentage of Users Attended a Contest - Problem

You are given two tables: Users and Register.

The Users table contains information about users with their user_id (primary key) and user_name.

The Register table contains contest registration data with contest_id and user_id as a composite primary key.

Task: Calculate the percentage of users who registered for each contest, rounded to 2 decimal places.

Output requirements:

  • Order results by percentage in descending order
  • For ties, order by contest_id in ascending order
  • Return contest_id and percentage columns

Table Schema

Users
Column Name Type Description
user_id PK int Unique identifier for each user
user_name varchar Name of the user
Primary Key: user_id
Register
Column Name Type Description
contest_id PK int Identifier for the contest
user_id PK int User who registered for the contest
Primary Key: (contest_id, user_id)

Input & Output

Example 1 — Basic Contest Registration
Input Tables:
Users
user_id user_name
1 Alice
2 Bob
3 Charlie
4 David
Register
contest_id user_id
208 1
208 2
210 2
Output:
contest_id percentage
208 50
210 25
💡 Note:

Contest 208 has 2 registrations out of 4 total users: (2/4) × 100 = 50.00%. Contest 210 has 1 registration out of 4 total users: (1/4) × 100 = 25.00%. Results are ordered by percentage descending.

Example 2 — Equal Percentages
Input Tables:
Users
user_id user_name
1 Alice
2 Bob
Register
contest_id user_id
215 1
220 2
Output:
contest_id percentage
215 50
220 50
💡 Note:

Both contests have the same percentage (50.00%), so they are ordered by contest_id in ascending order: 215 comes before 220.

Constraints

  • 1 ≤ user_id ≤ 1000
  • 1 ≤ contest_id ≤ 1000
  • Each user can register for multiple contests
  • Each (contest_id, user_id) pair is unique in Register table

Visualization

Tap to expand
Contest Registration Percentage AnalysisUsers (4 total)user_iduser_name1,2,3,4Users...Registercontest_iduser_id2081,22102COUNT/TOTAL* 100Result: Percentagescontest_idpercentage20850.0021025.00Contest 208: 2/4 = 50%Contest 210: 1/4 = 25%
Understanding the Visualization
1
Input
Users table and Register table with registrations
2
Group & Count
Group by contest_id and count registrations
3
Calculate %
Divide by total users, multiply by 100, and round
Key Takeaway
🎯 Key Insight: Use GROUP BY to aggregate data and calculate percentages with proper rounding and ordering
Asked in
Amazon 12 Microsoft 8 Facebook 6
28.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen