This cheat sheet provides an extensive list of SQL commands and their descriptions. The commands are divided into different categories for easy reference.
Data Definition Language (DDL)
DDL commands are used to define the database schema and create, modify, and delete database objects.
Command | Description |
---|---|
CREATE DATABASE | Creates a new database |
CREATE TABLE | Creates a new table |
ALTER TABLE | Modifies an existing table |
DROP TABLE | Deletes a table |
CREATE INDEX | Creates an index on a table |
DROP INDEX | Deletes an index from a table |
Data Manipulation Language (DML)
DML commands are used to manipulate data in the database.
Command | Description |
---|---|
SELECT | Retrieves data from one or more tables |
INSERT INTO | Inserts new data into a table |
UPDATE | Modifies existing data in a table |
DELETE FROM | Deletes data from a table |
Data Control Language (DCL)
DCL commands are used to control access to the database.
Command | Description |
---|---|
GRANT | Grants privileges to a user or role |
REVOKE | Revokes privileges from a user or role |
Transaction Control Language (TCL)
TCL commands are used to manage transactions in the database.
Command | Description |
---|---|
COMMIT | Commits a transaction |
ROLLBACK | Rolls back a transaction |
SAVEPOINT | Creates a savepoint within a transaction |
ROLLBACK TO SAVEPOINT | Rolls back to a savepoint within a transaction |
Aggregate Functions
Aggregate functions are used to perform calculations on a set of values and return a single value.
Function | Description |
---|---|
AVG | Calculates the average value |
COUNT | Counts the number of values |
MAX | Returns the maximum value |
MIN | Returns the minimum value |
SUM | Calculates the sum of values |
String Functions
String functions are used to manipulate string values.
Function | Description |
---|---|
CONCAT | Concatenates two or more strings |
LENGTH | Returns the length of a string |
LOWER | Converts a string to lowercase |
UPPER | Converts a string to uppercase |
SUBSTRING | Returns a substring of a string |
Date Functions
Date functions are used to manipulate date and time values.
Function | Description |
---|---|
CURRENT_DATE | Returns the current date |
CURRENT_TIME | Returns the current time |
CURRENT_TIMESTAMP | Returns the current timestamp |
DATEADD | Adds a specified interval to a date |
DATEDIFF | Returns the difference between two dates |
Joins
Joins are used to combine data from two or more tables.
Join | Description |
---|---|
INNER JOIN | Returns only the rows that have matching values in both tables |
LEFT JOIN | Returns all the rows from the left table and the matching rows from the right table |
RIGHT JOIN | Returns all the rows from the right table and the matching rows from the left table |
FULL OUTER JOIN | Returns all the rows from both tables, with NULL values in the columns where there is no match |
Subqueries
Subqueries are used to retrieve data from one table based on the values in another table.
Subquery | Description |
---|---|
IN | Returns rows where the value matches any value in a subquery |
EXISTS | Returns rows where a subquery returns at least one row |
NOT EXISTS | Returns rows where a subquery returns no rows |