

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
query for single table, multiple tables, and aggregation/grouping
Typology: Cheat Sheet
1 / 2
This page cannot be seen from the preview
Don't miss anything!


SELECT * FROM country;
SELECT id, name FROM city;
SELECT name FROM city WHERE...
rating > 3;
name LIKE 'P%' OR name LIKE '%s';
name LIKE '_ublin';
name != 'Berlin' AND name != 'Madrid';
population BETWEEN 1000000 AND 5000000;
rating IS NOT NULL;
country_id IN (1,4,7,8);
SELECT city.name, country.name FROM city JOIN country ON city.country_id = country.id;
city LEFT (OUTER) JOIN country
city id name country_id 1 Paris 1 2 Berlin 2 3 Warsaw 4
country id name 1 France 2 Germany null null
city (INNER) JOIN country
city id name country_id 1 Paris 1 2 Berlin 2 3 Warsaw 4
country id name 1 France 2 Germany 3 Iceland
country id name population area 1 France 66600000 640680 2 Germany 80700000 357000 ... ... ... ...
city id name country_id population rating 1 Paris 1 2243000 5 2 Berlin 2 3460000 3 ... ... ... ... ...
city RIGHT (OUTER) JOIN country
city id name country_id 1 Paris 1 2 Berlin 2 null null null
country id name 1 France 2 Germany 3 Iceland
city FULL (OUTER) JOIN country
city id name country_id 1 Paris 1 2 Berlin 2 3 Warsaw 4 null null null
country id name 1 France 2 Germany null null 3 Iceland
SELECT name FROM city ORDER BY rating DESC;
SELECT COUNT(*) FROM city;
SELECT COUNT(rating) FROM city;
SELECT COUNT(DISTINCT country_id) FROM city;
SELECT MIN(population), MAX(population) FROM country;
SELECT country_id, SUM(population) FROM city GROUP BY country_id;
SELECT country_id, AVG(rating) FROM city GROUP BY country_id HAVING AVG(rating) > 3;
SINGLE VALUE
SELECT name FROM city WHERE rating = (SELECT rating FROM city WHERE name = 'Paris');
SELECT name FROM cycling WHERE country = 'DE' UNION (ALL) SELECT name FROM skating WHERE country = 'DE';
SELECT name FROM cycling WHERE country = 'DE' INTERSECT SELECT name FROM skating WHERE country = 'DE';
SELECT name FROM cycling WHERE country = 'DE' EXCEPT/MINUS SELECT name FROM skating WHERE country = 'DE';
MULTIPLE VALUES
SELECT name FROM city WHERE country_id IN (SELECT country_id FROM country WHERE population > 20000000);
CORRELATED
SELECT * FROM city main_city WHERE population > (SELECT AVG(population) FROM city average_city WHERE average_city.country_id = main_city.country_id);
SELECT name FROM country WHERE EXISTS (SELECT * FROM city WHERE country_id = country.id);
cycling id name country 1 YK DE 2 ZG DE 3 WT PL ... ... ...
skating id name country 1 YK DE 2 DF DE 3 AK PL ... ... ...