SQL Interview Questions – Percentage of Users Attended a Contest

Spread the love

Problem Description –

Write an SQL query 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.

The query result format is in the following example.

Problem Link – Percentage of Users Attended a Contest

Difficulty Level – Easy

Solution –

SELECT
    contest_id,
    ROUND(COUNT(DISTINCT user_id) * 100 / (SELECT COUNT(DISTINCT user_id) FROM Users ), 2) as percentage
FROM Register
GROUP BY 1
ORDER BY 2 DESC, 1

Rating: 1 out of 5.

Leave a Reply