Calculate Contest Registration Percentages with the Source Tie Order
Quick Overview
For each contest present in Register, report contest_id and percentage of all Users registered in it, rounded to two decimal places. Sort percentage descending, then contest_id ascending for equal percentages.
Calculate Contest Registration Percentages with the Source Tie Order
Company: Amazon
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
### Problem
For each contest present in Register, report contest_id and percentage of all Users registered in it, rounded to two decimal places. Sort percentage descending, then contest_id ascending for equal percentages.
### Tables
Table: `Users`
```text
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| user_name | varchar |
+-------------+---------+
user_id is the primary key (column with unique values) for this table.
Each row of this table contains the name and the id of a user.
```
Table: `Register`
```text
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| contest_id | int |
| user_id | int |
+-------------+---------+
(contest_id, user_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the id of a user and the contest they registered into.
```
### Constraints
- In this version, every `Register.user_id` references an existing `Users.user_id`.
### Example
```text
Users (user_id,user_name): (1,Ada), (2,Bo), (3,Cam), (4,Dee)
Register (contest_id,user_id): (20,1), (20,2), (10,3), (10,4), (30,1)
Output (contest_id,percentage): (10,50.00), (20,50.00), (30,25.00)
```
### What a Strong Answer Covers
- Use all users as the denominator for each registered contest.
- Round percentages to the requested precision and apply the stated tie order.
### Follow-up Questions
- How would you show contests with zero registrations if a contest table were available?
- How would you compare percentages using only users eligible for each contest?
Overview: For each contest present in Register, report contest_id and percentage of all Users registered in it, rounded to two decimal places. Sort percentage descending, then contest_id ascending for equal percentages.
For each contest present in Register, report contest_id and percentage of all Users registered in it, rounded to two decimal places. Sort percentage descending, then contest_id ascending for equal percentages.
Tables
Table: Users
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| user_name | varchar |
+-------------+---------+
user_id is the primary key (column with unique values) for this table.
Each row of this table contains the name and the id of a user.
Table: Register
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| contest_id | int |
| user_id | int |
+-------------+---------+
(contest_id, user_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the id of a user and the contest they registered into.
Constraints
In this version, every
Register.user_id
references an existing
Users.user_id
.