Find users who used a product more than 4 times in a month and rated it 4 or higher on average

Quick Overview

Write a SQL query over users, products, usage logs and reviews that returns each user, product and month with more than 4 uses where the user's average rating of the product is at least 4. It tests monthly grouping, HAVING filters, rounding, and avoiding join fan-out when combining two one-to-many tables.

Find users who used a product more than 4 times in a month and rated it 4 or higher on average

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Find active users: users who used a product more than 4 times in a calendar month and whose average rating of that product is 4 or higher. Return one row for each qualifying combination of user, product and month, with the user's name, the product's name, the number of uses in that month and the user's average rating of the product. ```hint Check the row count Before joining everything at once, work out how many joined rows you get for a user with three reviews and five usage records of the same product. ``` ### Input Table(s) `users` | Column | Type | Meaning | |---|---|---| | user_id | INT | User identifier. Primary key. | | username | VARCHAR(100) | Display name. NOT NULL. | `products` | Column | Type | Meaning | |---|---|---| | product_id | INT | Product identifier. Primary key. | | product_name | VARCHAR(100) | Product name. NOT NULL. | `usage_logs` | Column | Type | Meaning | |---|---|---| | usage_id | INT | One use of a product. Primary key. | | user_id | INT | The user. Foreign key to `users.user_id`. | | product_id | INT | The product used. Foreign key to `products.product_id`. | | usage_date | DATE | Date of the use. NOT NULL. | `reviews` | Column | Type | Meaning | |---|---|---| | review_id | INT | One review. Primary key. | | user_id | INT | The reviewer. Foreign key to `users.user_id`. | | product_id | INT | The product reviewed. Foreign key to `products.product_id`. | | rating | DECIMAL(2,1) | Rating from 1.0 to 5.0. NOT NULL. | ### Output Contract - Columns, in this order: `user_id`, `username`, `product_id`, `product_name`, `usage_month`, `usage_count`, `avg_rating`. - `usage_month` is the month of `usage_date` as text in `YYYY-MM` form, for example `2024-03`. - `usage_count` is the number of `usage_logs` rows for that user, product and month. - `avg_rating` is the average of all of the user's ratings of that product, rounded to 2 decimals. - Order by `user_id`, `product_id`, `usage_month`, all ascending. Each such triple appears at most once. ### Example `users` | user_id | username | |---|---| | 1 | ana | | 2 | ben | `products` | product_id | product_name | |---|---| | 10 | Photo Editor | | 20 | Video Calls | `usage_logs` | usage_id | user_id | product_id | usage_date | |---|---|---|---| | 1 | 1 | 10 | 2024-03-01 | | 2 | 1 | 10 | 2024-03-05 | | 3 | 1 | 10 | 2024-03-12 | | 4 | 1 | 10 | 2024-03-20 | | 5 | 1 | 10 | 2024-03-31 | | 6 | 1 | 10 | 2024-04-01 | | 7 | 1 | 10 | 2024-04-02 | | 8 | 1 | 10 | 2024-04-15 | | 9 | 1 | 10 | 2024-04-30 | | 10 | 2 | 20 | 2024-03-02 | | 11 | 2 | 20 | 2024-03-03 | | 12 | 2 | 20 | 2024-03-09 | | 13 | 2 | 20 | 2024-03-16 | | 14 | 2 | 20 | 2024-03-23 | `reviews` | review_id | user_id | product_id | rating | |---|---|---|---| | 1 | 1 | 10 | 4.5 | | 2 | 1 | 10 | 4.0 | | 3 | 1 | 10 | 4.2 | | 4 | 2 | 20 | 4.5 | | 5 | 2 | 20 | 3.5 | Expected result: | user_id | username | product_id | product_name | usage_month | usage_count | avg_rating | |---|---|---|---|---|---|---| | 1 | ana | 10 | Photo Editor | 2024-03 | 5 | 4.23 | | 2 | ben | 20 | Video Calls | 2024-03 | 5 | 4.00 | Ana used Photo Editor 5 times in March, which is more than 4, and her three ratings average 12.7 / 3 = 4.2333, shown as 4.23. Her 4 uses in April do not pass the threshold, so April has no row. Ben's two ratings average exactly 4.0, which meets "4 or higher". ### Constraints and Clarifications - "More than 4 times" means a `usage_count` of 5 or more. Each `usage_logs` row is one use, including rows on the same day. - Reviews have no date. The rating condition uses every review the user wrote for the product, and the same `avg_rating` appears on each qualifying month for that user and product. - The threshold applies to the unrounded average. An average of 3.996 does not qualify even though it would display as 4.00. - A user who never reviewed the product has no average rating and does not qualify. - Months are calendar months. A user, product and month with no usage rows produces no row. - Row limits: `users` up to 10,000, `products` up to 1,000, `usage_logs` up to 100,000, `reviews` up to 10,000. - Each `usage_id` and `review_id` is unique.

Overview: Write a SQL query over users, products, usage logs and reviews that returns each user, product and month with more than 4 uses where the user's average rating of the product is at least 4. It tests monthly grouping, HAVING filters, rounding, and avoiding join fan-out when combining two one-to-many tables.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Find active users: users who used a product more than 4 times in a calendar month and whose average rating of that product is 4 or higher. Return one row for each qualifying combination of user, product and month, with the user's name, the product's name, the number of uses in that month and the user's average rating of the product.

Input Table(s)

users

ColumnTypeMeaning
user_idINTUser identifier. Primary key.
usernameVARCHAR(100)Display name. NOT NULL.

products

ColumnTypeMeaning
product_idINTProduct identifier. Primary key.
product_nameVARCHAR(100)Product name. NOT NULL.

usage_logs

ColumnTypeMeaning
usage_idINTOne use of a product. Primary key.
user_idINTThe user. Foreign key to users.user_id.
product_idINTThe product used. Foreign key to products.product_id.
usage_dateDATEDate of the use. NOT NULL.

reviews

ColumnTypeMeaning
review_idINTOne review. Primary key.
user_idINTThe reviewer. Foreign key to users.user_id.
product_idINTThe product reviewed. Foreign key to products.product_id.
ratingDECIMAL(2,1)Rating from 1.0 to 5.0. NOT NULL.

Output Contract

  • Columns, in this order: user_id , username , product_id , product_name , usage_month , usage_count , avg_rating .
  • usage_month is the month of usage_date as text in YYYY-MM form, for example 2024-03 .
  • usage_count is the number of usage_logs rows for that user, product and month.
  • avg_rating is the average of all of the user's ratings of that product, rounded to 2 decimals.
  • Order by user_id , product_id , usage_month , all ascending. Each such triple appears at most once.

Example

users

user_idusername
1ana
2ben

products

product_idproduct_name
10Photo Editor
20Video Calls

usage_logs

usage_iduser_idproduct_idusage_date
11102024-03-01
21102024-03-05
31102024-03-12
41102024-03-20
51102024-03-31
61102024-04-01
71102024-04-02
81102024-04-15
91102024-04-30
102202024-03-02
112202024-03-03
122202024-03-09
132202024-03-16
142202024-03-23

reviews

review_iduser_idproduct_idrating
11104.5
21104.0
31104.2
42204.5
52203.5

Expected result:

user_idusernameproduct_idproduct_nameusage_monthusage_countavg_rating
1ana10Photo Editor2024-0354.23
2ben20Video Calls2024-0354.00

Ana used Photo Editor 5 times in March, which is more than 4, and her three ratings average 12.7 / 3 = 4.2333, shown as 4.23. Her 4 uses in April do not pass the threshold, so April has no row. Ben's two ratings average exactly 4.0, which meets "4 or higher".

Constraints and Clarifications

  • "More than 4 times" means a usage_count of 5 or more. Each usage_logs row is one use, including rows on the same day.
  • Reviews have no date. The rating condition uses every review the user wrote for the product, and the same avg_rating appears on each qualifying month for that user and product.
  • The threshold applies to the unrounded average. An average of 3.996 does not qualify even though it would display as 4.00.
  • A user who never reviewed the product has no average rating and does not qualify.
  • Months are calendar months. A user, product and month with no usage rows produces no row.
  • Row limits: users up to 10,000, products up to 1,000, usage_logs up to 100,000, reviews up to 10,000.
  • Each usage_id and review_id is unique.