cswork / libraryOpen practice workspace

Coding questions · Meta

Identify Shops with Low Weekly Visibility Rates

This question evaluates a candidate's competence in SQL data manipulation and analytical metrics, including aggregating boolean fields, computing daily and average visibility rates, and applying date-based filters and conditional logic.

SHOP_VISIBILITY

+-----------+---------+------------+---------+
| shop_id   | user_id | view_date  | visible |
+-----------+---------+------------+---------+
| 101       | 555     | 2023-07-01 | true    |
| 102       | 556     | 2023-07-01 | false   |
| 101       | 557     | 2023-07-02 | true    |
| 103       | 560     | 2023-07-02 | true    |
| 102       | 561     | 2023-07-03 | false   |
+-----------+---------+------------+---------+
Scenario

Marketplace team wants to measure how visible each shop is to users over time.

Question

Write an SQL query that calculates each shop’s daily visibility rate (percentage of records where visible = true). Identify shops whose average visibility rate is below 50% during the past 7 days.

Hints

Use CASE expressions, GROUP BY, HAVING, and date filters.

Shop visibility rate over a fixed 7-day window

You are given a SHOP_VISIBILITY table that tracks which shops were shown to which users on specific dates, and whether the shop was actually visible to the user. Using the data for the 7-day period from 2025-05-26 through 2025-06-01 (inclusive), write an SQL query that:

  1. Calculates each shop’s daily visibility rate (the fraction of records per shop per day where visible = true).
  2. Computes, for each shop, the average of these daily visibility rates over that 7-day window.
  3. Returns only the shops whose 7-day average visibility rate is below 0.5 (50%), with columns shop_id and avg_visibility_rate_7d (a number between 0 and 1).

Table SHOP_VISIBILITY

columntypeconstraints
shop_idINTEGER
user_idINTEGER
view_dateDATE
visibleBOOLEAN

Sample data:

shop_iduser_idview_datevisible
1015552025-05-27true
1025562025-05-27false
1015572025-05-28true
1035602025-05-28true
1025612025-05-29false

Expected output

Return one row per shop whose 7-day average of its daily visibility rates (for dates between 2025-05-26 and 2025-06-01) is below 0.5. Include shop_id and the computed avg_visibility_rate_7d as a fraction between 0 and 1. Based on the sample data, shop 102 has a daily visibility rate of 0.0 on each day it appears and thus a 7-day average of 0.0, so it is the only shop in the result.

avg_visibility_rate_7dshop_id
0102

Hints

  1. Filter rows to the fixed 7-day window: view_date between '2025-05-26' and '2025-06-01'.
  2. Compute daily visibility per shop and date using AVG(CASE WHEN visible THEN 1.0 ELSE 0.0 END).
  3. Aggregate those daily visibility rates again per shop to get a 7-day average.
  4. Use HAVING to keep only shops with an average visibility rate below 0.5.