cswork / libraryOpen practice workspace

Coding questions · Amazon

Compute unique visitors by department

This question evaluates proficiency in SQL data manipulation, specifically JOIN operations between event and product tables, deduplicating distinct users, applying time-range filters, and reasoning about hierarchical product attributes (department/category/subcategory).

You have two tables: products(product_id, department, category, subcategory) where department > category > subcategory form a parent–child hierarchy, and click_log(user_id, product_id, event_time). For any given department name as input, write SQL to return the number of distinct users who clicked products in that department over a specified time range. Ensure that a user who clicks multiple products within the same department is counted once, and extend your query to return counts for all departments in one result.

Compute unique visitors by department over a date range

You have two tables:

  1. products(product_id, department, category, subcategory) where department > category > subcategory form a parent–child hierarchy.
  2. click_log(user_id, product_id, event_time).

For the fixed date range FROM 2025-05-01 TO 2025-05-31 (inclusive), write SQL to return, for each department, the number of distinct users who clicked products in that department.

A user who clicks multiple products within the same department must be counted only once for that department in the date range. A user who clicks products in multiple departments should be counted once in each of those departments. The final result should have one row per department with its distinct user count for that period.

(Assume that in a real system, this query could be filtered for a single department by adding a WHERE clause on department, but your answer here should produce counts for all departments.)

Table products

columntypeconstraints
product_idINTPRIMARY KEY, NOT NULL
departmentVARCHAR(50)NOT NULL
categoryVARCHAR(50)NOT NULL
subcategoryVARCHAR(50)NOT NULL

Sample data:

categorydepartmentproduct_idsubcategory
PhonesElectronics1Smartphones
PhonesElectronics2Feature Phones
ComputersElectronics3Laptops
KitchenHome4Cookware
FurnitureHome5Chairs
MenClothing6Shirts
WomenClothing7Dresses

Table click_log

columntypeconstraints
user_idINTNOT NULL
product_idINTNOT NULL
event_timeTIMESTAMPNOT NULL

Sample data:

event_timeproduct_iduser_id
2025-05-05 10:00:001101
2025-05-06 11:00:003101
2025-05-07 09:30:002102
2025-05-08 14:15:004102
2025-05-09 16:45:004103
2025-05-10 12:00:005103
2025-05-11 13:20:001103
2025-05-12 08:05:006104
2025-05-13 18:30:007104
2025-06-02 10:00:006105
2025-04-30 09:00:002106
2025-05-15 17:10:004101
2025-05-16 11:25:005107

Expected output

One row per department, showing the department name and the count of distinct users who clicked any product in that department between 2025-05-01 and 2025-05-31 inclusive. Users who clicked multiple products in the same department are counted once for that department.

departmentdistinct_user_count
Clothing1
Electronics3
Home4

Hints

  1. Join click_log to products using product_id to know which department each click belongs to.
  2. Use COUNT(DISTINCT user_id) grouped by department, with a WHERE clause limiting event_time to the given date range.