SQL COMMANDS CHEAT SHEET
Command Syntax Description
AND SELECT column_name(s) It is an operator that is
FROM table-name used to combine two
WHERE column1 = value1 AND column2 = value2 conditions.
AS SELECT column_name AS ‘Alias’ It is a keyword in SQL
FROM table_name; used to rename a
column or table using
an alias name.
AVG SELECT AVG(column_name) It is used to aggregate a
FROM table_name; numeric column and
return its average.
ALTER ALTER TABLE table_name It is used to add
TABLE ADD column_name datatype; columns to a table in a
database.
BETWEEN SELECT column_name(s) It is an operator used to
FROM table_name filter the results within a
WHERE column_name certain range.
BETWEEN value1 AND value2
CASE SELECT column_name, It is a statement used to
CASE WHEN condition THEN ‘Result1’ WHEN condition create different outputs
THEN ‘Result2’ ELSE ‘Result3’ inside a SELECT
END statement.
FROM table_name;
CHAR CHAR [ (n) ] It is a fixed-length non-
Unicode string of length
Example 10.
CHAR (10)
Use char when the sizes
This means you must include 10 characters. of the column data
entries are consistent.
CHAR ( ) NOT CHAR [ (n) ] NOT NULL By default, a column
NULL can hold NULL values.
Example The NOT NULL
CHAR (1) NOT NULL constraint enforces a
column to NOT accept
This means that at least one character must be type because the NULL values. This
field can’t be left blank. enforces a field to
always contain a value,
which means that you
cannot insert a new
record, or update a
Page 1 of 9
SQL COMMANDS CHEAT SHEET
Command Syntax Description
record without adding a
value to this field.
Page 2 of 9
SQL COMMANDS CHEAT SHEET
Command Syntax Description
COUNT SELECT COUNT (column_name) It is a function that takes
FROM table_name; the name of a column as
an argument and counts
the number of rows
where the column is not
NULL.
CREATE CREATE TABLE table_name ( It is used to create a
TABLE Column1 datatype, new table in a database
Column2 datatype, and specify the name of
Column3 datatype the table and the
); columns inside it.
CREATE CREATE INDEX index_name It is used to retrieve data
INDEX ON table_name (column1, column2, …) from the database more
quickly than otherwise.
The users cannot see the
indexes and are just
used to speed up
searches/queries.
CREATE CREATE UNIQUE INDEX index_name It guarantees that the
UNIQUE ON table_name (column1, column2, …) index key contains no
INDEX duplicate values;
therefore, every row in
the table is somehow
unique.
CREATE CREATE VIEW view_name AS It is used to create a
VIEW SELECT column1, column2, ... view that can be used to
FROM table_name see data.
WHERE condition;
DELETE DELETE FROM table_name It is used
WHERE condition; to delete existing
records in a table.
Note: Be careful when deleting records from a table! Notice
the WHERE clause in the DELETE statement.
The WHERE clause specifies which record(s) should be
deleted. If you omit the WHERE clause, all records in the table
will be deleted!
DROP INDEX DROP INDEX index_name It is used to delete an
ON table_name index in a table.
Page 3 of 9
SQL COMMANDS CHEAT SHEET
Command Syntax Description
DROP TABLE DROP TABLE #Temp_Table It is used to delete a
table and place it at the
end of a query.
DROP TABLE DROP TABLE IF EXISTS #Temp_Table It is used to delete a
IF table and place it at the
beginning of a query.
Very helpful when a
query fails but creates a
temporary table.
HAVING SELECT column_name, COUNT(*) It is used in SQL
FROM table-name because the WHERE
GROUP BY column_name; keyword cannot be used
HAVING COUNT(*)>value; in aggregating
functions.
INNER JOIN SELECT column_name(s) It combines rows from
FROM table1 different tables if the
INNER JOIN table2 ON table1.table1_id = table2.table1_id; join condition goes
TRUE.
Below is for adding additional tables
INSERT INSERT INTO table_name It is used to add new
(Column1, Column2, Column3) rows to a table.
VALUES (Value1, ‘Value2’, Value3);
IS NULL / IS SELECT column_name(s) It is an operator used
NOT NULL FROM table_name with the WHERE clause
WHERE column_name is NULL; to check for empty
values.
GROUP BY SELECT column_name, COUNT(*) It is a clause in SQL
FROM table-name used for aggregate
GROUP BY column_name; functions in
Page 4 of 9
SQL COMMANDS CHEAT SHEET
Command Syntax Description
collaboration with the
SELECT statement.
Page 5 of 9
SQL COMMANDS CHEAT SHEET
Command Syntax Description
LIKE SELECT column_name(s) It is a special operator
FROM table_name used with the WHERE
WHERE colunn_name LIKE pattern; clause to search for a
specific pattern in a
column.
LIMIT SELECT column_name(s) The clause specifies the
FROM table_name maximum number of
LIMIT number; rows the result set must
have.
MAX SELECT MAX (column_name) It is a function that takes
FROM table_name; the number of columns
as an argument and
returns the largest value
among them.
MIN SELECT MIN (column_name) It is a function that takes
FROM table_name; the number of columns
as an argument and
returns the smallest
value among them.
OR SELECT column_name It is an operator that
FROM table_name filters the result set to
WHERE column_name = value1 OR column.name = value2; contain only the rows
where either condition
is TRUE.
ORDER BY SELECT column_name It is a clause used to sort
FROM table_name the results set by a
ORDER BY column_name ASC | DESC; particular column either
numerically or
alphabetically.
OUTER JOIN SELECT column_name(s) It combines rows from
FROM table1 different tables even if
LEFT JOIN table2 the condition is NOT
ON table1.column_name = table2.column_name; TRUE.
ROUND SELECT ROUND(column_name.integer) It is a function that takes
FROM table_name; the column name and an
integer as an argument
and rounds the values in
a column to the number
of decimal places
Page 6 of 9
SQL COMMANDS CHEAT SHEET
Command Syntax Description
specified by an integer.
Page 7 of 9
SQL COMMANDS CHEAT SHEET
Command Syntax Description
SELECT SELECT column_name It is a statement that is
FROM table_name used to fetch data from
a database.
To show data in a
database.
SELECT SELECT DISTINCT column_name It is used to specify that
DISTINCT FROM table_name the statement is a query
that returns unique
values in specified
columns.
SELECT INTO SELECT * It creates a Copy of a
INTO #Temp_Table Table into a Temporary
FROM Some_Table Table. You can also
select specific Columns
to copy.
(Can be combined with
other Commands)
SUM SELECT SUM(column_name) It is a function used to
FROM table_name; return the sum of values
from a particular
column.
UPDATE UPDATE table_name It is used to edit rows in
SET some_column = some_value a table.
WHERE some_column = some_value;
VARCHAR VARCHAR [ (n) ] This is a variable-length
non-Unicode string with
Example a maximum length of
VARCHAR (10) 10. This means the
actual length will
This means you can add up to 10 characters. depend upon the data.
Use varchar when the
sizes of the column data
entries vary
considerably.
WHERE SELECT column_name(s) It is a clause used to
FROM table_Name filter the result set to
WHERE column_name operator value; include the rows
Page 8 of 9
SQL COMMANDS CHEAT SHEET
Command Syntax Description
WHERE the condition
is TRUE.
WITH WITH temporary_name AS ( It is used to store the
SELECT * result of a particular
FROM table_name) query in a temporary
SELECT * table using an alias.
FROM temporary_name
WHERE column_name operator value;
Page 9 of 9