SQL Basics Cheat Sheet A3
SQL Basics Cheat Sheet A3
id name country_id
1 Paris 1
CITY
SINGLE VALUE CYCLING SKATING
101 Marseille 1 The simplest subquery returns exactly one column and exactly one row. It can be used
country_id count id name country id name country
102 Lyon 1 with comparison operators =, <, <=, >, or >=.
1 3 This query finds cities with the same rating as Paris: 1 YK DE 1 YK DE
2 Berlin 2 SELECT name
2 3 2 ZG DE 2 DF DE
103 Hamburg 2 FROM city
4 2 3 WT PL 3 AK PL
104 Munich 2 WHERE rating = (
SELECT rating ... ... ... ... ... ...
3 Warsaw 4 FROM city
105 Cracow 4 WHERE name = 'Paris'
);
UNION
UNION combines the results of two result sets and removes duplicates. UNION ALL
AGGREGATE FUNCTIONS doesn't remove duplicate rows.
avg(expr) − average value for rows within the group
count(expr) − count of values for rows within the group
MULTIPLE VALUES This query displays German cyclists together with German skaters:
max(expr) − maximum value within the group A subquery can also return multiple columns or multiple rows. Such subqueries can be SELECT name
min(expr) − minimum value within the group used with operators IN, EXISTS, ALL, or ANY. FROM cycling
sum(expr) − sum of values within the group This query finds cities in countries that have a population above 20M: WHERE country = 'DE'
SELECT name UNION / UNION ALL
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:
SELECT COUNT(rating) This query displays German cyclists who are also German skaters at the same time:
FROM city; CORRELATED
SELECT name
A correlated subquery refers to the tables introduced in the outer query. A correlated
FROM cycling
subquery depends on the outer query. It cannot be run independently from the outer
WHERE country = 'DE'
Find out the number of distinctive country values: query.
INTERSECT
SELECT COUNT(DISTINCT country_id) This query finds cities with a population greater than the average population in the
SELECT name
FROM city; country:
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)
FROM country; FROM city average_city
WHERE average_city.country_id = main_city.country_id
EXCEPT
EXCEPT returns only the rows that appear in the first result set but do not appear in the
);
second result set.
Find out the total population of cities in respective countries:
SELECT country_id, SUM(population) This query displays German cyclists unless they are also German skaters at the same
FROM city time:
GROUP BY country_id; This query finds countries that have at least one city:
SELECT name
SELECT name
FROM cycling
FROM country
WHERE country = 'DE'
Find out the average rating for cities in respective countries if the average is above 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; );