Below are top 10 Airbnb data queries, divided into three categories: CRUD (Create, Read, Update, Delete), Aggregation, and for convenience, I've also highlighted a special Bonus query that combines elements of both. Note that since we're working with hypothetical data interactions (as actual database access isn't provided here), these queries are written in a generic SQL format, adaptable to most database management systems. Assume the Airbnb data is stored in a database with the following relevant tables:
-
Listings
id (Primary Key)
title
description
price
host_id (Foreign Key referencing Hosts)
location
rating
-
Hosts
id (Primary Key)
name
email
-
Reviews
id (Primary Key)
listing_id (Foreign Key referencing Listings)
review
rating
date
CRUD Queries (Top 3)
1. Create: Add a New Listing
INSERT INTO Listings (title, description, price, host_id, location, rating)
VALUES ('Cozy Beach Apartment', 'Sunny apartment near the beach.', 120.00, 1, 'Miami Beach, FL', 5.0);
Purpose: This query adds a new listing to the Listings table, including essential details like title, description, price, host ID, location, and initial rating.
2. Read: Retrieve All Listings in a Specific Location
SELECT * FROM Listings
WHERE location LIKE '%New York City, NY%';
Purpose: This query fetches all listings located in New York City, NY, by searching for the location string in the Listings table. The LIKE operator with '%' allows for partial matches.
3. Update/Delete:
- Update: Increase Price of Listings in a Specific Location by 10%
UPDATE Listings
SET price = price * 1.10
WHERE location LIKE '%San Francisco, CA%';
Purpose: This query updates the prices of all listings in San Francisco, CA, by increasing them by 10%. This is useful for adjusting prices based on location-specific factors.
- Delete: Remove Listings with Zero Rating
DELETE FROM Listings
WHERE rating = 0;
Purpose: This query removes any listings with a rating of 0, which could indicate inactive, unreviewed, or low-quality listings.
Aggregation Queries (Top 6)
4. Average Price by Location
SELECT location, AVG(price) AS Average_Price
FROM Listings
GROUP BY location
ORDER BY Average_Price DESC;
Purpose: This query calculates the average price of listings in each location and sorts them in descending order. It helps identify the most expensive and affordable locations.
5. Top 5 Hosts with the Most Listings
SELECT H.name, COUNT(L.id) AS Number_of_Listings
FROM Hosts H
JOIN Listings L ON H.id = L.host_id
GROUP BY H.name
ORDER BY Number_of_Listings DESC
LIMIT 5;
Purpose: This query identifies the top 5 hosts with the most listings by joining the Hosts and Listings tables. It's useful for recognizing highly active or successful hosts.
6. Total Revenue by Location (Assuming price is per night and using a 30-day month)
SELECT location, SUM(price * 30) AS Total_Revenue
FROM Listings
GROUP BY location
ORDER BY Total_Revenue DESC;
Purpose: This query estimates the total monthly revenue for listings in each location, assuming a 30-day occupancy. It helps analyze revenue distribution across different areas.
7. Average Rating of Listings by Host
SELECT H.name, AVG(L.rating) AS Average_Rating
FROM Hosts H
JOIN Listings L ON H.id = L.host_id
GROUP BY H.name
ORDER BY Average_Rating DESC;
Purpose: This query calculates the average rating of listings for each host, providing insights into host performance and quality.
8. Count of Listings by Price Range
SELECT
CASE
WHEN price < 50 THEN 'Below $50'
WHEN price BETWEEN 50 AND 100 THEN '$50-$100'
ELSE 'Above $100'
END AS Price_Range,
COUNT(id) AS Number_of_Listings
FROM Listings
GROUP BY
CASE
WHEN price < 50 THEN 'Below $50'
WHEN price BETWEEN 50 AND 100 THEN '$50-$100'
ELSE 'Above $100'
END;
Purpose: This query categorizes listings into three price ranges (<50,50-100,>100) and counts the number of listings in each range. It helps understand the price distribution of listings.
9. Latest Review for Each Listing
SELECT L.id, L.title, R.review, R.date
FROM Listings L
JOIN Reviews R ON L.id = R.listing_id
WHERE (L.id, R.date) IN (
SELECT listing_id, MAX(date)
FROM Reviews
GROUP BY listing_id
);
Purpose: This query retrieves the most recent review for each listing, along with the listing's title and review date. It's useful for getting the latest feedback on each listing.
Bonus Query: Combination of Read and Aggregation
10. Retrieve Listings with Their Average Review Ratings in a Specific Location
SELECT
L.id,
L.title,
L.location,
COALESCE(AVG(R.rating), 0) AS Average_Review_Rating
FROM Listings L
LEFT JOIN Reviews R ON L.id = R.listing_id
WHERE L.location LIKE '%Los Angeles, CA%'
GROUP BY L.id, L.title, L.location
ORDER BY Average_Review_Rating DESC;
Purpose: This bonus query reads specific listings based on location and aggregates their review ratings, showcasing a combined operation. It fetches listings in Los Angeles, CA, calculates the average review rating for each, and sorts them by rating in descending order. The COALESCE function ensures that listings without reviews display an average rating of 0 instead of NULL.
This bonus query combines reading specific data (listings in a particular location) with aggregation (calculating average review ratings), demonstrating how to merge different query types for insightful analysis.