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.
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)
);
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:
Stay Updated on AIGet Weekly AI Skills, Trends, Actionable Advice.
Sign up for the newsletter
You have successfully subscribed. Please check your inbox.