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.

Using the tables `orders`, `order_items` and `restaurants`, 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. Tables: - `orders`: one row per order. Primary key: `order_id`. `restaurant_id` references `restaurants.restaurant_id`. Columns: `order_id` INTEGER (unique order id), `restaurant_id` INTEGER (restaurant the order was placed with), `city` VARCHAR(100) (city recorded on the order), `order_date` DATE (date the order was placed). - `order_items`: one row per item line on an order. No primary key. `order_id` references `orders.order_id`. Columns: `order_id` INTEGER (order the line belongs to), `food_item_id` INTEGER (food item on the line), `quantity` INTEGER (units of the item on this line, positive). - `restaurants`: primary key `restaurant_id`. Columns: `restaurant_id` INTEGER (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. 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.

Tables

orders(order_id INTEGER, restaurant_id INTEGER, city VARCHAR(100), order_date DATE)

order_items(order_id INTEGER, food_item_id INTEGER, quantity INTEGER)

restaurants(restaurant_id INTEGER, city VARCHAR(100))

Hints

  1. 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.
  2. The city filter uses orders.city. The restaurants table is part of the schema, but the query does not need it.
ZomatoData ScientisteasyData Manipulation (SQL/Python)

Top 3 Food Items by Quantity Ordered in New York in the Last 30 Days

Using the tables orders, order_items and restaurants, 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.

Tables:

  • orders: one row per order. Primary key: order_id. restaurant_id references restaurants.restaurant_id. Columns: order_id INTEGER (unique order id), restaurant_id INTEGER (restaurant the order was placed with), city VARCHAR(100) (city recorded on the order), order_date DATE (date the order was placed).
  • order_items: one row per item line on an order. No primary key. order_id references orders.order_id. Columns: order_id INTEGER (order the line belongs to), food_item_id INTEGER (food item on the line), quantity INTEGER (units of the item on this line, positive).
  • restaurants: primary key restaurant_id. Columns: restaurant_id INTEGER (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.

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.

Expected Output

Tables

ordersorder_itemsrestaurants
orders
ColumnType
order_idINTEGER
restaurant_idINTEGER
cityVARCHAR(100)
order_dateDATE
order_items
ColumnType
order_idINTEGER
food_item_idINTEGER
quantityINTEGER
restaurants
ColumnType
restaurant_idINTEGER
cityVARCHAR(100)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions