Get Highest Answer Rate Question

medium database sql

Problem

SurveyLog(id, action ∈ {show,answer,skip}, question_id, answer_id, q_num, timestamp). Return the question_id with the highest rate = answers / shows, breaking ties by smallest id.

Inputlogs=[(5,'show',285,null,1,123),(5,'answer',285,124124,1,124),(5,'show',369,null,2,125),(5,'skip',369,null,2,126)]
Output285
Q 285 has rate 1, Q 369 has rate 0.

SELECT question_id AS survey_log
FROM SurveyLog
GROUP BY question_id
ORDER BY SUM(action='answer') / SUM(action='show') DESC, question_id ASC
LIMIT 1;
Time: O(n) Space: O(n)