← Practice

Самые ночные клиенты в каждом городе

Questions like this come up in interviews at Wildberries

Level: MiddleTopic: Top-N per group100 XP

Instructions

Ночной заказ — доставка, созданная (created_at) с 22:00 до 06:00: час created_at не меньше 22 или меньше 6. Для каждого клиента посчитайте night_cnt — число его ночных заказов (любого статуса). В каждом городе клиента (customers.city) выберите 3 клиентов с наибольшим night_cnt; при равенстве выше стоит клиент с меньшим id. Выведите city, customer_id, night_cnt и place (1–3). Порядок строк не важен.

not translated yet

Hints

Each next one is more detailed and costs more

Log in to open hints

Query

Result

Click Run to see the query result, or Check to submit your solution.

Dataset schema

deliveries.restaurant_id (0…N) ↔ restaurants.id (1…1)deliveries.customer_id (0…N) ↔ customers.id (1…1)deliveries.courier_id (0…N) ↔ couriers.id (0…1)couriersPKidintegercustomersPKidintegerdeliveriesPKidintegerFKrestaurant_idintegerFKcustomer_idintegerFKcourier_idintegerrestaurantsPKidinteger
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-onedeliveries.restaurant_id → restaurants.id
  • N:1Many-to-onedeliveries.customer_id → customers.id
  • N:1Many-to-onedeliveries.courier_id → couriers.id

Table data

PostgreSQL

Select a table in the diagram or list to view its rows.

Dataset schema