0% found this document useful (0 votes)
20 views4 pages

Learn SQL - Queries Cheatsheet - Codecademy

The document is a SQL queries cheatsheet from Codecademy, outlining various SQL operators and clauses such as AND, OR, LIKE, and DISTINCT. It provides example queries demonstrating how to use these features to filter and manipulate data in SQL databases. Key concepts include wildcard usage, the SELECT statement, and the ORDER BY and LIMIT clauses for sorting and limiting results.

Uploaded by

Federico Massaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Learn SQL - Queries Cheatsheet - Codecademy

The document is a SQL queries cheatsheet from Codecademy, outlining various SQL operators and clauses such as AND, OR, LIKE, and DISTINCT. It provides example queries demonstrating how to use these features to filter and manipulate data in SQL databases. Key concepts include wildcard usage, the SELECT statement, and the ORDER BY and LIMIT clauses for sorting and limiting results.

Uploaded by

Federico Massaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

04/11/24, 12:37 Learn SQL: Queries Cheatsheet | Codecademy

Cheatsheets / Learn SQL

Queries

AND Operator

The AND operator allows multiple conditions to be SELECT model


combined. Records must match both conditions that
FROM cars
are joined by AND to be included in the result set.
The given query will match any car that is blue and WHERE color = 'blue'
made after 2014. AND year > 2014;

AS Clause

Columns or tables can be aliased using the AS clause. SELECT name AS 'movie_title'
This allows columns or tables to be specifically
FROM movies;
renamed in the returned result set. The given query will
return a result set with the column for name renamed
to movie_title .

OR Operator

The OR operator allows multiple conditions to be SELECT name


combined. Records matching either condition joined by
FROM customers
the OR are included in the result set. The given query
will match customers whose state is either 'CA' or WHERE state = 'CA'
'NY' . OR state = 'NY';

% Wildcard

The % wildcard can be used in a LIKE operator SELECT name


pattern to match zero or more unspecified character(s).
FROM movies
The given query will match any movie that begins with
The , followed by zero or more of any characters. WHERE name LIKE 'The%';

https://www.codecademy.com/learn/learn-sql/modules/learn-sql-queries/cheatsheet 1/4
04/11/24, 12:37 Learn SQL: Queries Cheatsheet | Codecademy

SELECT Statement

The SELECT * statement returns all columns from SELECT *


the provided table in the result set. The given query will
FROM movies;
fetch all columns and records (rows) from the movies
table.

_ Wildcard

The _ wildcard can be used in a LIKE operator SELECT name


pattern to match any single unspecified character. The
FROM movies
given query will match any movie which begins with a
single character, followed by ove . WHERE name LIKE '_ove';

ORDER BY Clause

The ORDER BY clause can be used to sort the SELECT *


result set by a particular column either alphabetically
FROM contacts
or numerically. It can be ordered in two ways:
DESC is a keyword used to sort the results in ORDER BY birth_date DESC;
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 SELECT name


WHERE clause to match a specified pattern. The
FROM movies
given query will match any movie that begins with Star
in its title. WHERE name LIKE 'Star%';

https://www.codecademy.com/learn/learn-sql/modules/learn-sql-queries/cheatsheet 2/4
04/11/24, 12:37 Learn SQL: Queries Cheatsheet | Codecademy

DISTINCT Clause

Unique values of a column can be selected using a SELECT DISTINCT city


DISTINCT query. For a table contact_details
FROM contact_details;
having five 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 filter by a SELECT *


range of values. The range of values can be text,
FROM movies
numbers, or date data. The given query will match any
movie made between the years 1980 and 1990, WHERE year BETWEEN 1980 AND 1990;
inclusive.

LIMIT Clause

The LIMIT clause is used to narrow, or limit, a result SELECT *


set to the specified number of rows. The given query
FROM movies
will limit the result set to 5 rows.
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
combination with the WHERE clause. The given WHERE address IS NOT NULL;
query will match all addresses where the address has a
value or is not NULL .

https://www.codecademy.com/learn/learn-sql/modules/learn-sql-queries/cheatsheet 3/4
04/11/24, 12:37 Learn SQL: Queries Cheatsheet | Codecademy

WHERE Clause

The WHERE clause is used to filter records (rows) SELECT title


that match a certain condition. The given query will
FROM library
select all records where the pub_year equals 2017 .
WHERE pub_year = 2017;

Print Share

https://www.codecademy.com/learn/learn-sql/modules/learn-sql-queries/cheatsheet 4/4

You might also like