This is a draft cheat sheet. It is a work in progress and is not finished yet.
Find All
SELECT * FROM <table name>; |
|
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, description, price FROM products; |
Alias
SELECT <column name> AS <alias> FROM <table name>; |
SELECT name AS "Product Name" FROM products; |
Finding Data
SELECT <columns> FROM <table> WHERE <column name> = <value>; |
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 |
Conditional Statement
AND: Both conditions have to be true |
SELECT <columns> FROM <table> WHERE <condition 1> AND <condition 2> ...;
|
OR: Either condition is true |
SELECT <columns> FROM <table> WHERE <condition 1> OR <condition 2> ...;
|
|
|
Pattern Matching
% Acts as a wildcard |
LIKE Searches for a pattern |
SELECT title FROM movies WHERE title LIKE "Alien%";
|
Search in a Set of Values
IN Finds all rows with that value |
SELECT title FROM courses WHERE topic IN ("JavaScript", "Databases", "CSS");
|
NOT IN Finds rows that do not have the value |
SELECT title FROM courses WHERE topic NOT IN ("SQL", "NoSQL");
|
BETWEEN Searches a range |
SELECT <columns> FROM <table> WHERE <column> BETWEEN <lesser value> AND <greater value>;
|
SELECT * FROM movies WHERE release_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 <table> WHERE <column> IS NOT NULL;
|
|
|
|