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

Learn SQL_ Queries Cheatsheet _ Codecademy

This document provides a cheatsheet for SQL queries, detailing various operators and clauses such as AND, OR, AS, and LIKE. It explains how to filter records, alias columns, sort results, and handle NULL values. Each section includes example queries demonstrating the usage of these SQL features.

Uploaded by

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

Learn SQL_ Queries Cheatsheet _ Codecademy

This document provides a cheatsheet for SQL queries, detailing various operators and clauses such as AND, OR, AS, and LIKE. It explains how to filter records, alias columns, sort results, and handle NULL values. Each section includes example queries demonstrating the usage of these SQL features.

Uploaded by

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

Cheatsheets / Learn SQL

Queries

AND Operator

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

AS Clause

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

OR Operator

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

% Wildcard

The % wildcard can be used in a LIKE operator pattern to match zero or more SELECT name
unspecified character(s). 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%';

SELECT Statement

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

_ Wildcard

The _ wildcard can be used in a LIKE operator pattern to match any single SELECT name
unspecified character. The 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 SELECT *
either alphabetically or numerically. It can be ordered in two ways:
FROM contacts
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). ORDER BY birth_date DESC;

LIKE Operator

The LIKE operator can be used inside of a WHERE clause to match a SELECT name
specified pattern. The given query will match any movie that begins with Star in its
FROM movies
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
contact_details having five rows in which the city column contains Chicago,
FROM contact_details;
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 range of values. The range of SELECT *
values can be text, numbers, or date data. The given query will match any movie
FROM movies
made 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 specified number SELECT *
of rows. The given query will limit the result set to 5 rows.
FROM movies
LIMIT 5;

NULL Values

Column values can be NULL , or have no value. These records can be matched SELECT address
(or not matched) using the IS NULL and IS NOT NULL operators in
FROM records
combination with the WHERE clause. The given query will match all addresses
where the address has a value or is not NULL .
WHERE address IS NOT NULL;
WHERE Clause

The WHERE clause is used to filter records (rows) that match a certain SELECT title
condition. The given query will select all records where the pub_year equals
FROM library
2017 .
WHERE pub_year = 2017;

Print Share

You might also like