Nights booked per city

dates

Nights booked per city

Airbnb SQL Interview Question

Airbnb's city teams plan local marketing around demand, measured in nights booked. Every booking is for a listing, and every listing is in a city.

Find the total number of nights booked in each city as nights_booked, counting only completed bookings. A stay from June 1 to June 5 is 4 nights. Sort the rows by nights_booked, highest first.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer

bookingsTable16 rows

Column NameType
booking_idBIGINT
listing_idBIGINT
guest_idBIGINT
check_inDATE
check_outDATE
statusVARCHAR

listingsTable10 rows

Column NameType
listing_idBIGINT
host_idBIGINT
cityVARCHAR
room_typeVARCHAR
price_per_nightBIGINT

bookingsExample Input

booking_idlisting_idguest_idcheck_incheck_outstatus
50011012012024-06-012024-06-05completed
50021032022024-06-022024-06-04completed
50031062032024-06-032024-06-10completed
50041022042024-06-052024-06-06canceled
50051042052024-06-072024-06-09completed
50061092062024-06-082024-06-11canceled
50071102072024-06-102024-06-15completed
50081052082024-06-122024-06-13completed
50091072092024-06-142024-06-16completed
50101012102024-06-152024-06-18canceled

listingsExample Input

listing_idhost_idcityroom_typeprice_per_night
10111LisbonEntire home120
10211LisbonPrivate room55
10312ParisEntire home210
10413ParisPrivate room85
10513ParisShared room40
10614TokyoEntire home160
10715TokyoPrivate room70
10816LisbonShared room30
10917TokyoEntire home190
11012ParisEntire home240

Example Output

citynights_booked
Paris10
Tokyo9
Lisbon4

Explanation

Paris has four completed bookings in the example, lasting 2, 2, 5 and 1 nights, which adds up to 10. One Tokyo booking was canceled, so only its 7-night and 2-night stays count, for a total of 9.

The example above is a small slice of the data. Your query runs against the full tables.