SQL Queries Cheat Sheet

A quick reference for common SQL queries.

CommandDescription
SELECT * FROM [table_name];Select all records from a table.
SELECT [column1], [column2] FROM [table_name];Select specific columns from a table.
SELECT * FROM [table_name] WHERE [condition];Select records based on a condition.
INSERT INTO [table_name] ([column1], [column2]) VALUES ([value1], [value2]);Insert a new record into a table.
UPDATE [table_name] SET [column1] = [value1] WHERE [condition];Update records in a table.
DELETE FROM [table_name] WHERE [condition];Delete records from a table.

CommandDescription
SELECT * FROM [table1] INNER JOIN [table2] ON [table1.column] = [table2.column];Select records that have matching values in both tables.
SELECT * FROM [table1] LEFT JOIN [table2] ON [table1.column] = [table2.column];Select all records from the left table, and the matched records from the right table.
SELECT * FROM [table1] RIGHT JOIN [table2] ON [table1.column] = [table2.column];Select all records from the right table, and the matched records from the left table.
SELECT * FROM [table1] FULL OUTER JOIN [table2] ON [table1.column] = [table2.column];Select all records when there is a match in either left or right table.

CommandDescription
SELECT COUNT([column]) FROM [table_name];Return the number of rows.
SELECT SUM([column]) FROM [table_name];Return the total sum of a numeric column.
SELECT AVG([column]) FROM [table_name];Return the average value of a numeric column.
SELECT MIN([column]) FROM [table_name];Return the smallest value of the selected column.
SELECT MAX([column]) FROM [table_name];Return the largest value of the selected column.