Top 3 Most Ordered Food Items in New York (Last 30 Days)

Quick Overview

A SQL question that joins orders to their line items, filters by city and a fixed date window, and returns the 3 food items with the most units ordered. It tests where filters belong in a join, inclusive date bounds, case-insensitive matching, and a top-N ordering with a tiebreaker.

Top 3 Most Ordered Food Items in New York (Last 30 Days)

Company: Zomato

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Using the tables below, find the top 3 food items by total quantity ordered in the city `'New York'` in the last 30 days. Return `food_item_id` and `total_quantity`. The reference date is `2024-03-31`. Include orders with `order_date` between `2024-03-01` and `2024-03-31`, both ends included. Order the result by `total_quantity` descending, with ties broken by `food_item_id` ascending. ```hint Where the filters belong The city and the date are recorded on the order, and the quantity is recorded on the item rows. Work out which rows survive the filters before you add anything up. ``` ### Input Table(s) Table `Orders`, one row per order. Primary key: `order_id`. `restaurant_id` references `Restaurants.restaurant_id`. | Column | Type | Meaning | |---|---|---| | order_id | INT | Unique order id | | restaurant_id | INT | Restaurant the order was placed with | | city | VARCHAR(100) | City recorded on the order | | order_date | DATE | Date the order was placed | Table `Order_Items`, one row per item line on an order. No primary key. `order_id` references `Orders.order_id`. | Column | Type | Meaning | |---|---|---| | order_id | INT | Order the line belongs to | | food_item_id | INT | Food item on the line | | quantity | INT | Units of the item on this line, positive | Table `Restaurants`. Primary key: `restaurant_id`. | Column | Type | Meaning | |---|---|---| | restaurant_id | INT | Unique restaurant id | | city | VARCHAR(100) | City of the restaurant | ### Output Contract - Columns, in order: `food_item_id`, `total_quantity`. - `total_quantity` is the sum of `quantity` over all `Order_Items` rows whose order is in New York and dated from `2024-03-01` to `2024-03-31` inclusive. It is an integer. - Rows are ordered by `total_quantity` descending, then `food_item_id` ascending. - Return at most 3 rows. ### Example Input `Orders`: | order_id | restaurant_id | city | order_date | |---|---|---|---| | 1 | 10 | New York | 2024-03-01 | | 2 | 11 | New York | 2024-03-15 | | 3 | 10 | New York | 2024-02-29 | | 4 | 12 | Boston | 2024-03-20 | | 5 | 11 | new york | 2024-03-31 | Input `Order_Items`: | order_id | food_item_id | quantity | |---|---|---| | 1 | 201 | 2 | | 1 | 202 | 1 | | 2 | 201 | 3 | | 2 | 203 | 4 | | 3 | 202 | 10 | | 4 | 204 | 8 | | 5 | 202 | 3 | | 5 | 205 | 4 | Input `Restaurants`: | restaurant_id | city | |---|---| | 10 | New York | | 11 | New York | | 12 | Boston | Expected output: | food_item_id | total_quantity | |---|---| | 201 | 5 | | 202 | 4 | | 203 | 4 | Order 3 is dated `2024-02-29`, before the window, and order 4 is in Boston, so neither counts. Order 5 is stored as `'new york'` and still counts because the city match ignores case. Over orders 1, 2 and 5, item 201 totals 2 + 3 = 5, and items 202 (1 + 3), 203 and 205 each total 4. The three-way tie for second place is settled by `food_item_id`, so 202 and 203 make the list and 205 does not. ### Constraints and Clarifications - `Orders` and `Order_Items` have 1 to 100,000 rows each. `Restaurants` has 1 to 10,000 rows. - `quantity` is a positive integer and `order_date` is a valid DATE. - The city filter uses `Orders.city`. `Restaurants` is part of the schema, but the query does not need it. - Match the city without regard to case, so `'New York'` and `'new york'` both count. Orders with a NULL city are excluded. - The window is the reference date minus 30 days (`2024-03-01`) through the reference date, both ends included. - One food item can appear on many `Order_Items` rows, in the same order or in different orders. Sum all of them. - `Order_Items` rows whose `order_id` has no match in `Orders` are ignored. - When items tie on `total_quantity`, the smaller `food_item_id` ranks higher, so a tie at third place still yields exactly 3 rows. If fewer than 3 items qualify, return only those.

Overview: A SQL question that joins orders to their line items, filters by city and a fixed date window, and returns the 3 food items with the most units ordered. It tests where filters belong in a join, inclusive date bounds, case-insensitive matching, and a top-N ordering with a tiebreaker.

Zomato logo
Zomato
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Using the tables below, find the top 3 food items by total quantity ordered in the city 'New York' in the last 30 days. Return food_item_id and total_quantity.

The reference date is 2024-03-31. Include orders with order_date between 2024-03-01 and 2024-03-31, both ends included. Order the result by total_quantity descending, with ties broken by food_item_id ascending.

Input Table(s)

Table Orders, one row per order. Primary key: order_id. restaurant_id references Restaurants.restaurant_id.

ColumnTypeMeaning
order_idINTUnique order id
restaurant_idINTRestaurant the order was placed with
cityVARCHAR(100)City recorded on the order
order_dateDATEDate the order was placed

Table Order_Items, one row per item line on an order. No primary key. order_id references Orders.order_id.

ColumnTypeMeaning
order_idINTOrder the line belongs to
food_item_idINTFood item on the line
quantityINTUnits of the item on this line, positive

Table Restaurants. Primary key: restaurant_id.

ColumnTypeMeaning
restaurant_idINTUnique restaurant id
cityVARCHAR(100)City of the restaurant

Output Contract

  • Columns, in order: food_item_id , total_quantity .
  • total_quantity is the sum of quantity over all Order_Items rows whose order is in New York and dated from 2024-03-01 to 2024-03-31 inclusive. It is an integer.
  • Rows are ordered by total_quantity descending, then food_item_id ascending.
  • Return at most 3 rows.

Example

Input Orders:

order_idrestaurant_idcityorder_date
110New York2024-03-01
211New York2024-03-15
310New York2024-02-29
412Boston2024-03-20
511new york2024-03-31

Input Order_Items:

order_idfood_item_idquantity
12012
12021
22013
22034
320210
42048
52023
52054

Input Restaurants:

restaurant_idcity
10New York
11New York
12Boston

Expected output:

food_item_idtotal_quantity
2015
2024
2034

Order 3 is dated 2024-02-29, before the window, and order 4 is in Boston, so neither counts. Order 5 is stored as 'new york' and still counts because the city match ignores case. Over orders 1, 2 and 5, item 201 totals 2 + 3 = 5, and items 202 (1 + 3), 203 and 205 each total 4. The three-way tie for second place is settled by food_item_id, so 202 and 203 make the list and 205 does not.

Constraints and Clarifications

  • Orders and Order_Items have 1 to 100,000 rows each. Restaurants has 1 to 10,000 rows.
  • quantity is a positive integer and order_date is a valid DATE.
  • The city filter uses Orders.city . Restaurants is part of the schema, but the query does not need it.
  • Match the city without regard to case, so 'New York' and 'new york' both count. Orders with a NULL city are excluded.
  • The window is the reference date minus 30 days ( 2024-03-01 ) through the reference date, both ends included.
  • One food item can appear on many Order_Items rows, in the same order or in different orders. Sum all of them.
  • Order_Items rows whose order_id has no match in Orders are ignored.
  • When items tie on total_quantity , the smaller food_item_id ranks higher, so a tie at third place still yields exactly 3 rows. If fewer than 3 items qualify, return only those.