Table Schema
Inspect
Table
| Column | Type |
|---|---|
| listing_id | integer |
| host_id | integer |
| city | varchar |
| Column | Type |
|---|---|
| listing_id | integer |
| booking_id | integer |
Find listings that have never been booked.
Table Schema
| Column | Type |
|---|---|
| listing_id | integer |
| host_id | integer |
| city | varchar |
| Column | Type |
|---|---|
| listing_id | integer |
| booking_id | integer |
Sample Data
| listing_id | host_id | city |
|---|---|---|
| 101 | 201 | NYC |
| 102 | 202 | SF |
| 103 | 203 | LA |
| 104 | 204 | NYC |
| listing_id | booking_id |
|---|---|
| 101 | 1 |
| 102 | 2 |
| 101 | 3 |
| listing_id |
|---|
| 103 |
| 104 |
SQL Editor
| listing_id |
|---|
| 103 |
| 104 |
Hints
Solution
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
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions