Professional Documents
Culture Documents
SQL Basics Cheat Sheet Letter 02
SQL Basics Cheat Sheet Letter 02
SQL Basics Cheat Sheet Letter 02
ORDER BY rating [ASC]; Fetch names of cities that have a population between 500K
and 5M:
Fetch city names sorted by the rating column in the SELECT name
DESCending order: FROM city
SELECT name WHERE population BETWEEN 500000 AND NATURAL JOIN
FROM city 5000000; NATURAL JOIN will join tables by all columns with the
ORDER BY rating DESC; RIGHT JOIN same name.
RIGHT JOIN returns all rows from the right table with SELECT city.name, country.name
corresponding rows from the left table. If there's no matching FROM city
ALIASES Fetch names of cities that don't miss a rating value:
SELECT name row, NULLs are returned as values from the left table. NATURAL JOIN country;
COLUMNS FROM city SELECT city.name, country.name CITY COUNTRY
SELECT name AS city_name WHERE rating IS NOT NULL; FROM city country_id id name name id
FROM city; RIGHT JOIN country 6 6 San Marino San Marino 6
ON city.country_id = country.id; 7 7 Vatican City Vatican City 7
id name country_id
SINGLE VALUE CYCLING SKATING
1 Paris 1 The simplest subquery returns exactly one column and exactly one row. It can be
CITY id name country id name country
101 Marseille 1 used with comparison operators =, <, <=, >, or >=.
country_id count This query finds cities with the same rating as Paris: 1 YK DE 1 YK DE
102 Lyon 1
1 3 SELECT name 2 ZG DE 2 DF DE
2 Berlin 2 FROM city
2 3 3 WT PL 3 AK PL
103 Hamburg 2 WHERE rating = (
4 2 SELECT rating ... ... ... ... ... ...
104 Munich 2
FROM city
3 Warsaw 4 WHERE name = 'Paris' UNION
105 Cracow 4 ); UNION combines the results of two result sets and removes duplicates. UNION
ALL doesn't remove duplicate rows.
AGGREGATE FUNCTIONS MULTIPLE VALUES This query displays German cyclists together with German skaters:
avg(expr) − average value for rows within the group A subquery can also return multiple columns or multiple rows. Such subqueries can SELECT name
count(expr) − count of values for rows within the group be used with operators IN, EXISTS, ALL, or ANY. FROM cycling
max(expr) − maximum value within the group This query finds cities in countries that have a population above 20M: WHERE country = 'DE'
min(expr) − minimum value within the group SELECT name UNION / UNION ALL
sum(expr) − sum of values within the group FROM city SELECT name
WHERE country_id IN ( FROM skating
EXAMPLE QUERIES SELECT country_id WHERE country = 'DE';
Find out the number of cities: FROM country
SELECT COUNT(*) WHERE population > 20000000
FROM city; ); INTERSECT
INTERSECT returns only rows that appear in both result sets.
Find out the number of cities with non-null ratings: This query displays German cyclists who are also German skaters at the same time:
SELECT COUNT(rating)
CORRELATED
SELECT name
FROM city; A correlated subquery refers to the tables introduced in the outer query. A
FROM cycling
correlated subquery depends on the outer query. It cannot be run independently
WHERE country = 'DE'
from the outer query.
Find out the number of distinctive country values: INTERSECT
This query finds cities with a population greater than the average population in the
SELECT COUNT(DISTINCT country_id) SELECT name
country:
FROM city; FROM skating
SELECT *
WHERE country = 'DE';
FROM city main_city
Find out the smallest and the greatest country populations:
WHERE population > (
SELECT MIN(population), MAX(population)
SELECT AVG(population) EXCEPT
FROM country;
FROM city average_city
EXCEPT returns only the rows that appear in the first result set but do not appear in
WHERE average_city.country_id = main_city.country_id
Find out the total population of cities in respective countries: the second result set.
);
SELECT country_id, SUM(population)
This query displays German cyclists unless they are also German skaters at the
FROM city
same time:
GROUP BY country_id; This query finds countries that have at least one city:
SELECT name
SELECT name
FROM cycling
Find out the average rating for cities in respective countries if the average is above FROM country
WHERE country = 'DE'
3.0: WHERE EXISTS (
EXCEPT / MINUS
SELECT country_id, AVG(rating) SELECT *
SELECT name
FROM city FROM city
FROM skating
GROUP BY country_id WHERE country_id = country.id
WHERE country = 'DE';
HAVING AVG(rating) > 3.0; );