SQL
Lab
Microsoft Microsoft Interview Question 02

Product
Adoption
Trend

Find monthly active users for each product.

Table Schema

Inspect
Table

interactive
ColumnType
user_idinteger
product_namevarchar
usage_datedate

Sample Data

Input
Output

Sample Input: product_usage
user_idproduct_nameusage_date
101Teams2024-01-01
102Teams2024-01-02
103Outlook2024-01-03
101Teams2024-02-01
104Outlook2024-02-02
105Teams2024-02-03
Expected Output
monthproduct_nameactive_users
2024-01Outlook1
2024-01Teams2
2024-02Outlook1
2024-02Teams2

SQL Editor

Run
Query

postgresql
Waiting for query

monthproduct_nameactive_users
2024-01Outlook1
2024-01Teams2
2024-02Outlook1
2024-02Teams2

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT TO_CHAR(DATE_TRUNC('month', usage_date), 'YYYY-MM') AS month, product_name, COUNT(DISTINCT user_id) AS active_users
FROM product_usage
GROUP BY DATE_TRUNC('month', usage_date), product_name;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving