SQL Commands Cheat Sheet

In SQL

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.

CommandDescription
CREATE DATABASECreates a new database
CREATE TABLECreates a new table
ALTER TABLEModifies an existing table
DROP TABLEDeletes a table
CREATE INDEXCreates an index on a table
DROP INDEXDeletes an index from a table

Data Manipulation Language (DML)

DML commands are used to manipulate data in the database.

CommandDescription
SELECTRetrieves data from one or more tables
INSERT INTOInserts new data into a table
UPDATEModifies existing data in a table
DELETE FROMDeletes data from a table

Data Control Language (DCL)

DCL commands are used to control access to the database.

CommandDescription
GRANTGrants privileges to a user or role
REVOKERevokes privileges from a user or role

Transaction Control Language (TCL)

TCL commands are used to manage transactions in the database.

CommandDescription
COMMITCommits a transaction
ROLLBACKRolls back a transaction
SAVEPOINTCreates a savepoint within a transaction
ROLLBACK TO SAVEPOINTRolls 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.

FunctionDescription
AVGCalculates the average value
COUNTCounts the number of values
MAXReturns the maximum value
MINReturns the minimum value
SUMCalculates the sum of values

String Functions

String functions are used to manipulate string values.

FunctionDescription
CONCATConcatenates two or more strings
LENGTHReturns the length of a string
LOWERConverts a string to lowercase
UPPERConverts a string to uppercase
SUBSTRINGReturns a substring of a string

Date Functions

Date functions are used to manipulate date and time values.

FunctionDescription
CURRENT_DATEReturns the current date
CURRENT_TIMEReturns the current time
CURRENT_TIMESTAMPReturns the current timestamp
DATEADDAdds a specified interval to a date
DATEDIFFReturns the difference between two dates

Joins

Joins are used to combine data from two or more tables.

JoinDescription
INNER JOINReturns only the rows that have matching values in both tables
LEFT JOINReturns all the rows from the left table and the matching rows from the right table
RIGHT JOINReturns all the rows from the right table and the matching rows from the left table
FULL OUTER JOINReturns 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.

SubqueryDescription
INReturns rows where the value matches any value in a subquery
EXISTSReturns rows where a subquery returns at least one row
NOT EXISTSReturns rows where a subquery returns no rows

References