The orders table has one row per order and the reviews table has one row per customer review. Find restaurants whose total orders dropped by 30% or more from one week to the next while their overall average rating is strictly above 4.5. Return one row per qualifying drop with the restaurant, the later week of the comparison, the order counts of both weeks and the restaurant's average rating.
Input Table(s)
Table orders, primary key order_id:
| Column | Type | Meaning |
|---|
| order_id | INT | Order identifier |
| restaurant_id | INT | Restaurant that received the order |
| order_date | DATE | Date the order was placed |
Table reviews, primary key review_id:
| Column | Type | Meaning |
|---|
| review_id | INT | Review identifier |
| restaurant_id | INT | Restaurant that was reviewed |
| rating | DECIMAL(3,2) | Score from 1.00 to 5.00 |
Reviews carry no date, so a restaurant's rating is the average over all of its reviews.
Output Contract
| Column | Meaning |
|---|
| restaurant_id | Restaurant with the drop |
| week | Later week of the comparison, written as ISO year times 100 plus ISO week number (ISO week 2 of 2024 is 202402) |
| previous_week_orders | Orders in the restaurant's previous week that has orders |
| current_week_orders | Orders in week |
| avg_rating | Average of all the restaurant's ratings, rounded to 2 decimals |
Return one row per qualifying (restaurant_id, week) pair, sorted by restaurant_id ascending and then week ascending. The pair is unique, so the order is fully determined.
Example
orders:
| order_id | restaurant_id | order_date |
|---|
| 1 | 101 | 2024-01-01 |
| 2 | 101 | 2024-01-03 |
| 3 | 101 | 2024-01-07 |
| 4 | 101 | 2024-01-08 |
| 5 | 101 | 2024-01-12 |
| 6 | 101 | 2024-01-15 |
| 7 | 101 | 2024-01-20 |
| 8 | 102 | 2024-01-02 |
| 9 | 102 | 2024-01-05 |
| 10 | 102 | 2024-01-09 |
| 11 | 103 | 2024-01-04 |
| 12 | 103 | 2024-01-06 |
| 13 | 103 | 2024-01-17 |
reviews:
| review_id | restaurant_id | rating |
|---|
| 1 | 101 | 4.80 |
| 2 | 101 | 4.60 |
| 3 | 102 | 4.90 |
| 4 | 102 | 4.10 |
| 5 | 103 | 5.00 |
Expected output:
| restaurant_id | week | previous_week_orders | current_week_orders | avg_rating |
|---|
| 101 | 202402 | 3 | 2 | 4.70 |
| 103 | 202403 | 2 | 1 | 5.00 |
ISO week 202401 runs from Monday 2024-01-01 to Sunday 2024-01-07, so order 3 still belongs to it. Restaurant 101 went from 3 orders in week 202401 to 2 in week 202402, a drop of 33.3%, and its average rating is (4.80 + 4.60) / 2 = 4.70. Its next comparison, 2 orders to 2, is no drop. Restaurant 102 fell from 2 orders to 1, but its average rating is exactly 4.50, which is not strictly above 4.5. Restaurant 103 had no orders in week 202402, so week 202403 (1 order) is compared with week 202401 (2 orders), a 50% drop, and its rating is 5.00.
Constraints and Clarifications
-
Each table holds 1 to 100,000 rows.
restaurant_id
and
rating
are never NULL, and
rating
lies between 1.00 and 5.00.
-
Weeks follow ISO 8601: they start on Monday, and the ISO year can differ from the calendar year near January 1. For example, 2024-12-30 falls in week 202501.
-
The weeks of a restaurant are the weeks in which it has at least one order. Each week is compared with the restaurant's previous week that has orders (a
LAG
over its ordered weeks), even when empty weeks sit between them. A restaurant's first week has nothing to compare with and produces no row.
-
A drop qualifies when the current week has at most 70% of the previous week's orders, so a drop of exactly 30% counts.
-
The rating filter uses the unrounded average. An average of exactly 4.50 fails, and an average of 4.5033 passes even though it displays as 4.50.
-
A restaurant with no reviews has no average rating and is left out.