Delivery SLA by Meal Band

SQL practice question · Hard · Date & Time · Swiggy

Classify each order by the hour of `placed_at`: hours 12-14 -> 'Lunch', hours 19-22 -> 'Dinner', anything else -> 'Off-peak'. An order is on time if it was delivered (`delivered_at` not NULL) and the minutes from placed_at to delivered_at are <= `promised_min`. Undelivered orders are late. Return `band`, `total_orders`, `on_time_orders`, `on_time_pct` (ROUND 1), `avg_delay_min` = average of (actual minutes - promised_min) over LATE orders that WERE delivered (ROUND 1; NULL if there are none). Order by `on_time_pct` DESC, then `band`.

Table: deliveries

Schema of deliveries
ColumnType
order_idINTEGER
restaurant_idINTEGER
placed_atTIMESTAMP
delivered_atTIMESTAMP
promised_minINTEGER
Sample rows of deliveries
order_idrestaurant_idplaced_atdelivered_atpromised_min
112026-08-01 12:10:002026-08-01 12:40:0030
212026-08-01 13:00:002026-08-01 13:50:0035
322026-08-01 14:59:002026-08-01 15:20:0030
422026-08-01 19:00:002026-08-01 19:45:0040
532026-08-01 20:00:00NULL30
632026-08-01 22:30:002026-08-01 22:55:0030
712026-08-01 16:00:002026-08-01 16:20:0025
822026-08-01 11:59:002026-08-01 12:30:0030
Expected output on the sample data
bandtotal_orderson_time_orderson_time_pctavg_delay_min
Lunch3266.715
Off-peak21501
Dinner3133.35

Solve it in the live editor on this page — run on sample data, then submit against hidden tests.

‹ Ordered Checkout Funnel by City · De-duplicate Burst Reviews ›

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Question Asked in Interview Blog
SQL Interview Practice

Company-wise SQL questions

{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.

🏆 Weekly SQL Contest
{{ totalCount }}
Questions
{{ companyCount }}
Companies
{{ prog.total.solved }}
Solved
Bookmark Question Companies Difficulty Category Status
{{ r.n }} - {{ r.title }} {{ c }} {{ r.difficulty }} {{ r.category }} {{ r.statusIcon }}
No questions match these filters.
Showing {{ rangeLabel }} of {{ filteredCount }}

Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.

← All questions

{{ cur.n }} - {{ cur.title }}

🏆 Contest {{ contestWeek }}
{{ cur.catLabel }} {{ cur.diffLabel }} {{ c }}
Embed this questionPaste on a blog, course page or LMS. Free to use — educator guide
{{ embedCode }}

{{ cur.statement }}

Table: {{ tb.name }}
ColumnType {{ c.name }}{{ c.type }}
{{ h }} {{ v.text }}
Expected output on the sample data:
{{ h }} {{ v.text }}
  • {{ h }}

No submissions yet. Press Evaluate to submit your query.

{{ s.verdict }} {{ s.meta }}
The solution unlocks after you solve this question or after 3 evaluations that didn't pass ({{ failCount }}/3 so far). Trying first is the fastest way to learn.
{{ solutionSql }}
  1. {{ e }}
⏱ {{ timerLabel }}
{{ fnHelpTitle }}
{{ fnHelpText }}
Writing {{ dialectName }} · Ctrl/Cmd + Enter runs the query · Tab indents · your code is saved automatically
Output {{ out.status }}
  • {{ w }}
{{ out.verdict }}
{{ out.detail }}
{{ out.error }}
Expected (sample data)
{{ h }}{{ v.text }}
Your output (sample data)
{{ h }}{{ v.text }}
{{ h }} {{ v.text }}
Run your query to see the result here.
🔒

To Track Your Progress, Log In / Sign Up

You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.

Sign Up Free Log In