SQL Cheat Sheet with Commands & Description (2026)

โšก Smart Summary

This SQL Cheat Sheet collects the commands a working developer reaches for daily, arranged by task. Each section pairs the exact syntax with a plain description, covering database creation, data types, querying, filtering, joining, and indexing.

  • ๐Ÿ—„๏ธ Structure Commands: CREATE DATABASE, CREATE TABLE, ALTER, RENAME and DROP define and reshape the schema.
  • ๐Ÿ”ข Data Types: Numeric, text, date and binary types set the storage size and valid range of every column.
  • ๐Ÿ”Ž Retrieval Commands: SELECT with WHERE, IN, LIKE and REGEXP narrows a result set to the rows that matter.
  • โœ๏ธ Modification Commands: INSERT, UPDATE and DELETE change stored rows, and each accepts an optional condition.
  • ๐Ÿ“Š Aggregation Commands: GROUP BY with COUNT, MIN, MAX, SUM and AVG summarises data, and HAVING filters the groups.
  • ๐Ÿ”— Combination Commands: JOIN merges tables side by side, while UNION stacks the results of two queries.

SQL Cheat Sheet

Create Database and table commands

Every SQL session starts with a database and at least one table to hold the rows.

Command Description
CREATE DATABASE database1; Create database
CREATE DATABASE IF NOT EXISTS database1; IF NOT EXISTS lets you instruct MySQL server to check the existence of a database with a similar name prior to creating the database.
CREATE DATABASE IF NOT EXISTS database1 CHARACTER SET latin1 COLLATE latin1_swedish_ci; The latin1 character set uses the latin1_swedish_ci collation, which is the Swedish case insensitive order.
SHOW DATABASES; You can see a list of existing databases by running this SQL command.
CREATE TABLE [IF NOT EXISTS] TableName (fieldname dataType [optional parameters]) ENGINE = storage Engine; Create table syntax

SQL Data Types Cheat Sheet

The data type of a column fixes what it can store and how much space each row consumes.

Numeric Data types

Command Description
TINYINT( ) -128 to 127 normal, 0 to 255 UNSIGNED.
SMALLINT( ) -32768 to 32767 normal
0 to 65535 UNSIGNED.
MEDIUMINT( ) -8388608 to 8388607 normal
0 to 16777215 UNSIGNED.
INT( ) -2147483648 to 2147483647 normal
0 to 4294967295 UNSIGNED.
BIGINT( ) -9223372036854775808 to 9223372036854775807 normal
0 to 18446744073709551615 UNSIGNED.
FLOAT A small approximate number with a floating decimal point.
DOUBLE( , ) A large approximate number with a floating decimal point.
DECIMAL( , ) An exact fixed-point number. The preferred choice for storing currency values.

Text Data Types

Command Description
CHAR( ) A fixed length section from 0 to 255 characters long.
VARCHAR( ) A variable length section from 0 to 65535 characters long.
TINYTEXT A string with a maximum length of 255 characters.
TEXT A string with a maximum length of 65535 characters.
BLOB Binary data with a maximum length of 65535 bytes.
MEDIUMTEXT A string with a maximum length of 16777215 characters.
MEDIUMBLOB Binary data with a maximum length of 16777215 bytes.
LONGTEXT A string with a maximum length of 4294967295 characters.
LONGBLOB Binary data with a maximum length of 4294967295 bytes.

Date / Time data types

Command Description
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIMESTAMP YYYY-MM-DD HH:MM:SS, stored in UTC and converted to the session time zone.
TIME HH:MM:SS

Other data types

Command Description
ENUM To store one text value chosen from a list of predefined text values.
SET This is also used for storing text values chosen from a list of predefined text values. It can hold multiple values.
BOOL Synonym for TINYINT(1), used to store Boolean values.
BINARY Similar to CHAR; the difference is that text is stored in binary format.
VARBINARY Similar to VARCHAR; the difference is that text is stored in binary format.

SQL SELECT statement command

SELECT is the command used to read rows back out of a table.

Command Description
SELECT * FROM table1; Select every column from the table
SELECT t1, t2, t3, t4 FROM table1; We are only interested in getting the t1, t2, t3 and t4 fields.
SELECT CONCAT(t1, ' (', t3, ')'), t4 FROM table2; Joining column values into a single output column
SELECT column_name | value | expression [AS] alias_name; Alias field names syntax

SQL WHERE clause with AND, OR, IN, NOT IN commands

The WHERE clause filters rows, and the operators below can be combined inside it.

Command Description
SELECT * FROM tableName WHERE condition; WHERE clause syntax
SELECT * FROM table1 WHERE t1 = 2 AND t2 = 2008; WHERE clause combined with the AND logical operator
SELECT * FROM table1 WHERE t1 = 1 OR t1 = 2; WHERE clause combined with the OR logical operator
SELECT * FROM table2 WHERE t1 IN (1,2,3); WHERE clause combined with the IN keyword
SELECT * FROM table2 WHERE t1 NOT IN (1,2,3); WHERE clause combined with the NOT IN keyword
SELECT * FROM table2 WHERE t3 = 'Female'; WHERE clause combined with the equal (=) comparison operator
SELECT * FROM table3 WHERE t3 > 2000; WHERE clause combined with the greater than (>) comparison operator
SELECT * FROM table1 WHERE t1 <> 1; WHERE clause combined with the not equal to (<>) comparison operator

SQL Command INSERT INTO Table

Command Description
INSERT INTO table_name(column_1, column_2, ...) VALUES (value_1, value_2, ...); Basic syntax of the SQL INSERT command
INSERT INTO table1 (t1,t2,t3,t4) VALUES (X1,X2,X3,X4); Insert data into a table
INSERT INTO table_1 SELECT * FROM table_2; Inserting into a table from another table

SQL DELETE command

Command Description
DELETE FROM table_name [WHERE condition]; Delete a row in MySQL

SQL Update Command

Command Description
UPDATE table_name SET column_name = new_value [WHERE condition]; Update command syntax

ORDER BY in SQL: DESC & ASC command

Command Description
SELECT statement... [WHERE condition | GROUP BY field_name(s) HAVING condition] ORDER BY field_name(s) [ASC | DESC]; ORDER BY clause basic syntax
SELECT {fieldName(s) | *} FROM tableName(s) [WHERE condition] ORDER BY fieldname(s) ASC | DESC [LIMIT N]; DESC and ASC syntax

SQL GROUP BY and HAVING Clause command

GROUP BY collapses rows into groups, and HAVING filters those groups after they are formed.

Group by

Command Description
SELECT statements... GROUP BY column_name1[, column_name2, ...] [HAVING condition]; GROUP BY syntax

Grouping and aggregate functions

Command Description
SELECT t2, COUNT(t1) FROM table1 GROUP BY t2; Suppose we want the total number of t2 column values in our database.

HAVING clause

Command Description
SELECT * FROM table2 GROUP BY t1_id, t4 HAVING t1_id = x1; Returns all the t4 values for table2 rows whose t1_id is x1.

SQL Wildcards commands for Like, NOT Like, Escape, ( % ), ( _ )

Wildcards let LIKE match partial strings.

% the percentage wildcard command in MySQL

Command Description
SELECT statements... WHERE fieldname LIKE 'xxx%'; Basic syntax for the % percentage wildcard, which matches any number of characters

_ underscore wildcard command

Command Description
SELECT * FROM table1 WHERE t3 LIKE 'x2_'; The underscore matches exactly one character.

NOT Like wildcard command

Command Description
SELECT * FROM table1 WHERE t3 NOT LIKE 'x2_'; Suppose we want the table1 rows whose t3 value does not match the pattern.

Escape keyword wildcard command

Command Description
LIKE '67#%' ESCAPE '#'; Checks for the literal string “67%”, because # escapes the wildcard.

SQL Regular Expressions (REGEXP)

Command Description
SELECT statements... WHERE fieldname REGEXP 'pattern'; Basic syntax of a regular expression match

Regular expression Metacharacters

Command Description
* The asterisk (*) metacharacter matches zero or more instances of the string preceding it.
+ The plus (+) metacharacter matches one or more instances of the string preceding it.
? The question mark (?) metacharacter matches zero or one instance of the string preceding it.
. The dot (.) metacharacter matches any single character except a new line.
[abc] The charlist [abc] matches any of the enclosed characters.
[^abc] The charlist [^abc] matches any character excluding the ones enclosed.
[A-Z] The [A-Z] range matches any upper case letter.
[a-z] The [a-z] range matches any lower case letter.
[0-9] The [0-9] range matches any digit from 0 through to 9.
^ The caret (^) anchors the match at the beginning.
| The vertical bar (|) isolates alternatives.
[[:<:]] The [[:<:]] marker matches the beginning of words.
[[:>:]] The [[:>:]] marker matches the end of words.
[:class:] The [:class:] marker matches a character class, for example [:alpha:] for letters, [:space:] for white space, [:punct:] for punctuation and [:upper:] for upper case letters.

SQL Functions commands

String functions

Command Description
SELECT t1_id, t2, UCASE(t2) FROM table1; The UCASE function takes a string as a parameter and converts all the letters to upper case.

Numeric functions

Command Description Example
DIV Integer division SELECT 23 DIV 6;
/ Division SELECT 23 / 6;
- Subtraction SELECT 23 - 6;
+ Addition SELECT 23 + 6;
* Multiplication SELECT 23 * 6 AS multiplication_result;
% or MOD Modulus SELECT 23 % 6; or SELECT 23 MOD 6;
FLOOR Removes the decimal places from a number and rounds it down to the nearest whole number. SELECT FLOOR(23 / 6) AS floor_result;
ROUND Rounds a number with decimal places to the nearest whole number. SELECT ROUND(23 / 6) AS round_result;

Stored functions

Command Description
CREATE FUNCTION sf_name ([parameter(s)]) Mandatory. Tells MySQL server to create a function named sf_name with the optional parameters defined in the parentheses.
RETURNS data type Mandatory. Specifies the data type that the function returns.
DETERMINISTIC The function returns the same value whenever the same arguments are supplied to it.
STATEMENTS The procedural code that the function executes.

SQL Aggregate function commands

Command Description
SELECT COUNT(t1_id) FROM table1 WHERE t1_id = 2; COUNT function
SELECT MIN(t3) FROM table2; MIN function
SELECT MAX(t3) FROM table2; MAX function
SELECT SUM(t4) FROM table3; SUM function
SELECT AVG(t4) FROM table3; AVG function

SQL IS NULL & IS NOT NULL commands

A NULL is an absent value, so it must be tested with IS NULL rather than the equals operator.

Command Description
SELECT COUNT(t3) FROM table1; COUNT ignores NULL values in the column.
CREATE TABLE table2(
t1_number int NOT NULL,
t2_names varchar(255),
t3 varchar(6)
);
NOT NULL forbids empty values in a column.
column_name IS NULL
column_name IS NOT NULL
NULL keywords basic syntax
SELECT * FROM table1 WHERE t2_number IS NULL; Example of IS NULL
SELECT * FROM table1 WHERE t2_number IS NOT NULL; Example of IS NOT NULL

SQL AUTO_INCREMENT commands

Command Description
CREATE TABLE table1 (
t1_id int(11) AUTO_INCREMENT,
t2_name varchar(150) DEFAULT NULL,
t3 varchar(500) DEFAULT NULL,
PRIMARY KEY (t1_id)
);
Auto increment syntax

SQL – ALTER, DROP, RENAME, MODIFY

These commands change a table that already contains data.

Command Description
ALTER TABLE table_name ADD COLUMN column_name data_type; ALTER syntax
DROP TABLE sample_table; DROP TABLE syntax
RENAME TABLE current_table_name TO new_table_name; RENAME command syntax
ALTER TABLE table1 CHANGE COLUMN t1_names t1name char(250) NOT NULL; CHANGE keyword
ALTER TABLE table1 MODIFY t1name char(50) NOT NULL; MODIFY keyword
ALTER TABLE table1 ADD t4 date NULL AFTER t3; AFTER keyword

SQL LIMIT & OFFSET

Command Description
SELECT {fieldname(s) | *} FROM tableName(s) [WHERE condition] LIMIT N; LIMIT keyword syntax
SELECT * FROM table1 LIMIT 1, 2; Offset in the LIMIT query: skip 1 row, then return 2.

SQL SubQuery commands

Command Description
SELECT t1_name FROM table1 WHERE
category_id = (SELECT MIN(t1_id) FROM table2);
Sub queries

SQL JOINS commands

JOINS combine columns from two tables into one result set.

Command Description
SELECT * FROM table1 CROSS JOIN table2; Cross JOIN
SELECT table1.t1, table1.t2, table2.t1
FROM table1, table2
WHERE table2.id = table1.table2_id;
INNER JOIN
SELECT A.t1, B.t2, B.t3
FROM table2 AS A
LEFT JOIN table1 AS B
ON B.table2_id = A.id;
LEFT JOIN
SELECT A.t1, A.t2, B.t3
FROM table1 AS A
RIGHT JOIN table2 AS B
ON B.id = A.table2_id;
RIGHT JOIN
SELECT A.t1, B.t2, B.t3
FROM table2 AS A
LEFT JOIN table1 AS B
USING (table2_id);
“ON” and “USING” clauses

SQL UNION commands

UNION stacks the rows of two queries, whereas a JOIN widens them into more columns.

Command Description
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;
UNION syntax. Duplicate rows are removed.
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2;
UNION ALL keeps duplicate rows.

SQL in Views commands

Views store a query under a name so it can be reused like a table.

Command Description
CREATE VIEW view_name AS SELECT statement; Views syntax
DROP VIEW general_v_movie_rentals; Dropping views

SQL Index commands

An index is a lookup structure that makes searches on a column far faster.

Command Description
CREATE INDEX id_index ON table_name(column_name); Add index basic syntax
DROP INDEX index_id ON table_name; Drop index basic syntax

FAQs

No. Keywords such as SELECT and WHERE work in any case. Uppercase is a convention separating commands from identifiers. Table names, however, can be case sensitive on Linux.

DELETE removes selected rows and can be rolled back. TRUNCATE empties the whole table quickly and resets AUTO_INCREMENT. DROP removes the table definition itself.

FROM and JOIN run first, then WHERE, GROUP BY, HAVING, SELECT, ORDER BY and finally LIMIT. This is why a column alias defined in SELECT cannot be used inside WHERE.

Yes. Text-to-SQL assistants translate a question into a query using the table schema. Always read the generated WHERE and JOIN conditions, because a plausible query can return wrong rows.

Yes. Reviewing AI output, tuning slow queries and reading an execution plan all demand fluency in the commands. A reference sheet speeds recall; it does not replace understanding.

Summarize this post with: