SQL Basics Cheat Sheet
SQL Basics Cheat Sheet
Sciaku.com
Try out the interactive Programming Basics course at Sciaku.com, and check out our other SQL
SQL Basics Cheat Sheet
AGGREGATION AND GROUPING SUBQUERIES SET OPERATIONS
GROUP BY groups together rows that have the same values in specified columns. A subquery is a query that is nested inside another query, or inside another subquery. Set operations are used to combine the results of two or more queries into a single
It computes summaries (aggregates) for each unique combination of values. There are different types of subqueries. result. The combined queries must return the same number of columns and
compatible data types. The names of the corresponding columns can be different.
CITY SINGLE VALUE
id name country_id
1 Paris 1
The simplest subquery returns exactly one column and exactly one row. It can be CYCLING SKATING
101 Marseille 1
CITY used with comparison operators =, <, <=, >, or >=. id name country id name country
country_id count 1 YK DE 1 YK DE
102 Lyon 1 This query finds cities with the same rating as Paris:
1 3 2 ZG DE 2 DF DE
2 Berlin 2
2 3 SELECT name FROM city 3 WT PL 3 AK PL
103 Hamburg 2
4 2 WHERE rating = ( ... ... ... ... ... ...
104 Munich 2
3 Warsaw 4 SELECT rating
105 Cracow 4 FROM city
WHERE name = 'Paris' UNION
); UNION combines the results of two result sets and removes duplicates.
AGGREGATE FUNCTIONS
UNION ALL doesn't remove duplicate rows.
• avg(expr) − average value for rows within the group MULTIPLE VALUES
This query displays German cyclists together with German skaters:
• count(expr) − count of values for rows within the group A subquery can also return multiple columns or multiple rows. Such subqueries can be
used with operators IN, EXISTS, ALL, or ANY. SELECT name
• max(expr) − maximum value within the group FROM cycling
• min(expr) − minimum value within the group This query finds cities in countries that have a population above 20M: WHERE country = 'DE'
• sum(expr) − sum of values within the group SELECT name UNION / UNION ALL
FROM city SELECT name
EXAMPLE QUERIES WHERE country_id IN ( FROM skating
Find out the number of cities: SELECT country_id WHERE country = 'DE';
SELECT COUNT(*) FROM country
FROM city; WHERE population > 20000000
);
INTERSECT
INTERSECT returns only rows that appear in both result sets.
Find out the number of cities with non-null ratings:
CORRELATED This query displays German cyclists who are also German skaters at the same time:
SELECT COUNT(rating)
FROM city; A correlated subquery refers to the tables introduced in the outer query. A correlated SELECT name
subquery depends on the outer query. It cannot be run independently from the outer FROM cycling
query.
Find out the number of distinctive country values: WHERE country = 'DE'
This query finds cities with a population greater than the average population in the INTERSECT
SELECT COUNT(DISTINCT country_id)
country: SELECT name
FROM city;
SELECT * FROM skating
Find out the smallest and the greatest country populations: FROM city main_city WHERE country = 'DE';
SELECT MIN(population), MAX(population) WHERE population > (
FROM country; SELECT AVG(population)
FROM city average_city
EXCEPT
WHERE average_city.country_id = main_city.country_id 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: 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 same time:
This query finds countries that have at least one city:
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';
Sciaku.com
Try out the interactive Programming Basics course at Sciaku.com, and check out our other SQL