SQL Cheatsheet
A quick reference for common SQL commands and keywords.
Aggregate Functions
Command / Keyword | Description |
---|---|
COUNT() |
Returns the number of rows. |
SUM() |
Returns the total sum of a numeric column. |
AVG() |
Returns the average value of a numeric column. |
MIN() |
Returns the smallest value of the selected column. |
MAX() |
Returns the largest value of the selected column. |
Data Definition Language (DDL)
Command / Keyword | Description |
---|---|
CREATE TABLE [table_name] (...) |
Creates a new table in the database. |
ALTER TABLE [table_name] ADD [column] [datatype] |
Adds a column to a table. |
DROP TABLE [table_name] |
Deletes a table. |
CREATE INDEX [index_name] ON [table_name] (column1, ...) |
Creates an index on a table. |
Data Manipulation Language (DML)
Command / Keyword | Description |
---|---|
INSERT INTO [table] (col1, col2) VALUES (val1, val2) |
Adds a new row to a table. |
UPDATE [table] SET [col1]=[val1] WHERE [condition] |
Modifies existing records in a table. |
DELETE FROM [table] WHERE [condition] |
Deletes existing records from a table. |
Data Query Language (DQL)
Command / Keyword | Description |
---|---|
SELECT [column1], [column2] |
Selects specific columns from a table. |
SELECT * FROM [table_name] |
Selects all columns from a table. |
WHERE [condition] |
Filters records based on a condition. |
ORDER BY [column] ASC|DESC |
Sorts the result set. |
GROUP BY [column] |
Groups rows that have the same values into summary rows. |
HAVING [condition] |
Filters groups based on a condition, used with GROUP BY. |
Joins
Command / Keyword | Description |
---|---|
INNER JOIN |
Returns records that have matching values in both tables. |
LEFT JOIN |
Returns all records from the left table, and the matched records from the right table. |
RIGHT JOIN |
Returns all records from the right table, and the matched records from the left table. |
FULL OUTER JOIN |
Returns all records when there is a match in either left or right table. |