Learn SQL - Queries Cheatsheet - Codecademy PDF
Learn SQL - Queries Cheatsheet - Codecademy PDF
Queries
AND Operator
The AND operator allows multiple conditions to be
combined. Records must match both conditions that are SELECT model
joined by AND to be included in the result set. The
FROM cars
given query will match any car that is blue and made after WHERE color = 'blue'
2014. AND year > 2014;
% Wildcard
The % wildcard can be used in a LIKE operator
pattern to match zero or more unspeci ed character(s). SELECT name
The given query will match any movie that begins with FROM movies
The , followed by zero or more of any characters. WHERE name LIKE 'The%';
AS Clause
Columns or tables can be aliased using the AS clause.
This allows columns or tables to be speci cally renamed SELECT name AS 'movie_title'
in the returned result set. The given query will return a FROM movies;
result set with the column for name renamed to
movie_title .
OR Operator
The OR operator allows multiple conditions to be
combined. Records matching either condition joined by SELECT name
FROM customers
the OR are included in the result set. The given query
WHERE state = 'CA'
will match customers whose state is either 'CA' or
OR state = 'NY';
'NY' .
SELECT Statement
The SELECT * statement returns all columns from
the provided table in the result set. The given query will SELECT *
fetch all columns and records (rows) from the movies
FROM movies;
table.
_ Wildcard
The _ wildcard can be used in a LIKE operator
pattern to match any single unspeci ed character. The SELECT name
given query will match any movie which begins with a FROM movies
single character, followed by ove . WHERE name LIKE '_ove';
ORDER BY Clause
The ORDER BY clause can be used to sort the result
set by a particular column either alphabetically or SELECT *
numerically. It can be ordered in two ways: FROM contacts
ORDER BY birth_date DESC;
●
DESC is a keyword used to sort the results in
descending order.
●
ASC is a keyword used to sort the results in
ascending order (default).
LIKE Operator
The LIKE operator can be used inside of a WHERE
clause to match a speci ed pattern. The given query will SELECT name
FROM movies
match any movie that begins with Star in its title.
WHERE name LIKE 'Star%';
DISTINCT Clause
Unique values of a column can be selected using a
DISTINCT query. For a table SELECT DISTINCT city
FROM contact_details;
contact_details having ve rows in which the
city column contains Chicago, Madison, Boston,
Madison, and Denver, the given query would return:
●
Chicago
●
Madison
●
Boston
●
Denver
BETWEEN Operator
The BETWEEN operator can be used to lter by a
range of values. The range of values can be text, numbers, SELECT *
or date data. The given query will match any movie made FROM movies
between the years 1980 and 1990, inclusive. WHERE year BETWEEN 1980 AND 1990;
LIMIT Clause
The LIMIT clause is used to narrow, or limit, a result
set to the speci ed number of rows. The given query will SELECT *
limit the result set to 5 rows. FROM movies
LIMIT 5;
NULL Values
Column values can be NULL , or have no value. These
SELECT address
records can be matched (or not matched) using the IS
FROM records
NULL and IS NOT NULL operators in
WHERE address IS NOT NULL;
combination with the WHERE clause. The given query
will match all addresses where the address has a value or
is not NULL .
WHERE Clause
The WHERE clause is used to lter records (rows) that
match a certain condition. The given query will select all SELECT title
records where the pub_year equals 2017 .
FROM library
WHERE pub_year = 2017;