Guest Booking History
Return user_id, country, booking_id, listing_id, and nights for all guests who have bookings, ordered by user_id then booking_id.
- Joins
- Sorting
Challenge brief
Understand the request
Customer Experience Customer Experience needs a unified view of each guest's country and booking details to prepare personalised support responses.
Show each guest's bookings with their country, listing, and nights.
Return
- user_id
- country
- booking_id
- listing_id
- nights
Constraints
- Include bookings with a matching registered guest
- Return one row per booking
- Order by user ID, then booking ID
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
users
user_idINTEGERcountryVARCHAR(50)
bookings
user_idINTEGERbooking_idINTEGERlisting_idINTEGERnightsINTEGER
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Guest country and booking details live in related tables.
Hint 2
Connect each booking to its user key.
Hint 3
Order first by guest and then by booking.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT u.user_id, u.country, b.booking_id, b.listing_id, b.nights
FROM users u
JOIN bookings b ON u.user_id = b.user_id
ORDER BY u.user_id, b.booking_idWhy this works
country lives in users; booking details live in bookings. INNER JOIN on user_id combines them. All 8 users have at least one booking so no rows are lost.
Success check
Returns each booking at booking grain with its guest country
Expected result
Use this output to verify values, aliases, ordering, and row count.
| user_id | country | booking_id | listing_id | nights |
|---|---|---|---|---|
| 1 | US | 8 | 5 | 1 |
| 2 | IN | 1 | 1 | 3 |
| 2 | IN | 9 | 3 | 2 |
| 2 | IN | 11 | 1 | 4 |
| 3 | UK | 2 | 2 | 5 |
| 3 | UK | 10 | 1 | 3 |
| 4 | CA | 3 | 3 | 2 |
| 5 | US | 4 | 4 | 4 |
| 6 | FR | 5 | 5 | 3 |
| 7 | US | 6 | 1 | 2 |
Previewing 10 of 11 expected rows. Run the query in the editor to inspect the full result.
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Explore related company challenges
Uber
Independent Uber-style mobility marketplace SQL practice covering trips, drivers, riders, pricing, payments, and promotions.
Amazon
Independent Amazon-style e-commerce, warehouse, inventory, and customer analytics SQL practice.
Meta
Independent Meta-style social-product, engagement, content, community, messaging, and advertising SQL practice.
Return to the complete interview preparation experience.