SQL Cheatsheet
SQL Cheatsheet
CITY
id name country_id SINGLE VALUE
1 Paris 1 CYCLING SKATING
101 Marseille 1
CITY The simplest subquery returns exactly one column and exactly one row. It can be
country_id count id name country id name country
102 Lyon 1 used with comparison operators =, <, <=, >, or >=.
1 3 1 YK DE 1 YK DE
2 Berlin 2 This query finds cities with the same rating as Paris: 2 ZG DE 2 DF DE
2 3
103 Hamburg 2 3 WT PL 3 AK PL
104 Munich 2
4 2 SELECT name FROM city
... ... ... ... ... ...
3 Warsaw 4 WHERE rating = (
105 Cracow 4 SELECT rating
FROM city
WHERE name = 'Paris'
UNION
AGGREGATE FUNCTIONS );
UNION combines the results of two result sets and removes duplicates.
•
avg(expr) − average value for rows within the group
UNION ALL doesn't remove duplicate rows.
• count(expr) − count of values for rows within the group
MULTIPLE VALUES This query displays German cyclists together with German skaters:
• m ax(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 FROM cycling
used with operators IN, EXISTS, ALL, or ANY.
• sum(expr) − sum of values within the group WHERE country = 'DE'
This query finds cities in countries that have a population above 20M: UNION / UNION ALL
SELECT name SELECT name
EXAMPLE QUERIES FROM city FROM skating
Find out the number of cities: WHERE country_id IN ( WHERE country = 'DE';
SELECT country_id
SELECT COUNT(*)
FROM country
FROM city;
WHERE population > 20000000
); INTERSECT
Find out the number of cities with non-null ratings: INTERSECT returns only rows that appear in both result sets.
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
Find out the number of distinctive country values: subquery depends on the outer query. It cannot be run independently from the outer WHERE country = 'DE'
SELECT COUNT(DISTINCT country_id) query. INTERSECT
FROM city; SELECT name
This query finds cities with a population greater than the average population in the
FROM skating
country:
WHERE country = 'DE';
Find out the smallest and the greatest country populations: SELECT *
FROM city main_city
SELECT MIN(population), MAX(population)
WHERE population > (
FROM country;
SELECT AVG(population) EXCEPT
FROM city average_city EXCEPT returns only the rows that appear in the first result set but do not appear
Find out the total population of cities in respective countries: WHERE average_city.country_id = main_city.country_id in the second result set.
SELECT country_id, SUM(population) );
This query displays German cyclists unless they are also German skaters at the
FROM city
This query finds countries that have at least one city: same time:
GROUP BY country_id;
SELECT name SELECT name
FROM country FROM cycling
Find out the average rating for cities in respective countries if the average is above 3.0: WHERE EXISTS ( WHERE country = 'DE'
SELECT country_id, AVG(rating) SELECT * EXCEPT / MINUS
FROM city FROM city SELECT name
GROUP BY country_id WHERE country_id = country.id FROM skating
HAVING AVG(rating) > 3.0; ); WHERE country = 'DE';
CREATE TEMPORARY VIEW v MIN returns the minimum value in a list CREATE TRIGGER before_insert_person
AS BEFORE INSERT
SELECT c1, c2 ON person FOR EACH ROW
FROM t; EXECUTE stored_procedure;
Create a temporary view Create a trigger invoked before a new row is
inserted into the person table