0% found this document useful (0 votes)
22 views1 page

Data Science

The document contains SQL queries for analyzing Airbnb listings, including filtering, aggregating, and sorting data based on room numbers and city locations. It provides examples of how to retrieve specific data, such as listings with missing room numbers, unique cities, and average room counts per country. Additionally, it highlights the importance of SQL as a structured query language used for database management.

Uploaded by

luna04anvi20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views1 page

Data Science

The document contains SQL queries for analyzing Airbnb listings, including filtering, aggregating, and sorting data based on room numbers and city locations. It provides examples of how to retrieve specific data, such as listings with missing room numbers, unique cities, and average room counts per country. Additionally, it highlights the importance of SQL as a structured query language used for database management.

Uploaded by

luna04anvi20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

5.

Get the listing id, city, ordered by the number_of_rooms in descending order Filtering on missing data
SELECT id, city

SQL for Data Science


FROM airbnb_listings

ORDER BY number_of_rooms DESC; R


12. eturn the listings where number_of_rooms is missing

6. Get the first 5 rows from the airbnb_listings table SELECT *

FROM airbnb_listings

SQL Basics Cheat Sheet


SELECT *
WHERE number_of_rooms IS NULL;
FROM airbnb_listings

LIMIT 5; R
13. eturn the listings where number_of_rooms is not missing

7. Get a unique list of cities where there are listings SELECT *

Learn SQL online at www.DataCamp.com SELECT DISTINCT city

FROM airbnb_listings

WHERE number_of_rooms IS NOT NULL;


FROM airbnb_lisitings;

What is SQL?
> Filtering Data > Aggregating Data
Filtering on numeric columns Simple aggregations
SQL stands for “structured query language”. It is a language used to query,
analyze, and manipulate data from databases. Today, SQL is one of the most b v b
1. Get the total num er of rooms a aila le across all listings
1. Get all the listings where number_of_rooms is more or equal to 3
widely used tools in data.
SELECT SUM(number_of_rooms)

SELECT *
FROM airbnb_listings;
FROM airbnb_listings

WHERE number_of_rooms >= 3; 2. Get the average number of rooms per listing across all listings
SELECT AVG(number_of_rooms)

> The different dialects of SQL 2. Get all the listings where number_of_rooms is more than 3
SELECT *

FROM airbnb_listings;
FROM airbnb_listings
3. Get the listing with the highest number of rooms across all listings
Although SQL languages all share a basic structure, some of the specific WHERE number_of_rooms > 3; SELECT MAX(number_of_rooms)

3. Get all the listings where number_of_rooms is exactly equal to 3 FROM airbnb_listings;
commands and styles can differ slightly. Popular dialects include MySQL,
SELECT *
4. Get the listing with the lowest number of rooms across all listings
SQLite, SQL Server, Oracle SQL, and more. PostgreSQL is a good place to start FROM airbnb_listings
SELECT MIN(number_of_rooms)

—since it’s close to standard SQL syntax and is easily adapted to other WHERE number_of_rooms = 3; FROM airbnb_listings;
dialects.
4. Get all the listings where number_of_rooms is lower or equal to 3
SELECT *

FROM airbnb_listings
Grouping, filtering, and sorting
> Sample Data WHERE number_of_rooms <= 3;
5. Get all the listings where number_of_rooms is lower than 3 5. Get the total number of rooms for each country
SELECT *
SELECT country, SUM(number_of_rooms)

Throughout this cheat sheet, we’ll use the columns listed in this sample table of FROM airbnb_listings
FROM airbnb_listings

WHERE number_of_rooms < 3; GROUP BY country;


airbnb_listings
6. Get all the listings with 3 to 6 rooms 6. Get the average number of rooms for each country
SELECT *
SELECT country, AVG(number_of_rooms)

airbnb_listings FROM airbnb_listings


FROM airbnb_listings

WHERE number_of_rooms BETWEEN 3 AND 6; GROUP BY country;


b
7. Get the listing with the maximum num er of rooms per country
id city country number_of_rooms year_listed
SELECT country, MAX(number_of_rooms)

Filtering on text columns FROM airbnb_listings

1 Paris France 5 2018


GROUP BY country;
b
7. Get all the listings that are ased in ‘Paris’ 8. Get the listing with the lowest amount of rooms per country
SELECT *
SELECT country, MIN(number_of_rooms)

2 Tokyo Japan 2 2017 FROM airbnb_listings


FROM airbnb_listings

WHERE city = ‘Paris’; GROUP BY country;


b
8. Get the listings ased in the ‘USA’ and in ‘France’ 9. For each country, get the average number of rooms per listing, sorted by ascending order
3 New York USA 2 2022
SELECT *
SELECT country, AVG(number_of_rooms) AS avg_rooms

FROM airbnb_listings
FROM airbnb_listings

WHERE country IN (‘USA’, ‘France’); GROUP BY country

u y b
ORDER BY avg_rooms ASC;

> Q er ing ta les


9. Get all the listings where the city starts with ‘j’ and where the city does not end in ‘t’
SELECT *

v b
10. For Japan and the USA, get the a erage num er of rooms per listing in each country

FROM airbnb_listings
SELECT country, AVG(number_of_rooms)

WHERE city LIKE ‘j%’ AND city NOT LIKE ‘%t’; FROM airbnb_listings

b
1. Get all the columns from a ta le WHERE country IN (‘USA’,‘Japan’);

SELECT *
GROUP BY country;
FROM airbnb_listings;
R city column from the table
Filtering on multiple columns 11. Get the number of listings per country
2. eturn the SELECT country, COUNT(id) AS number_of_listings

SELECT city
FROM airbnb_listings

FROM airbnb_listings; b
10. Get all the listings in `Paris` where number_of_rooms is igger than 3 GROUP BY country;

3. Get the city and year_listed columns from the table SELECT *
12. Get all the years where there were more than 100 listings per year
FROM airbnb_listings

SELECT city, year_listed


SELECT year_listed

WHERE city = ‘Paris’ AND number_of_rooms > 3;


FROM airbnb_listings; FROM airbnb_listings

R
11. Get all the listings in `Paris` O the ones that were listed after 2012 GROUP BY year_listed

4. Get the listing id, city, ordered by the number_of_rooms in ascending order HAVING COUNT(id) > 100;
SELECT *

SELECT id, city


FROM airbnb_listings

FROM airbnb_listings
WHERE city = ‘Paris’ OR year_listed > 2012;
ORDER BY number_of_rooms ASC;

Learn Data Skills Online at www.DataCamp.com

You might also like