1633. Percentage of Users Attended a Contest
SQL 50 · Basic Aggregate Functions (day 3). Decode it with the hints, solve it on LeetCode, then compare with the reference solution in MySQLPostgreSQL.
Your work
Hints
Open one at a time. Try to finish with as few as you can.
1. What does one output row represent?
One contest.
2. Approach
GROUP BY contest_id, COUNT(user_id) * 100 / (SELECT COUNT(*) FROM Users), ROUND to 2.
3. The trap
Order by percentage DESC, then contest_id ASC.COUNT * 100 / COUNT is integer division in PostgreSQL. Write 100.0. Order by percentage DESC, contest_id ASC.
Reference solution
Tested on MySQL 8.0 and PostgreSQL 16. Switch dialect at the top of the page.
Show the MySQLPostgreSQL solution
SELECT contest_id, ROUND(COUNT(user_id) * 100 / (SELECT COUNT(*) FROM Users), 2) AS percentage FROM Register GROUP BY contest_id ORDER BY percentage DESC, contest_id;SELECT contest_id, ROUND(COUNT(user_id) * 100.0 / (SELECT COUNT(*) FROM Users), 2) AS percentage FROM Register GROUP BY contest_id ORDER BY percentage DESC, contest_id;