Magicsheet logo

Percentage of Users Attended a Contest

Easy
88.6%
Updated 6/1/2025

Percentage of Users Attended a Contest

What is this problem about?

The Percentage of Users Attended a Contest SQL problem asks you to compute the percentage of registered users who attended each contest, ordered by percentage descending, then contest_id ascending. This easy SQL problem tests JOIN, GROUP BY, percentage calculation, and ordering. The database interview pattern is directly demonstrated.

Why is this asked in interviews?

Microsoft, Meta, Amazon, Google, and Bloomberg ask this as a standard SQL aggregation and percentage calculation problem — a fundamental data analytics task. It tests the ability to combine user counts with attendance records.

Algorithmic pattern used

JOIN + GROUP BY + percentage formula. Count total users: SELECT COUNT(*) FROM Users. For each contest: COUNT(user_id) * 100 / total_users. Join Register table, group by contest_id, compute percentage, order by percentage DESC, contest_id ASC.

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 ASC

Example explanation

Total users=6. Contest 215: attended=5. Percentage=5*100/6=83.33. Contest 209: attended=6. Percentage=100.00. Order by percentage descending: 209 first, then 215.

Common mistakes candidates make

  • Integer division instead of decimal percentage (multiply by 100.0 or use ROUND).
  • Wrong ORDER BY direction (percentage DESC but contest_id ASC on ties).
  • Computing total users inside GROUP BY (must be a subquery or variable).
  • Not rounding to 2 decimal places as required.

Interview preparation tip

Percentage computation SQL problems follow the pattern: COUNT(condition) * 100.0 / total. Always precompute the total in a subquery. Use ROUND(value, 2) for decimal rounding. Practice similar problems: "percentage of orders shipped," "percentage of users active." The double ordering (by metric DESC, by ID ASC) is standard for ranked results.

Similar Questions