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

SQL Basic Cheat Sheet

This document provides a cheat sheet on SQL commands and operations for querying and filtering data from single and multiple database tables. It covers basic SQL statements like SELECT and WHERE as well as common operators like comparison, text matching, and BETWEEN. It also summarizes different types of joins like INNER JOIN, FULL JOIN, LEFT JOIN, and RIGHT JOIN that allow querying data across related tables.

Uploaded by

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

SQL Basic Cheat Sheet

This document provides a cheat sheet on SQL commands and operations for querying and filtering data from single and multiple database tables. It covers basic SQL statements like SELECT and WHERE as well as common operators like comparison, text matching, and BETWEEN. It also summarizes different types of joins like INNER JOIN, FULL JOIN, LEFT JOIN, and RIGHT JOIN that allow querying data across related tables.

Uploaded by

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

SQL Cheat Sheet

SQL FILTERING THE OUTPUT QUERYING MULTIPLE TABLES


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

LearnSQL.com is owned by Vertabelo SA


Try out the interactive SQL Basics course at LearnSQL.com, and check out our other SQL vertabelo.com | CC BY-NC-ND Vertabelo SA
SQL Cheat Sheet
CROSS JOINreturns all possible combinations of
rows from both tables. There are two
syntaxes available.
SELECT city.name,
country.name FROM city
CROSS JOIN country;

SELECT city.name, country.name


FROM city, country;
CITY COUNTRY
id name country_id id name
1 Paris 1 1 France
1 Paris 1 2 Germany
2 Berlin 2 1 France
2 Berlin 2 2 Germany

NATURAL JOIN
NATURAL JOINwill join tables by all columns
with the same name.
SELECT city.name,
country.name FROM city
NATURAL JOIN country;
CITY COUNTRY
country_id id name name id
6 6 San Marino San Marino 6
7 7 Vatican City Vatican City 7
5 9 Greece Greece 9
10 11 Monaco Monaco 10
NATURAL JOIN used these columns
to match rows: city.id,
city.name, country.id,
country.name NATURAL JOIN is
very rarely used in practice.

LearnSQL.com is owned by Vertabelo SA


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

LearnSQL.com is owned by Vertabelo SA


Try out the interactive SQL Basics course at LearnSQL.com, and check out our other SQL vertabelo.com | CC BY-NC-ND Vertabelo SA

You might also like