Zomato: Top 10 Restaurants by Latest Customer Ratings
SQL coding challenge · Difficulty: medium · Topic: Deduplication with ROW_NUMBER · +100 XP
Problem
*Zomato-style interview practice.*
Zomato wants a Top 10 restaurants list ranked by customer rating. The ratings table keeps every version of every review, so a customer who changed their mind appears more than once. Make sure each customer counts once per restaurant, with their current opinion.
- Customers can edit a review, and every version is kept as its own row. For each restaurant, only a customer's latest rating of that restaurant counts: the row with the latest
rated_at. If the same customer has two ratings of the same restaurant in the same second, the one with the higherrating_idis the later one. - A customer's ratings of different restaurants are separate: rating one restaurant never replaces a rating of another.
avg_ratingis the average of the ratings that count, rounded to 2 decimal places.review_countis the number of customers whose rating counts (not the number of rating rows).- Rank restaurants by
avg_ratingfrom highest to lowest. Break ties byreview_count(more customers first), then byrestaurant_id(A to Z). - Return the top 10 restaurants. If there are fewer than 10, return them all.
Return restaurant_id, avg_rating and review_count (the grader checks which rows you return, not their order, but the top 10 cut must follow the ranking above).
Schema
Table: ratings
| Column | Type | Meaning | | --- | --- | --- | | rating_id | INT | Unique id of this rating row; a later save gets a higher id | | restaurant_id | VARCHAR | The restaurant being rated | | user_id | VARCHAR | The customer who rated it | | rating | DECIMAL(2,1) | 1.0 to 5.0 | | rated_at | DATETIME | When this version of the review was saved |
Tables
Table: ratings
| rating_id | restaurant_id | user_id | rating | rated_at | | --- | --- | --- | --- | --- | | 1 | R101 | U1 | 4.0 | 2025-05-01 10:00:00 | | 7 | R102 | U4 | 4.5 | 2025-05-01 13:00:00 | | 2 | R101 | U2 | 4.5 | 2025-05-01 11:30:00 | | 3 | R101 | U3 | 4.0 | 2025-05-02 09:15:00 | | 6 | R102 | U1 | 3.5 | 2025-05-01 12:00:00 | | 4 | R101 | U1 | 5.0 | 2025-05-03 09:00:00 | | 8 | R103 | U5 | 4.8 | 2025-05-02 12:00:00 | | 9 | R103 | U5 | 3.0 | 2025-05-02 12:00:00 | | 10 | R103 | U6 | 4.4 | 2025-05-02 18:45:00 | | 11 | R104 | U2 | 4.5 | 2025-05-02 20:00:00 | | 12 | R104 | U7 | 4.5 | 2025-05-03 08:30:00 | | 13 | R105 | U8 | 4.0 | 2025-05-03 10:10:00 |
Expected Output
| restaurant_id | avg_rating | review_count | | --- | --- | --- | | R101 | 4.50 | 3 | | R104 | 4.50 | 2 | | R102 | 4.00 | 2 | | R105 | 4.00 | 1 | | R103 | 3.70 | 2 |
Explanation
- R101: U1 rated it 4.0, then edited the review to 5.0 two days later, so only 5.0 counts. With U2's 4.5 and U3's 4.0 the average is (5.0 + 4.5 + 4.0) / 3 = 4.50 from 3 customers. Counting both of U1's versions would give 4.38 from 4 rows.
- R102: U1's rating of R102 still counts. Their later rating of R101 is a different restaurant: (3.5 + 4.5) / 2 = 4.00.
- R103: U5 saved two ratings at exactly 12:00:00. Rating 9 has the higher
rating_id, so it is the later one and 3.0 counts, not 4.8: (3.0 + 4.4) / 2 = 3.70. - Ties: R101 and R104 both average 4.50; R101 has more customers (3 against 2), so it ranks first. R102 and R105 both average 4.00; R102 (2 customers) comes before R105 (1).
- There are only 5 restaurants here, so all 5 are returned.
Notes
- Column names must be exactly
restaurant_id,avg_rating,review_count. - The query runs on MySQL 8, which supports window functions.
What this SQL challenge teaches you
“Zomato: Top 10 Restaurants by Latest Customer Ratings” is a medium-level SQL challenge focused on Deduplication with ROW_NUMBER. Working through it gives you hands-on practice with ROW_NUMBER, Deduplication, Latest record per group, AVG, Top-N with tie-breakers, Food Delivery — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- ROW_NUMBER
- Deduplication
- Latest record per group
- AVG
- Top-N with tie-breakers
- Food Delivery
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- ROW_NUMBER() OVER (PARTITION BY restaurant_id, user_id ORDER BY rated_at DESC, rating_id DESC) numbers each customer's ratings of a restaurant, newest first.
- Keep only rn = 1, then GROUP BY restaurant_id: ROUND(AVG(rating), 2) and COUNT(*) now count customers, not rating rows.
- ORDER BY avg_rating DESC, review_count DESC, restaurant_id, then LIMIT 10.
Where this comes up
Variations of this problem have been reported in interviews at Zomato. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Deduplicate User Login Sessions
- Remove Duplicate Customer Records
- Remove Duplicate Customer Records (PySpark)
- Zomato: Top 10 Restaurants by Latest Customer Ratings (PySpark)
- Top 3 Products per Category
- Running Total Revenue
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Deduplication with ROW_NUMBER.