0% found this document useful (0 votes)
18 views

SQL Basics Cheat Sheet

Uploaded by

ruchikatalele1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL Basics Cheat Sheet

Uploaded by

ruchikatalele1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL Basics Cheat Sheet

SQL, or Structured Query Language, is a language to talk to FULL JOIN


databases. It allows you to select specific data and to build FILTERING THE OUTPUT QUERYING MULTIPLE TABLES
FULL JOIN (or explicitly FULL OUTER JOIN) returns all rows
complex reports. Today, SQL is a universal language of data. It is COMPARISON OPERATORS INNER JOIN from both tables – if there's no matching row in the second table,
used in practically all technologies that process data. Fetch names of cities that have a rating above 3: JOIN (or explicitly INNER JOIN ) returns rows that have NULLs are returned.
SAMPLE DATA SELECT name
FROM city
matching values in both tables.
SELECT city.name, country.name
SELECT city.name, country.name
FROM city
WHERE rating > 3; FROM city FULL [OUTER] JOIN country
COUNTRY
[INNER] JOIN country ON city.country_id = country.id;
id name population area ON city.country_id = country.id; CITY COUNTRY
1 France 66600000 640680 Fetch names of cities that are neither Berlin nor Madrid: CITY COUNTRY id name country_id id name
2 Germany 80700000 357000 SELECT name id name country_id id name 1 Paris 1 1 France
FROM city 1 Paris 1 1 France 2 Berlin 2 2 Germany
... ... ... ...
WHERE name != 'Berlin' 2 Berlin 2 2 Germany 3 Warsaw 4 NULL NULL
CITY AND name != 'Madrid'; 3 Warsaw 4 3 Iceland NULL NULL NULL 3 Iceland
id name country_id population rating
1 Paris 1 2243000 5
2 Berlin 2 3460000 3 TEXT OPERATORS
Fetch names of cities that start with a 'P' or end with an 's':
CROSS JOIN
... ... ... ... ... CROSS JOINreturns all possible combinations of rows from
SELECT name
FROM city LEFT JOIN both tables. There are two syntaxes available.
QUERYING SINGLE TABLE WHERE name LIKE 'P%' LEFT JOIN returns all rows from the le table with SELECT city.name, country.name
FROM city
OR name LIKE '%s'; corresponding rows from the right table. If there's no matching
Fetch all columns from the country table: CROSS JOIN country;
row, NULLs are returned as values from the second table.
SELECT *
SELECT city.name, country.name
FROM country;
FROM city SELECT city.name, country.name
Fetch names of cities that start with any letter followed by 'ublin' LEFT JOIN country FROM city, country;
Fetch id and name columns from the city table:
SELECT id, name (like Dublin in Ireland or Lublin in Poland): ON city.country_id = country.id; CITY COUNTRY
SELECT name
FROM city; CITY COUNTRY id name country_id id name
FROM city
Fetch city names sorted by the rating WHERE name LIKE '_ublin'; id name country_id id name 1 Paris 1 1 France
column in the default
ASCending order: 1 Paris 1 1 France 1 Paris 1 2 Germany
SELECT name 2 Berlin 2 2 Germany 2 Berlin 2 1 France
FROM city 3 Warsaw 4 NULL NULL 2 Berlin 2 2 Germany
ORDER BY rating [ASC]; OTHER OPERATORS
Fetch city names sorted by the rating Fetch names of cities that have a population between 500K and
column in the 5M:
DESCending order:
SELECT name
SELECT name NATURAL JOIN
FROM city FROM city NATURAL JOINwill join tables by all columns with the same
ORDER BY rating DESC; WHERE population BETWEEN 500000 AND 5000000; RIGHT JOIN name.
RIGHT JOIN returns all rows from the right table with SELECT city.name, country.name
corresponding rows from the le table. If there's no matching FROM city
ALIASES Fetch names of cities that don't miss a rating value: row, NULLs are returned as values from the le table. NATURAL JOIN country;
SELECT name SELECT city.name, country.name
COLUMNS FROM city
CITY COUNTRY
FROM city country_id id name name id
SELECT name AS city_name
WHERE rating IS NOT NULL; RIGHT JOIN country 6 6 San Marino San Marino 6
FROM city;
ON city.country_id = country.id; 7 7 Vatican City Vatican City 7
TABLES CITY COUNTRY 5 9 Greece Greece 9
SELECT co.name, ci.name id name country_id id name
Fetch names of cities that are in countries with IDs 1, 4, 7, or 8: 10 11 Monaco Monaco 10
FROM city AS ci 1 Paris 1 1 France
SELECT name NATURAL JOIN used these columns to match rows:
JOIN country AS co
FROM city 2 Berlin 2 2 Germany city.id, city.name, country.id, country.name.
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.
SQL Basics Cheat Sheet
AGGREGATION AND GROUPING
GROUP BY groups together rows that have the same values in specified columns. It SUBQUERIES SET OPERATIONS
computes summaries (aggregates) for each unique combination of values. 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 result.
There are different types of subqueries. The combined queries must return the same number of columns and compatible data
CITY types. The names of the corresponding columns can be different.
id name country_id SINGLE VALUE CYCLING SKATING
1 Paris 1 The simplest subquery returns exactly one column and exactly one row. It can be used
id name country id name country
CITY with comparison operators ,=<<=>
, , , or >=.
101 Marseille 1 This query finds cities with the same rating as Paris: 1 YK DE 1 Y DE
country_id count
102 Lyon 1 SELECT name 2 ZG DE 2 K DE
1 3 FROM city
2 Berlin 2 3 WT PL 3 D PL
2 3 WHERE rating = (
103 Hamburg 2 SELECT rating ... ... ... ... ...F ...
4 2
104 Munich 2 FROM city A
WHERE name = 'Paris' UNION
3 Warsaw 4 K
); UNION combines the results of two result sets and removes duplicates.UNION ALL
105 Cracow 4 doesn't remove duplicate rows.

MULTIPLE VALUES This query displays German cyclists together with German skaters:
AGGREGATE FUNCTIONS A subquery can also return multiple columns or multiple rows. Such subqueries can be SELECT name
avg(expr) − average value for rows within the group used with operators INEXISTSALL
, , , or ANY. FROM cycling
count(expr) − count of values for rows within the group This query finds cities in countries that have a population above 20M: WHERE country = 'DE'
max(expr) − maximum value within the group SELECT name UNION / UNION ALL
min(expr) − minimum value within the group FROM city SELECT name
sum(expr) − sum of values within the group WHERE country_id IN ( FROM skating
SELECT country_id WHERE country = 'DE';
EXAMPLE QUERIES FROM country
INTERSECT
Find out the number of cities: WHERE population > 20000000
SELECT COUNT(*) ); INTERSECT returns only rows that appear in both result sets.
FROM city;
This query displays German cyclists who are also German skaters at the same time:
Find out the number of cities with non-null ratings: CORRELATED
SELECT COUNT(rating) SELECT name
A correlated subquery refers to the tables introduced in the outer query. A correlated
FROM city; FROM cycling
subquery depends on the outer query. It cannot be run independently from the outer
Find out the number of distinctive country values: WHERE country = 'DE'
quer y.
SELECT COUNT(DISTINCT country_id) INTERSECT
This query finds cities with a population greater than the average population in the
FROM city; SELECT name
country:
FROM skating
Find out the smallest and the greatest country populations: SELECT *
SELECT MIN(population), MAX(population) WHERE country = 'DE';
FROM city main_city
FROM country; WHERE population > ( EXCEPT
Find out the total population of cities in respective countries: SELECT AVG(population) FROM city average_city WHERE EXCEPT returns only the rows that appear in the first result set but do not appear in the
SELECT country_id, SUM(population) average_city.country_id = main_city.country_id second result set.
FROM city
GROUP BY country_id; This query displays German cyclists unless they are also German skaters at the same
);
time:
Find out the average rating for cities in respective countries if the average is above 3.0: SELECT name
SELECT country_id, AVG(rating) This query finds countries that have at least one city: FROM cycling
FROM city SELECT name WHERE country = 'DE'
GROUP BY country_id FROM country EXCEPT / MINUS
HAVING AVG(rating) > 3.0; WHERE EXISTS ( SELECT name
SELECT * FROM skating
FROM city WHERE country = 'DE';
WHERE country_id = country.id
);

You might also like