Skip to main content

LeetCode 578. Get Highest Answer Rate Question SQL Solution

Problem

LeetCode SQL Problem

  1. Get Highest Answer Rate Question

survey_log table

idactionquestion_idanswer_idq_numtimestamp
5show2851123
5answer2851241241124
5show3692125
5skip3692126

Solution

Get Highest Answer Rate Question
WITH answer_rate_analysis
AS (
SELECT question_id
,COUNT(answer_id) / CAST(SUM(CASE
WHEN action = 'show'
THEN 1
ELSE 0
END) AS FLOAT) AS answer_rate
FROM survey_log
GROUP BY question_id
)
SELECT TOP 1 question_id AS survey_log
FROM answer_rate_analysis
ORDER BY answer_rate DESC

Query Output

survey_log
285