SQL Basics Cheat Sheet

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

SQL Basics Cheat Sheet

SQL FILTERING THE OUTPUT QUERYING MULTIPLE TABLES


SQL, or Structured Query Language, is a language to talk to FULL JOIN
databases. It allows you to select specific data and to build COMPARISON OPERATORS INNER JOIN
complex reports. Today, SQL is a universal language of data. It is Fetch names of cities that have a rating above 3: JOIN (or explicitly INNER JOIN) returns rows that have FULL JOIN (or explicitly FULL OUTER JOIN) returns all rows
used in practically all technologies that process data. matching values in both tables. from both tables – if there's no matching row in the second
SELECT name table, NULLs are returned.
FROM city
SAMPLE DATA WHERE rating > 3; SELECT city.name, country.name SELECT city.name, country.name
COUNTRY FROM city FROM city
id name population area [INNER] JOIN country FULL [OUTER] JOIN country
ON city.country_id = country.id; ON city.country_id = country.id;
1 France 66600000 640680 Fetch names of cities that are neither Berlin nor Madrid:
CITY COUNTRY
2 Germany 80700000 357000 SELECT name id name country_id id name
CITY COUNTRY
CITY ... ... ... ... FROM city 1 Paris 1 1 France
id name country_id id name
id name country_id population rating WHERE name != 'Berlin' 2 Berlin 2 2 Germany
1 Paris 1 1 France
AND name != 'Madrid'; 2 Berlin 2 2 Germany 3 Warsaw 4 NULL NULL
NULL NULL NULL 3 Iceland
1 Paris 1 2243000 5 3 Warsaw 4 3 Iceland
2 Berlin 2 3460000 3
... ... ... ... ... TEXT OPERATORS
Fetch names of cities that start with a 'P' or end with an 's':
QUERYING SINGLE TABLE SELECT name
Fetch all columns from the country table: LEFT JOIN CROSS JOIN
FROM city LEFT JOIN returns all rows from the left table with CROSS JOIN returns all possible combinations of rows from
SELECT * WHERE name LIKE 'P%' corresponding rows from the right table. If there's no both tables. There are two syntaxes available.
FROM country; OR name LIKE '%s'; matching row, NULLs are returned as values from the second SELECT city.name, country.name
table. FROM city
Fetch id and name columns from the city table: CROSS JOIN country;
SELECT city.name, country.name
SELECT id, name Fetch names of cities that start with any letter followed by FROM city
FROM city; 'ublin' (like Dublin in Ireland or Lublin in Poland): SELECT city.name, country.name
LEFT JOIN country
FROM city, country;
SELECT name ON city.country_id = country.id;
Fetch city names sorted by the rating column CITY COUNTRY
in the default ASCending order: FROM city CITY COUNTRY id name country_id id name
WHERE name LIKE '_ublin'; id name country_id id name 1 Paris 1 1 France
SELECT name 1 Paris 1 1 France 1 Paris 1 2 Germany
FROM city 2 Berlin 2 2 Germany 2 Berlin 2 1 France
ORDER BY rating [ASC]; 3 Warsaw 4 NULL NULL 2 Berlin 2 2 Germany
OTHER OPERATORS
Fetch city names sorted by the rating column Fetch names of cities that have a population between
in the DESCending order: 500K and 5M:
SELECT name SELECT name
FROM city FROM city RIGHT JOIN NATURAL JOIN
ORDER BY rating DESC; WHERE population BETWEEN 500000 AND 5000000;
RIGHT JOIN returns all rows from the right table with NATURAL JOIN will join tables by all columns with the same
corresponding rows from the left table. If there's no name.
matching row, NULLs are returned as values from the left
ALIASES Fetch names of cities that don't miss a rating value: table.
SELECT city.name, country.name
FROM city
COLUMNS SELECT name NATURAL JOIN country;
SELECT city.name, country.name
SELECT name AS city_name FROM city
FROM city CITY COUNTRY
FROM city; WHERE rating IS NOT NULL; country_id id name name id
RIGHT JOIN country
6 6 San Marino San Marino 6
ON city.country_id = country.id;
TABLES 7 7 Vatican City Vatican City 7
Fetch names of cities that are in countries with IDs 1, 4, 7, or 8: CITY COUNTRY 5 9 Greece Greece 9
SELECT co.name, ci.name 10 11 Monaco Monaco 10
id name country_id id name
FROM city AS ci SELECT name
1 Paris 1 1 France NATURAL JOIN used these columns to match rows:
JOIN country AS co FROM city city.id, city.name, country.id, country.name
2 Berlin 2 2 Germany
ON ci.country_id = co.id; WHERE country_id IN (1, 4, 7, 8); NULL NULL NULL 3 Iceland NATURAL JOIN is very rarely used in practice.

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

You might also like