SQL Cheat Sheet
SQL Cheat Sheet
What Is SQL?
SQL is short for Structured Query Language. Its chief function is managing structured data
on a relational database management system (RDBMS), usually arranged in tables. SQL is
case-insensitive, but it’s common to capitalize SQL keywords such as SELECT and FROM.
Suppose you want to execute multiple SQL statements in the same server call. In that case,
\
some database administration tools, such as MySQL Workbench, require a semicolon (;) at
the end of each SQL statement to separate them.
Screenshot of MySQL Workbench in action
In weakly typed languages, once a type is assigned to a variable at runtime or compile time,
it can be intermingled in expressions with other types easily. Here, an integer assigned to a
string variable may get converted into the character(s) representing the integer. You can
also assign a string or integer to a variable previously used to hold a Boolean value.
The same name may map to different data types in other SQL implementations. Therefore,
always consult the relevant documentation (MySQL, PostgreSQL).
String
Data type Description
BINARY(size) Equal to CHAR() but stores binary byte strings. The size
parameter specifies the column length in bytes. The default
is 1.
VARBINARY(size) Equal to VARCHAR() but stores binary byte strings. The
size parameter specifies the maximum column length in
bytes.
TINYBLOB For BLOBs (Binary Large Objects). Max length: 255 bytes.
Numeric
We leave the “Alias” field blank if a data type has no alias.
Note: All the numeric data types may have an extra option: UNSIGNED or ZEROFILL. If you
add the UNSIGNED option, MySQL disallows negative values for the column. If you add the
ZEROFILL option, MySQL automatically adds the UNSIGNED attribute to the column.
Below, the fsp (fractional seconds precision, in microseconds) value must be 0–6. For
example, set fsp to 1 to encapsulate 0.1–0.9 seconds and 2 for 0.01–0.99 seconds. A value
of 0 indicates the absence of a fractional part. If omitted, the default precision is 0.
TIME [ (p) ] WITH TIME TIMETZ Time of day, including time zone
ZONE
\ TIMESTAMP [ (p) ] [ Date and time (no time zone)
without time zone ]
TIMESTAMP [ (p) ] WITH TIMESTAMPTZ Date and time, including time zone
TIME ZONE
TSQUERY Text search query
SQL Operators
This subsection is a basic SQL operators cheat sheet, where you learn to create complex
Boolean expressions in SQL queries.
SQL Functions
SQL functions help you compute and analyze the contents of database tables.
Aliases: MID(),
SUBSTR()
SUM() SELECT Return the sum of values
SUM(column_name) FROM from a particular column
table_name;
UCASE() SELECT Convert string values in the
UCASE(column_name) selected column to
FROM table_name; uppercase
VAR() SELECT Return the statistical
VAR(column_name) FROM variance
table_name;
SQL Clauses
A SQL clause presents the results of a SQL query in a way you specify.
SQL Joins
Combining two tables in SQL is easy:
\
Command Syntax Description
INNER JOIN SELECT column_name(s) Select records that have
FROM table_1 JOIN matching values in both
table_2 ON tables
table_1.column_name =
table_2.column_name;
LEFT JOIN SELECT column_name(s) Combine all records from
FROM table_1 LEFT the left side and any
JOIN table_2 ON matching rows from the right
table_1.column_name = table.
table_2.column_name;
LEFT OUTER JOIN and
LEFT JOIN are the same.
RIGHT JOIN SELECT column_name(s) Combine all rows from the
FROM table_1 RIGHT right side and any matching
JOIN table_2 ON rows from the left table.
table_1.column_name =
table_2.column_name; RIGHT OUTER JOIN and
RIGHT JOIN are the same.
FULL JOIN SELECT column_name(s) Return all records whether
FROM table1 FULL the records in the left
OUTER JOIN table2 ON (table1) and right
table1.column_name = (table2) tables match.
table2.column_name FULL OUTER JOIN and
WHERE condition; FULL JOIN are the same.
CROSS JOIN SELECT * FROM table1 Combine each row of the
CROSS JOIN table2 first table (table1) with
each row of the second
table (table2).
SQL Views
In SQL, a view is a virtual table based on the results of an SQL query. A view contains rows
and columns, just like a real table. The fields in a view are fields from one or more real tables
in the database. You can add SQL statements and functions to a view and present the data
as if the data were coming from a single table.
Here are the most important functions for manipulating SQL views:
SQL Indexes
Indexes are for speeding up data retrieval from a database. The users cannot see the
indexes. Updating a table with indexes takes longer than updating a table without (because
the indexes also need an update). So, only create indexes on the columns against which
users frequently search.
/* SQL Server */
DROP INDEX
table_name.index_name
;
/* DB2/Oracle */
DROP INDEX
index_name;
/* MySQL */
ALTER TABLE
table_name DROP INDEX
index_name;
SQL Constraints
Constraints are for specifying rules for data in a table. Use them with the CREATE TABLE
statement for a new table or the ALTER TABLE statement for an existing table.
Command Description
NOT NULL Ensure that a column cannot have a NULL
value
UNIQUE Ensure that all values in a column are
different
PRIMARY KEY A combination of NOT NULL and UNIQUE:
uniquely identifies each row in a table.
FOREIGN KEY Prevent actions that would destroy links
between tables
CHECK Ensure that the values in a column satisfy a
specific condition
DEFAULT Set a default value for a column that
contains no specified value
AUTO_INCREMENT Allow the automatic generation of a unique
number when inserting a new record into a
table.
SQL Transactions
A transaction is the propagation of one or more changes to the database. For example, you
perform a transaction if you perform create, update, and delete operations on a table.
Conclusion
This SQL command cheat sheet covers most SQL database tasks. We hope it has helped
you solve your problems at hand. Bookmark the documentation links for your SQL
implementation, such as MySQL or PostgreSQL. Remember to check out our articles on
SQL and our beginner-friendly cyber security courses, which cover SQL injection attacks: