Study - Problems(IT)/LeetCode - SQL

1633. Percentage of Users Attended a Contest

Dev.D 2024. 11. 14. 23:38

* Problem

 

Write a solution to find the percentage of the users registered in each contest rounded to two decimals.

Return the result table ordered by percentage in descending order. In case of a tie, order it by contest_id in ascending order.

 

Explanation: 
All the users registered in contests 208, 209, and 210. The percentage is 100% and we sort them in the answer table by contest_id in ascending order.
Alice and Alex registered in contest 215 and the percentage is ((2/3) * 100) = 66.67%
Bob registered in contest 207 and the percentage is ((1/3) * 100) = 33.33%

 

* Final Solution (Success)

# Write your MySQL query statement below

select contest_id
           , round( count(distinct user_id)*100/ (select count(user_id) from Users),2) as percentage
    from register
    group by contest_id
    order by percentage desc, contest_id