186. Average comments per question

easy google

  • ********************************************************************
  • ***************************************************************
  • ***************************************************************************************************************************************

Table: google_forum

Google's internal Q&A forum.

   col_name   | col_type
--------------+--------------------------
 created_dt   | date
 post_id      | int
 post_type    | varchar(20) -- either a question or a comment 
 employee_id  | int
 content      | text 

Sample results

          avg
------------------------
 0.72631578947368421053

Solution postgres

WITH questions AS (
    SELECT post_id, created_dt
    FROM google_forum
    WHERE post_type = 'question'
),
comments AS (
    SELECT post_id, created_dt
    FROM google_forum
    WHERE post_type = 'comment'
)

SELECT avg(num_comments) FROM (
    SELECT Q.post_id, COUNT(C.post_id) AS num_comments
    FROM questions Q
    LEFT JOIN comments C
    ON Q.post_id = C.post_id
    AND Q.created_dt = C.created_dt
    GROUP BY 1
) X;
    

Explanation

This query calculates the average number of comments per question in a Google forum. It does this by first creating two temporary tables, "questions" and "comments," which contain the post ID and creation date for all posts of the respective types. Then, it joins these two tables on the post ID and creation date to count the number of comments for each question. Finally, it takes the average of these counts to get the desired result.

Expected results


More Google questions

ID Title Level FTPR
101 Average number of visits per user google easy
14%
102 Histogram by visit session duration google hard
17%
103 Median and average session duration by day of the week google medium
20%
104 MAU: Monthly active users google easy
20%
105 Number of days gap between last two actions google hard
8%
106 Mobile vs. web google medium
10%
107 Customer count by platform google medium
17%
147 2 days streak customers google hard
13%
148 Most popular video category google medium
13%
149 fans by video category google easy
36%
150 Countries with above average customers google easy
15%
187 Comment distribution at Google Forum google easy
17%
188 Top 1 popular question by department google hard
14%