When it comes to SQL, it doesn’t matter if you’re a student or system administrator; The SQL documentation is huge, and when you’re working on a time crunch you want to find that one key command quickly.
Fear not; we’ve got you covered. Whether you need a refresher on SQL commands or are new to SQL and its various flavors, this SQL query cheat sheet is for you. Any IT professional would do well to keep this SQL language cheat sheet within reach.
We have endeavored to use consistent SQL syntax across different implementations (mainly MySQL and PostgreSQL), and we’ll point out discrepancies between other SQL implementations to you wherever appropriate.
Download this SQL cheat sheet here. Without further ado, let’s dive in.
SQL Cheat Sheet Search
Search our SQL cheat sheet to find the right cheat for the term you're looking for. Simply enter the term in the search bar and you'll receive the matching cheats available.
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
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.
SQL Command Generator
Say goodbye to the hassle of trying to remember the exact syntax for your SQL commands! With our SQL Command Generator, you can simply say what you need SQL to do, and we will generate the command for you.
Basic SQL Syntax
This section is the essential SQL syntax cheat sheet. If you’re short on time, read this section first.
|Add columns of a specified |
|A keyword in SQL to rename a column or table using an alias name|
|Create different outputs inside a |
|Create a new table in a database and specify the name of the table and columns of a specified |
|Remove the rows from a table|
|Use it like the |
|Add new rows to a table with specified values|
|Fetch data from a database; the |
|Return unique, non-repeating values in specified columns|
|Edit rows in a table|
|Process the result of a query |
• For comments spanning several lines:
• For comments on the same line as the command:
Data Types in SQL
The data type of a SQL column identifies how SQL will interact with the stored data. SQL is a strongly typed language, so it’s important to tell apart various data types.
Strongly Typed Languages vs Weakly Typed Languages
In computer programming, a programming language is strongly typed if it demands the specification of data types.
In strongly typed languages, once a type is assigned to a variable at runtime or compile time, it retains that type and can’t be intermingled in expressions with other types easily. You cannot assign an integer to a string variable in a strongly typed language. Boolean variables can only hold Boolean values, and writing any other value to it may throw errors.
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.
MySQL Data Types (Version 8.0)
MySQL has three main data types: string, numeric, and date and time:
|A fixed-length string: can contain letters, numbers, and special characters. The |
|A variable-length string: can contain letters, numbers, and special characters. The |
|Equal to |
|Equal to |
|For BLOBs (Binary Large Objects). Max length: 255 bytes.|
|Hold a string with a maximum length of 255 characters.|
|Hold a string with a maximum length of 65,535 bytes.|
|For BLOBs (Binary Large Objects). Hold up to 65,535 bytes of data.|
|Hold a string with a maximum length of 16,777,215 characters.|
|For BLOBs (Binary Large Objects). Hold up to 16,777,215 bytes of data.|
|Hold a string with a maximum length of 4,294,967,295 characters.|
|For BLOBs (Binary Large Objects). Hold up to 4,294,967,295 bytes of data.|
|A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 values in an ENUM list. If you insert a value that is not in the list, you insert a blank value. SQL sorts the values in the order you enter them.|
|A string object that can have 0 or more values, chosen from a list of possible values. You can list up to 64 values in a SET list.|
We leave the “Alias” field blank if a data type has no alias.
|A bit-value type. The |
|A tiny integer. The signed is from -128 to 127. The unsigned range is from 0 to 255. The |
|Zero = false, nonzero values = true.|
|A small integer. The signed range is from -32768 to 32767. The unsigned range is from 0 to 65535. The |
|A medium-sized integer. The signed range is from -8388608 to 8388607. The unsigned range is from 0 to 16777215. The |
|A medium-sized integer. The signed range is from -2147483648 to 2147483647. The unsigned range is from 0 to 4294967295. The |
|A large integer. The signed range is from -9223372036854775808 to 9223372036854775807. The unsigned range is from 0 to 18446744073709551615. The |
|Floating point number. The parameter |
Future MySQL versions (beyond MySQL 8.0.17) will remove this syntax.
|Floating point number. MySQL uses the |
|Fixed-point number. The parameter |
Note: All the numeric data types may have an extra option:
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.
Date and time
ON UPDATE in the column definition helps you get automatic initialization and updating to the current date and time.
fsp (fractional seconds precision, in microseconds) value must be 0–6. For example, set
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.
|Date. Format: |
|Date and time combination. Format: |
|Timestamp. MySQL stores |
|Time. Format: |
|Year in four-digit format. MySQL 8.0 does not support a two-digit year format.|
PostGreSQL Data Types (Version 15)
We leave the “Aliases” field blank if a data type has no alias.
|Signed eight-byte integer|
|Auto-incrementing eight-byte integer|
|Fixed-length bit string|
|Variable-length bit string|
|Logical Boolean (true/false)|
|Rectangular box on a plane|
|Binary data (“byte array”)|
|Fixed-length character string|
|Variable-length character string|
|IPv4 or IPv6 network address|
|Circle on a plane|
|Calendar date (year, month, day)|
|Double precision floating-point number (eight bytes)|
|IPv4 or IPv6 host address|
|Signed four-byte integer|
|Textual JSON data|
|Binary JSON data, decomposed|
|Infinite line on a plane|
|Line segment on a plane|
|MAC (Media Access Control) address|
|MAC (Media Access Control) address (EUI-64 format)|
|Exact numeric of selectable precision|
|Geometric path on a plane|
|PostgreSQL Log Sequence Number|
|User-level transaction ID snapshot|
|Geometric point on a plane|
|Closed geometric path on a plane|
|Single precision floating-point number (four bytes)|
|Signed two-byte integer|
|Auto-incrementing two-byte integer|
|Auto-incrementing four-byte integer|
|Variable-length character string|
|Time of day (no time zone)|
|Time of day, including time zone|
|Date and time (no time zone)|
|Date and time, including time zone|
|Text search query|
|Text search document|
|Universally unique identifier|
This subsection is a basic SQL operators cheat sheet, where you learn to create complex Boolean expressions in SQL queries.
|Combine two conditions|
|Filter the result within a certain range|
|Check for empty values in conjunction with the |
|Check for the absence of empty values in conjunction with the |
|Search for a specific pattern in a column in conjunction with the |
|OR||Filter the result set to contain only the rows where either condition is |
|Combine the results of two or more |
|Combine the results of two or more |
SQL functions help you compute and analyze the contents of database tables.
Here are some common SQL functions:
|Aggregate a numeric column and return its arithmetic mean, ignoring |
Once a condition is true, it will stop reading and return the result. If no conditions are
|Convert a |
|(MySQL) Return the length of a |
|Return the first non-null value in a list|
|Take the name of a column as an argument and count the number of rows when the column is not |
|Return the first value of the selected column|
|Return the last value of the selected column|
|Convert string values in the selected column to lowercase|
|(SQL Server) Return the length of a |
|Take at least one column as an argument and return the largest value among them|
|Take at least one column as an argument and return the smallest value among them|
|Take the column name and an integer as an argument, and round the values in a column to the number of decimal places specified by an integer|
|Extract some characters from a string, where |
|Return the sum of values from a particular column|
|Convert string values in the selected column to uppercase|
|Return the statistical variance|
A SQL clause presents the results of a SQL query in a way you specify.
|Specify the maximum number of rows the result set must have. Some SQL implementations have |
|Used for aggregate functions in collaboration with the |
|Sort the result set by a particular column either numerically or alphabetically.|
|Filter the result set to include the rows where the condition is |
Combining two tables in SQL is easy:
|Select records that have matching values in both tables|
|Combine all records from the left side and any matching rows from the right table.|
|Combine all rows from the right side and any matching rows from the left table.|
|Return all records whether the records in the left (|
|Combine each row of the first table (|
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:
|Create a view from the SQL query beginning with |
|Update a view created from the SQL query beginning with |
|Delete a view|
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.
|Creates a unique index on a table, allowing duplicate values|
|Create a unique index on a table, forbidding duplicate values|
|Delete an index in a table|
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.
The syntax is:
[CREATE|ALTER] TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
The table below lists common constraints in SQL:
|Ensure that a column cannot have a |
|Ensure that all values in a column are different|
|A combination of |
|Prevent actions that would destroy links between tables|
|Ensure that the values in a column satisfy a specific condition|
|Set a default value for a column that contains no specified value|
|Allow the automatic generation of a unique number when inserting a new record into a table.|
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.
Below we list the top SQL transactional commands:
|Save changes invoked by a transaction to the database|
|Undo transactions not yet saved to the database|
|Roll the transaction back to a certain point without rolling back the entire transaction|
|Initiate a database transaction, and specify characteristics for the transaction that follows.|
For example, you can specify a transaction to be
SQL Performance Tuning Tips
As this article is a SQL basics cheat sheet, we present the following SQL performance optimization tips without elaboration.
- Add missing indexes and check for unused indexes
SELECTfields instead of
- Avoid using multiple
- Create joins with
- Avoid too many
HAVINGto define filters
- Use wildcards at the end of a phrase only
LIMITto sample query results
- Minimize the usage of query hints
- Minimize large write operations
- Run the query during off-peak hours and analyze wait statistics
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: