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.

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

Notes

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

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:

  1. 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.
  2. Keep only rn = 1, then GROUP BY restaurant_id: ROUND(AVG(rating), 2) and COUNT(*) now count customers, not rating rows.
  3. 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

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.

Solve this challenge free on PySpark.in