Interview Questions/SQL/Percentage of Users Attended a Contest

Percentage of Users Attended a Contest

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You have two tables: Users and Register. The Register table records which users signed up for which contests. Write a SQL query to find the percentage of users registered in each contest, rounded to 2 decimal places. Return the result ordered by percentage descending. If there is a tie, order by contest_id ascending. (contest_id, user_id) is the primary key.

Database Schema

Users

Column NameTypeDescription
user_idINTPrimary key
user_nameVARCHARUser name

Register

Column NameTypeDescription
contest_idINTContest identifier
user_idINTFK to Users

Example

Users

user_iduser_name
6Alice
2Bob
7Alex

Register

contest_iduser_id
2156
2092
2082
2106
2086
2097
2096
2157
2087
2102
2072
2107

Output

contest_idpercentage
208100
209100
210100
21566.67
20733.33

Explanation:

Contests 208, 209, 210 had all 3 users (100%). Contest 215 had 2/3 users (66.67%). Contest 207 had 1/3 users (33.33%).

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Users

user_iduser_name
6Alice
2Bob
7Alex

Register

contest_iduser_id
2156
2092
2082
2106
2086
2097
2096
2157
2087
2102
2072
2107

Output

contest_idpercentage
208100
209100
210100
21566.67
20733.33