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.
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.
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
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.
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.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Fix Names in a Table | Easy | Solve | |
| Not Boring Movies | Easy | Solve | |
| Primary Department for Each Employee | Easy | Solve | |
| Queries Quality and Percentage | Easy | Solve | |
| Combine Two Tables | Easy | Solve |