Catalog / Microsoft SQL Server Cheatsheet
Microsoft SQL Server Cheatsheet
A comprehensive cheat sheet for Microsoft SQL Server, covering essential commands, syntax, and functions for database management and querying.
Basic SQL Commands
Data Definition Language (DDL)
CREATE DATABASE |
Creates a new database.
|
ALTER DATABASE |
Modifies an existing database.
|
DROP DATABASE |
Deletes a database.
|
CREATE TABLE |
Creates a new table.
|
ALTER TABLE |
Modifies an existing table.
|
DROP TABLE |
Deletes a table.
|
Data Manipulation Language (DML)
SELECT |
Retrieves data from a database.
|
INSERT |
Inserts new data into a table.
|
UPDATE |
Updates existing data in a table.
|
DELETE |
Deletes data from a table.
|
MERGE |
Performs insert, update, or delete operations based on conditions.
|
Querying Data
Filtering and Sorting
WHERE |
Filters rows based on a condition.
|
AND / OR |
Combines multiple conditions.
|
ORDER BY |
Sorts the result set.
|
TOP |
Returns the top N rows.
|
BETWEEN |
Filters rows within a range.
|
IN |
Filters rows based on a set of values.
|
Joins
INNER JOIN |
Returns rows with matching values in both tables.
|
LEFT JOIN |
Returns all rows from the left table and matching rows from the right table.
|
RIGHT JOIN |
Returns all rows from the right table and matching rows from the left table.
|
FULL OUTER JOIN |
Returns all rows when there is a match in either the left or right table.
|
CROSS JOIN |
Returns the Cartesian product of the tables.
|
Advanced SQL Features
Aggregate Functions
COUNT |
Counts the number of rows.
|
SUM |
Calculates the sum of values.
|
AVG |
Calculates the average of values.
|
MIN |
Finds the minimum value.
|
MAX |
Finds the maximum value.
|
Grouping and Having
GROUP BY |
Groups rows with the same values.
|
HAVING |
Filters groups based on a condition.
|
ROLLUP |
Generates multiple grouping sets, including subtotals and grand totals.
|
CUBE |
Generates all possible grouping sets for the specified columns.
|
Subqueries
Subquery in WHERE clause |
Using a subquery to filter results.
|
Subquery in SELECT clause |
Using a subquery to return a value.
|
Correlated Subquery |
A subquery that references a column from the outer query.
|
Transactions and Stored Procedures
Transactions
BEGIN TRANSACTION |
Starts a new transaction.
|
COMMIT TRANSACTION |
Saves all changes made during the transaction.
|
ROLLBACK TRANSACTION |
Reverts all changes made during the transaction.
|
SAVE TRANSACTION |
Sets a savepoint within a transaction.
|
Stored Procedures
CREATE PROCEDURE |
Creates a new stored procedure.
|
EXECUTE PROCEDURE |
Executes a stored procedure.
|
ALTER PROCEDURE |
Modifies an existing stored procedure.
|
DROP PROCEDURE |
Deletes a stored procedure.
|