Get Highest Answer Rate Question - Problem

Imagine you're analyzing user behavior on a survey platform! ๐Ÿ“Š You have a detailed log of every action users take with survey questions.

Your Mission: Find the question with the highest answer rate - the one that users are most likely to answer when they see it!

The Data: You have a SurveyLog table tracking user interactions:

  • id: User identifier
  • action: What the user did - "show" (question displayed), "answer" (user responded), or "skip" (user ignored)
  • question_id: Which question this was about
  • answer_id: The answer chosen (null if not answered)
  • q_num: Question order in the session
  • timestamp: When this happened

Answer Rate Formula: (Number of "answer" actions) รท (Number of "show" actions) for each question

Output: Return the question_id with the highest answer rate. If there's a tie, choose the question with the smallest question_id.

Input & Output

example_1.sql โ€” Basic Survey Data
$ Input: SurveyLog table: +----+--------+-------------+-----------+-------+-----------+ | id | action | question_id | answer_id | q_num | timestamp | +----+--------+-------------+-----------+-------+-----------+ | 5 | show | 285 | null | 1 | 123 | | 5 | answer | 285 | 124124 | 1 | 124 | | 5 | show | 369 | null | 2 | 125 | | 5 | skip | 369 | null | 2 | 126 | +----+--------+-------------+-----------+-------+-----------+
โ€บ Output: 285
๐Ÿ’ก Note: Question 285: 1 answer out of 1 show = 100% answer rate. Question 369: 0 answers out of 1 show = 0% answer rate. Question 285 has the highest answer rate.
example_2.sql โ€” Multiple Questions with Ties
$ Input: SurveyLog table: +----+--------+-------------+-----------+-------+-----------+ | id | action | question_id | answer_id | q_num | timestamp | +----+--------+-------------+-----------+-------+-----------+ | 1 | show | 285 | null | 1 | 100 | | 1 | answer | 285 | 100 | 1 | 101 | | 2 | show | 369 | null | 1 | 102 | | 2 | answer | 369 | 200 | 1 | 103 | | 3 | show | 403 | null | 1 | 104 | | 3 | skip | 403 | null | 1 | 105 | +----+--------+-------------+-----------+-------+-----------+
โ€บ Output: 285
๐Ÿ’ก Note: Question 285: 1/1 = 100% answer rate. Question 369: 1/1 = 100% answer rate. Question 403: 0/1 = 0% answer rate. Questions 285 and 369 tie at 100%, but 285 has smaller question_id, so it wins.
example_3.sql โ€” Complex Multi-User Scenario
$ Input: SurveyLog table: +----+--------+-------------+-----------+-------+-----------+ | id | action | question_id | answer_id | q_num | timestamp | +----+--------+-------------+-----------+-------+-----------+ | 1 | show | 285 | null | 1 | 100 | | 1 | answer | 285 | 100 | 1 | 101 | | 2 | show | 285 | null | 1 | 102 | | 2 | skip | 285 | null | 1 | 103 | | 3 | show | 369 | null | 1 | 104 | | 3 | answer | 369 | 200 | 1 | 105 | +----+--------+-------------+-----------+-------+-----------+
โ€บ Output: 369
๐Ÿ’ก Note: Question 285: 1 answer out of 2 shows = 50% answer rate. Question 369: 1 answer out of 1 show = 100% answer rate. Question 369 has the higher answer rate.

Constraints

  • 1 โ‰ค SurveyLog table rows โ‰ค 104
  • Each action is one of: 'show', 'answer', 'skip'
  • answer_id is null unless action = 'answer'
  • question_id and answer_id are positive integers
  • At least one question will have action = 'show'

Visualization

Tap to expand
๐Ÿ“Š Survey Answer Rate AnalysisRaw Survey Logs (Mixed Actions)show Q285answer Q285show Q369skip Q369show Q285answer Q369show Q403GROUP BY question_idQ285Shows: 2Answers: 1Rate: 50%Q369Shows: 1Answers: 1Rate: 100%Q403Shows: 1Answers: 0Rate: 0%ORDER BY rate DESC, question_id ASC๐Ÿ† Ranking Results1st: Q369 (100%)2nd: Q285 (50%)3rd: Q403 (0%)Winner: Q369 โœจ๐Ÿ’ก Key: Single SQL query with GROUP BY + conditional aggregation
Understanding the Visualization
1
Data Collection
Every user interaction (show, answer, skip) is logged with timestamps
2
Group by Question
SQL GROUP BY creates separate buckets for each question_id
3
Count Actions
For each question, count 'show' and 'answer' actions using CASE WHEN
4
Calculate Rates
Answer rate = (answers รท shows) ร— 100% for each question
5
Find Winner
ORDER BY rate DESC, question_id ASC finds the highest rate with tie-breaking
Key Takeaway
๐ŸŽฏ Key Insight: SQL's GROUP BY with CASE WHEN enables efficient calculation of answer rates for all questions in a single table scan, avoiding the need for multiple queries or complex joins.
Asked in
Facebook 35 Google 28 Amazon 22 Microsoft 18
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen