Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Second-Highest Order Per Restaurant
00:00
5 left

Second-Highest Order Per Restaurant

HardSQL · PostgreSQL

Problem

Write a complex SQL query involving multiple joins, subqueries, and window functions to find the second-highest order value per restaurant on Zomato.

Use delivered orders and calculate each order's value from its menu items and quantities. Return restaurants that have a second-highest distinct value, including all orders tied at that value.

Output

  1. One row per qualifying order, with restaurant_id, restaurant_name, order_id, and order_value.
  2. Sort by restaurant_id, then order_id ascending.

Schema

restaurants
ColumnTypeDescription
restaurant_idPKINTUnique restaurant identifier
restaurant_nameVARCHAR(100)Restaurant display name
cityVARCHAR(80)Restaurant city
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
restaurant_idINTRestaurant receiving the order
statusVARCHAR(20)Order fulfillment status
ordered_atTIMESTAMPOrder creation timestamp
order_items
ColumnTypeDescription
order_item_idPKINTUnique order-line identifier
order_idINTRelated order
menu_item_idINTOrdered menu item
quantityINTNumber of units ordered
menu_items
ColumnTypeDescription
menu_item_idPKINTUnique menu item identifier
restaurant_idINTRestaurant offering the item
item_nameVARCHAR(100)Menu item name
priceNUMERIC(10,2)Current item price
Tablesrestaurantsordersorder_itemsmenu_items
Interviewer

Your question is Second-Highest Order Per Restaurant. Start with the requirements and the four tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.