SQL
Lab
Airbnb Airbnb Interview Question 05

Listings
with No
Bookings

Find listings that have never been booked.

Table Schema

Inspect
Table

interactive
ColumnType
listing_idinteger
host_idinteger
cityvarchar
ColumnType
listing_idinteger
booking_idinteger

Sample Data

Input
Output

Sample Input: listings
listing_idhost_idcity
101201NYC
102202SF
103203LA
104204NYC
Sample Input: bookings
listing_idbooking_id
1011
1022
1013
Expected Output
listing_id
103
104

SQL Editor

Run
Query

postgresql
Waiting for query

listing_id
103
104

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT l.listing_id
FROM listings l
LEFT JOIN bookings b
  ON l.listing_id = b.listing_id
WHERE b.booking_id IS NULL;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving