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 identifieraction: What the user did - "show" (question displayed), "answer" (user responded), or "skip" (user ignored)question_id: Which question this was aboutanswer_id: The answer chosen (null if not answered)q_num: Question order in the sessiontimestamp: 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code