Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

SQL Basics Cheat Sheet, Cheat Sheet of Database Management Systems (DBMS)

SQL, Querying Single Table, Aliases, Aggregation and Grouping, Subqueries, Set Operators commands

Typology: Cheat Sheet

2020/2021

Uploaded on 04/26/2021

myboy
myboy 🇺🇸

4.4

(72)

260 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
SQL Basics Cheat Sheet
SQL, or Structured Query Language, is a lang uage to talk to
databases . It allows you to select specific dat a and to build
complex repo rts. Today, SQL is a unive rsal language of d ata. It is
used in prac tically all tech nologies that pro cess data.
SQL
SAMPLE DATA
CITY
id name co un tr y_id population rating
1Paris 12243000 5
2Be rlin 23460000 3
... ... ... ... ...
COUNTRY
id name population area
1Fran ce 66600000 640680
2Germany 80700000 357000
... ... ... ...
ALIASES
COLUMNS
SELECT na me AS city_name
FROM cit y;
TABL ES
SELECT c o.nam e, c i.nam e
FROM cit y AS ci
JOIN country A S co
ON ci.country_id = co.id;
QUERYING MULTIPLE TABLES
INNER JOIN
SELECT c it y. na m e, c ou n tr y.n a m e
FROM cit y
[INNER] JOIN country
ON city.country_id = country.id;
CITY
id name co un tr y_id
1Paris 1
2Be rlin 2
3Warsaw 4
COUNTRY
id name
1Fran ce
2Germany
3Icela nd
JOIN (or explicitly INNER JOIN) returns rows t hat have
matching value s in both tables.
LEFT JOIN
SELECT c it y. na m e, c ou n tr y.n a m e
FROM cit y
LEFT JOIN co untry
ON city.country_id = country.id;
CITY
id name co un tr y_id
1Paris 1
2Be rlin 2
3Warsaw 4
COUNTRY
id name
1Fran ce
2Germany
NULL NULL
LEFT JOIN retur ns all rows from the left t able with
correspon ding rows from the rig ht table. If there's no
matching row, NULLs are retur ned as values from t he second
table.
RIGHT JOIN
SELECT c it y. na m e, c ou n tr y.n a m e
FROM cit y
RIGHT J OIN countr y
ON city.country_id = country.id;
CITY
id name co un tr y_id
1Paris 1
2Be rlin 2
NULL NULL NULL
COUNTRY
id name
1Fran ce
2Germany
3Icela nd
RIGHT JOIN r eturns all rows from t he right table with
correspon ding rows from the left t able. If there's no
matching row, NULLs are retur ned as values from t he left
table.
FULL JOIN
SELECT c it y. na m e, c ou n tr y.n a m e
FROM cit y
FULL [OUTER] JOIN country
ON city.country_id = country.id;
CITY
id name co un tr y_id
1Paris 1
2Be rlin 2
3Warsaw 4
NULL NULL NULL
COUNTRY
id name
1Fran ce
2Germany
NULL NULL
3Icela nd
FULL JOIN (or explicitly FULL OUTER JOIN) re turns all rows
from both tab les – if there's no matchin g row in the second
table, NULL s are returned.
CITY
co un try _id id name
6 6 San Ma rino
7 7 Vatica n City
5 9 Greece
10 11 Monaco
COUNTRY
name id
San Ma rino 6
Vatica n City 7
Greece 9
Monaco 10
NATURAL JOIN
SELECT c it y. na m e, c ou n tr y.n a m e
FROM cit y
NATU RAL JOIN c ou ntr y;
NATUR AL JOI N will join table s by all columns with the s ame
name.
NATUR AL JOIN u sed these columns to m atch rows:
cit y.id, cit y.na m e, c ou ntr y.id, co un tr y.na me
NATUR AL JOIN i s very rarely us ed in practice.
CROSS JOIN
SELECT c it y. na m e, c ou n tr y.n a m e
FROM cit y
CROSS JOIN co unt ry;
SELECT c it y. na m e, c ou n tr y.n a m e
FROM city, country;
CROSS JOIN returns al l possible combinati ons of rows from
both tables . There are two sy ntaxes available.
CITY
id name co un tr y_id
1Paris 1
1Paris 1
2Be rlin 2
2Be rlin 2
COUNTRY
id name
1Fran ce
2Germany
1Fran ce
2Germany
QUERYING SINGLE TABLE
Fetch all columns f rom the coun try table:
SELECT *
FROM co un try;
Fetch id and nam e columns from the c ity table:
SELECT i d, na me
FROM cit y;
SELECT nam e
FROM cit y
ORDER BY r ating DESC;
Fetch city na mes sorted by t he ratin g column
in the DESCending order:
SELECT nam e
FROM cit y
ORDER BY r ating [A S C];
Fetch city na mes sorted by t he ratin g column
in the default A SCending order:
SELECT nam e
FROM cit y
WHERE nam e LIKE '_ u bl in ';
Fetch names of ci ties that star t with any lett er followed by
'ublin' (like Dublin in I reland or Lublin in Pol and):
SELECT nam e
FROM cit y
WHERE nam e != 'Berlin'
AND name != 'Madrid';
Fetch names of ci ties that are neit her Berlin nor Ma drid:
SELECT nam e
FROM cit y
WHERE ra ting IS NOT NULL;
Fetch names of ci ties that don't mis s a rating value:
SELECT nam e
FROM cit y
WHERE country_id IN (1, 4, 7, 8);
Fetch names of ci ties that are in coun tries with IDs 1, 4, 7, or 8:
FILTERING THE OUTPUT
SELECT nam e
FROM cit y
WHERE rating > 3;
Fetch names of ci ties that have a rat ing above 3:
COMPARISON OPERATORS
SELECT nam e
FROM cit y
WHERE nam e LIKE 'P %'
OR name LIKE '% s';
Fetch names of ci ties that star t with a 'P' or end w ith an 's':
TEXT OPERATORS
SELECT nam e
FROM cit y
WHERE population BETWEEN 500000 AND 5000000;
Fetch names of ci ties that have a popula tion between
500K and 5M :
OTHER OPERATORS
pf2

Partial preview of the text

Download SQL Basics Cheat Sheet and more Cheat Sheet Database Management Systems (DBMS) in PDF only on Docsity!

SQL Basics Cheat Sheet

SQL , or Structured Query Language , is a language to talk to databases. It allows you to select specific data and to build complex reports. Today, SQL is a universal language of data. It is used in practically all technologies that process data.

SQL

SAMPLE DATA

CITY id name country_id population rating 1 Paris 1 2243000 5 2 Berlin 2 3460000 3 ... ... ... ... ...

COUNTRY id name population area 1 France 66600000 640680 2 Germany 80700000 357000 ... ... ... ...

ALIASES

COLUMNS

SELECT name AS city_name FROM city;

TABLES

SELECT co.name, ci.name FROM city AS ci JOIN country AS co ON ci.country_id = co.id;

QUERYING MULTIPLE TABLES

INNER JOIN

SELECT city.name, country.name FROM city [INNER] JOIN country ON city.country_id = country.id;

CITY id name country_id 1 Paris 1 2 Berlin 2 3 Warsaw 4

COUNTRY id name 1 France 2 Germany 3 Iceland

JOIN (or explicitly INNER JOIN ) returns rows that have matching values in both tables.

LEFT JOIN

SELECT city.name, country.name FROM city LEFT JOIN country ON city.country_id = country.id; CITY id name country_id 1 Paris 1 2 Berlin 2 3 Warsaw 4

COUNTRY id name 1 France 2 Germany NULL NULL

LEFT JOIN returns all rows from the left table with corresponding rows from the right table. If there's no matching row, NULL s are returned as values from the second table.

RIGHT JOIN

SELECT city.name, country.name FROM city RIGHT JOIN country ON city.country_id = country.id;

CITY id name country_id 1 Paris 1 2 Berlin 2 NULL NULL NULL

COUNTRY id name 1 France 2 Germany 3 Iceland

RIGHT JOIN returns all rows from the right table with corresponding rows from the left table. If there's no matching row, NULL s are returned as values from the left table.

FULL JOIN

SELECT city.name, country.name FROM city FULL [OUTER] JOIN country ON city.country_id = country.id; 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

FULL JOIN (or explicitly FULL OUTER JOIN ) returns all rows from both tables – if there's no matching row in the second table, NULL s are returned.

CITY country_id id name 6 6 San Marino 7 7 Vatican City 5 9 Greece 10 11 Monaco

COUNTRY name id San Marino 6 Vatican City 7 Greece 9 Monaco 10

NATURAL JOIN

SELECT city.name, country.name FROM city NATURAL JOIN country;

NATURAL JOIN will join tables by all columns with the same name.

NATURAL JOIN used these columns to match rows: city.id, city.name, country.id, country.name NATURAL JOIN is very rarely used in practice.

CROSS JOIN

SELECT city.name, country.name FROM city CROSS JOIN country;

SELECT city.name, country.name FROM city, country;

CROSS JOIN returns all possible combinations of rows from both tables. There are two syntaxes available.

CITY id name country_id 1 Paris 1 1 Paris 1 2 Berlin 2 2 Berlin 2

COUNTRY id name 1 France 2 Germany 1 France 2 Germany

QUERYING SINGLE TABLE

Fetch all columns from the country table: SELECT * FROM country;

Fetch id and name columns from the city table: SELECT id, name FROM city;

SELECT name FROM city ORDER BY rating DESC;

Fetch city names sorted by the rating column in the DESCending order:

SELECT name FROM city ORDER BY rating [ASC];

Fetch city names sorted by the rating column in the default ASCending order:

SELECT name FROM city WHERE name LIKE '_ublin';

Fetch names of cities that start with any letter followed by 'ublin' (like Dublin in Ireland or Lublin in Poland):

SELECT name FROM city WHERE name != 'Berlin' AND name != 'Madrid';

Fetch names of cities that are neither Berlin nor Madrid:

SELECT name FROM city WHERE rating IS NOT NULL;

Fetch names of cities that don't miss a rating value:

SELECT name FROM city WHERE country_id IN (1, 4, 7, 8);

Fetch names of cities that are in countries with IDs 1, 4, 7, or 8:

FILTERING THE OUTPUT

SELECT name FROM city WHERE rating > 3;

Fetch names of cities that have a rating above 3:

COMPARISON OPERATORS

SELECT name FROM city WHERE name LIKE 'P%' OR name LIKE '%s';

Fetch names of cities that start with a 'P' or end with an 's':

TEXT OPERATORS

SELECT name FROM city WHERE population BETWEEN 500000 AND 5000000;

Fetch names of cities that have a population between 500K and 5M:

OTHER OPERATORS

SQL Basics Cheat Sheet

- avg( expr ) − average value for rows within the group - count( expr ) − count of values for rows within the group - max( expr ) − maximum value within the group - min( expr ) − minimum value within the group - sum( expr ) − sum of values within the group

AGGREGATE FUNCTIONS

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 ... ... ...

AGGREGATION AND GROUPING

GROUP BY groups together rows that have the same values in specified columns. It computes summaries (aggregates) for each unique combination of values.

SUBQUERIES

A subquery is a query that is nested inside another query, or inside another subquery. There are different types of subqueries.

SET OPERATIONS

Set operations are used to combine the results of two or more queries into a single 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 country_id count 1 3 2 3 4 2

CITY id name country_id 1 Paris 1 101 Marseille 1 102 Lyon 1 2 Berlin 2 103 Hamburg 2 104 Munich 2 3 Warsaw 4 105 Cracow 4

EXAMPLE QUERIES

SELECT COUNT(*)

FROM city;

Find out the number of cities:

SELECT COUNT( rating ) FROM city;

Find out the number of cities with non-null ratings:

SELECT COUNT(DISTINCT country_id ) FROM city;

Find out the number of distinctive country values:

SELECT MIN( population ) , MAX( population ) FROM country;

Find out the smallest and the greatest country populations:

SELECT country_id, SUM( population ) FROM city GROUP BY country_id;

Find out the total population of cities in respective countries:

SELECT country_id, AVG( rating ) FROM city GROUP BY country_id HAVING AVG( rating ) > 3.0;

Find out the average rating for cities in respective countries if the average is above 3.0:

UNION

SELECT name FROM cycling WHERE country = 'DE' UNION / UNION ALL SELECT name FROM skating WHERE country = 'DE';

UNION combines the results of two result sets and removes duplicates. UNION ALL doesn't remove duplicate rows. This query displays German cyclists together with German skaters:

INTERSECT

SELECT name FROM cycling WHERE country = 'DE' INTERSECT SELECT name FROM skating WHERE country = 'DE';

INTERSECT returns only rows that appear in both result sets. This query displays German cyclists who are also German skaters at the same time:

EXCEPT

SELECT name FROM cycling WHERE country = 'DE' EXCEPT / MINUS SELECT name FROM skating WHERE country = 'DE';

EXCEPT returns only the rows that appear in the first result set but do not appear in the second result set. This query displays German cyclists unless they are also German skaters at the same time:

SINGLE VALUE

SELECT name FROM city WHERE rating = ( SELECT rating FROM city WHERE name = 'Paris' );

The simplest subquery returns exactly one column and exactly one row. It can be used with comparison operators =, <, <=, >, or >=. This query finds cities with the same rating as Paris:

MULTIPLE VALUES

SELECT name FROM city WHERE country_id IN ( SELECT country_id FROM country WHERE population > 20000000 );

A subquery can also return multiple columns or multiple rows. Such subqueries can be used with operators IN, EXISTS, ALL, or ANY. This query finds cities in countries that have a population above 20M:

CORRELATED

SELECT *

FROM city main_city WHERE population > ( SELECT AVG(population) FROM city average_city WHERE average_city.country_id = main_city.country_id );

This query finds countries that have at least one city: SELECT name FROM country WHERE EXISTS ( SELECT * FROM city WHERE country_id = country.id );

A correlated subquery refers to the tables introduced in the outer query. A correlated subquery depends on the outer query. It cannot be run independently from the outer query. This query finds cities with a population greater than the average population in the country: