0% found this document useful (0 votes)
20 views

SQL Basics CHEAT SHEAT

Uploaded by

muraliinfo24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

SQL Basics CHEAT SHEAT

Uploaded by

muraliinfo24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Basics Cheat Sheet

What is SQL? The different dialects of SQL

Sample Data

airbnb_listings

id city country number_of_rooms year_listed

1 Paris France 5 2018

2 Tokyo Japan 2 2017

3 New York USA 2 2022

Querying tables E Get the listing id, city, ordered by th


number_of_rooms in descending order
FE Get all the columns from a table
SELECT id, city

SELECT *
FROM airbnb_listings

FROM airbnb_listings; ORDER BY number_of_rooms DESC;

mE Return the city column from the table !E Get the first 5 rows from the
SELECT city
airbnb_listings table
FROM airbnb_listings; SELECT *

FROM airbnb_listings

’E Get the city and year_listed columns FROM 5;


from the table
SELECT city, year_listed
9E Get a unique list of cities where there are
FROM airbnb_listings; listings
*Source: Internet

SELECT city

ÂE Get the listing id, city, ordered by the FROM airbnb_listings;


number_of_rooms in ascending order
SELECT id, city

FROM airbnb_listings

ORDER BY number_of_rooms ASC;


SQL Basics Cheat Sheet
Filtering Data Filtering on text columns
Filtering on numeric columns ! Get all the listings that are based in ‘Paris’
SELECT *

-! Get all the listings where number_of_rooms FROM airbnb_listings

is more or equal to 3 WHERE city = ‘Paris’;


SELECT *

FROM airbnb_listings
=! Get all the listings based in the ‘USA’ and in
WHERE number_of_rooms >= 3; ‘France’
SELECT *

j! Get all the listings where number_of_rooms FROM airbnb_listings

is more than 3 WHERE country IN (‘USA’, ‘France’);


SELECT *

FROM airbnb_listings
! Get all the listings where the city starts with
WHERE number_of_rooms > 3; ‘j’ and where the city does not end in ‘t’
SELECT *

€! Get all the listings where number_of_rooms FROM airbnb_listings

is exactly equal to 3 WHERE city LIKE ‘j%’ AND city NOT


SELECT *
LIKE ‘%t’;
FROM airbnb_listings

WHERE number_of_rooms = 3; Filtering on multiple columns


“! Get all the listings where number_of_rooms -É! Get all the listings in ‘Paris’ where
is lower or equal to 3 number_of_rooms is bigger than 3
SELECT *
SELECT *

FROM airbnb_listings
FROM airbnb_listings

WHERE number_of_rooms <= 3; WHERE city = ‘Paris’; AND


number_of_rooms > 3;
¬! Get all the listings where number_of_rooms
is lower than 3 --! Get all the listings in ‘Paris’ OR the ones
SELECT *
that were listed after 2012
FROM airbnb_listings
SELECT *

WHERE number_of_rooms < 3; FROM airbnb_listings

WHERE city = ‘Paris’; OR year_listed > 2012;


È! Get all the listings with 3 to 6 rooms
SELECT *
Filtering on missing data
FROM airbnb_listings

WHERE number_of_rooms BETWEEN 3 AND 6;


*Source: Internet

-j! Return the listings where number_of_rooms


is missing
SELECT *

FROM airbnb_listings

WHERE number_of_rooms IS NULL;


SQL Basics Cheat Sheet
 Return the listings where number_of_rooms is + Get the listing with the maximum number of
not missing rooms per country
SELECT *
SELECT country, MAX(number_of_rooms)

FROM airbnb_listings
FROM airbnb_listings

WHERE number_of_rooms IS NOT NULL; GROUP BY country;

AGGREGATING DATA A Get the listing with the lowest amount of


rooms per country
Simple aggregations SELECT country, MIN(number_of_rooms)

FROM airbnb_listings

 Get the total number of rooms available across GROUP BY country;


all listings
Z For each country, get the average number of
SELECT SUM(number_of_rooms)
rooms per listing, sorted by ascending order
FROM airbnb_listings;
SELECT country, AVG(number_of_rooms)
Œ Get the average number of rooms per listing AS avg_rooms

across all listings FROM airbnb_listings

GROUP BY country;

SELECT AVG(number_of_rooms)
ORDER BY avg_rooms ASC;
FROM airbnb_listings;
“ For Japan and the USA, get the average
 Get the listing with the highest number of rooms number of rooms per listing in each country
across all listings
SELECT country, MAX(number_of_rooms)

SELECT MAX(number_of_rooms)
FROM airbnb_listings

FROM airbnb_listings; WHERE country IN (‘USA’, ‘Japan’);

GROUP BY country;
§ Get the listing with the lowest number of rooms
across all listings  Get the number of cities per country, where
SELECT MIN(number_of_rooms)
there are listings
FROM airbnb_listings; SELECT country, COUNT(city) AS
number_of_cities

Grouping, filtering, and sorting FROM airbnb_listings

GROUP BY country;
Ë Get the total number of rooms for each country
Œ Get all the years where there were more than
SELECT country, SUM(number_of_rooms)
100 listings per year
FROM airbnb_listings

GROUP BY country; SELECT year_listed

FROM airbnb_listings

*Source: Internet

 Get the average number of rooms for each country GROUP BY year_listed

HAVING COUNT(id) > 100;


SELECT country, AVG(number_of_rooms)

FROM airbnb_listings

GROUP BY country;

You might also like