Skip to content

Latest commit

 

History

History
25 lines (20 loc) · 906 Bytes

1633. Percentage of Users Attended a Contest.md

File metadata and controls

25 lines (20 loc) · 906 Bytes

1633. Percentage of Users Attended a Contest

Question Link

First we'll see a wrong one and explanation then we'll see a right one ;-)

WEONG Code

# Write your MySQL query statement below
select r.contest_id, round(count(distinct r.user_id)*100/count(user_id), 2) percentage
from Register r left join Users u
on u.user_id=r.user_id group by r.contest_id ORDER BY percentage DESC, contest_id

Why wrong: The dividend (or the denominator) is wrong. It is not the User table's countnumber but the new table's.

RIGHT Code

# Write your MySQL query statement below
select r.contest_id, round(count(distinct r.user_id)*100/(select count(user_id) from Users), 2) percentage
from Register r left join Users u
on u.user_id=r.user_id group by r.contest_id ORDER BY percentage DESC, contest_id
  • 2024.06.02