SQL for Data
Analysis
-By Satyam Muley
Get all the columns from a table
SELECT *
FROM Table;
Return the city column from the table
SELECT city
FROM Table;;
Get the city and year_listed columns from the table
SELECT city, year_listed
FROM Table;
-By Satyam Muley
Get the listing id, city, ordered by the number_of_rooms in ascending
order
SELECT city, year_listed
FROM Table
ORDER BY number_of_rooms ASC;
Get the listing id, city, ordered by the number_of_rooms in descending
order
SELECT city, year_listed
FROM Table
ORDER BY number_of_rooms DESC;
Get the first 5 rows from airbnb_listings
SELECT *
FROM Table
LIMIT 5;
-By Satyam Muley
Get a unique list of cities where there are listings
SELECT DISTINCT city
FROM Table;
Get all the listings where number_of_rooms is more or equal to 3
SELECT *
FROM Table
WHERE number_of_rooms >= 3;
Get all the listings where number_of_rooms is lower
or equal to 3
SELECT *
FROM Table
WHERE number_of_rooms <= 3;
-By Satyam Muley
Filtering columns within a range—Get all the listings with 3 to 6 rooms
SELECT *
FROM Table
WHERE number_of_rooms BETWEEN 3 AND 6;
Filter one column on many conditions—Get the listings based in the
'USA' and in ‘France’
SELECT *
FROM Table
WHERE country IN ('USA', 'France');
Get all listings where city starts with "j" and where it
does not end with "t"
SELECT *
FROM Table
WHERE city LIKE 'j%' AND city NOT LIKE '%t';
-By Satyam Muley
Get all the listings where number_of_rooms is missing
SELECT *
FROM Table
WHERE number_of_rooms IS NULL;
Get all the listings where number_of_rooms is not missing
SELECT *
FROM Table
WHERE number_of_rooms IS NOT NULL;
Get the total number of rooms available across all
listings
SELECT SUM(number_of_rooms)
FROM Table;
-By Satyam Muley
Get the average number of rooms per listing across all listings
SELECT AVG(number_of_rooms)
FROM Table;
Get the listing with the highest number of rooms across all listings
SELECT MAX(number_of_rooms)
FROM Table;
Get the total number of rooms for each country
SELECT country, SUM(number_of_rooms)
FROM Table
GROUP BY country;
-By Satyam Muley
For each country, get the average number of rooms per listing, sorted
by ascending order
SELECT country, AVG(number_of_rooms) AS
avg_rooms
FROM Table
GROUP BY country
ORDER BY avg_rooms ASC;
For Japan and the USA, get the average number of rooms per listing in
each country
SELECT country, AVG(number_of_rooms)
FROM airbnb_listings
WHERE country IN ('USA', 'Japan');
GROUP BY country;
-By Satyam Muley
Thank You
Follow for More !!
-By Satyam Muley