Show Menu
Cheatography

SQL Cheat Sheet (DRAFT) by

Sql Basic CheatSheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Find All

SELECT * FROM <table name>;
SELECT * FROM products;
SELECT: Asks for a column name
FROM: Asks from what table
The semi-colon ( ; ) terminates the statement.

Retrieving Specific Columns Informatin

SELECT <column name> FROM <table name>;
SELECT name FROM products;
Add multiple columns separated by ( , )
SELECT name, descri­ption, price FROM products;

Alias

SELECT <column name> AS <al­ias> FROM <table name>;
SELECT name AS "­Product Name" FROM products;

Finding Data

SELECT <co­lum­ns> FROM <ta­ble> WHERE <column name> = <va­lue­>;
SELECT name AS "­Product Name" FROM products WHERE stock_­count = 0;

Operators

Equality
=
Equals
Inequality
!=
Not Equal
Relational
<
Less Than
 
<=
Less than or equal to
 
>
Greater than
 
>=
Greater than or equal to

Condit­ional Statement

AND: Both conditions have to be true
SELECT <co­lum­ns> FROM <ta­ble> WHERE <co­ndition 1> AND <co­ndition 2> ...;
OR: Either condition is true
SELECT <co­lum­ns> FROM <ta­ble> WHERE <co­ndition 1> OR <co­ndition 2> ...;
 

Pattern Matching

% Acts as a wildcard
LIKE Searches for a pattern
SELECT title FROM movies WHERE title LIKE "­Ali­en%­";

Search in a Set of Values

IN Finds all rows with that value
SELECT title FROM courses WHERE topic IN ("Ja­vaS­cri­pt", "­Dat­aba­ses­", "­CSS­");
NOT IN Finds rows that do not have the value
SELECT title FROM courses WHERE topic NOT IN ("SQ­L", "­NoS­QL");
BETWEEN Searches a range
SELECT <co­lum­ns> FROM <ta­ble> WHERE <co­lum­n> BETWEEN <lesser value> AND <gr­eater value>;
SELECT * FROM movies WHERE releas­e_year BETWEEN 2000 AND 2010;
The IN keyword is followed by ()

Missing Value

IS / IS NOT
IS NULL and IS NOT NULL check for empty string
SELECT * FROM <ta­ble> WHERE <co­lum­n> IS NOT NULL;