MSDN Documentation – SQL Guides

Welcome to SQL Guides

This section provides comprehensive guides for writing efficient and effective SQL queries. Explore the topics below to deepen your understanding of relational database concepts, query formulation, and best practices.

Table of Contents

SELECT Statements

Retrieve data from one or more tables using the SELECT clause.

SELECT column1, column2
FROM dbo.TableName
WHERE condition
ORDER BY column1 DESC;

Learn more in the SELECT guide.

INSERT Statements

Add new rows to a table with INSERT INTO.

INSERT INTO dbo.TableName (column1, column2)
VALUES ('Value1', 123);

Details are covered in the INSERT guide.

UPDATE Statements

Modify existing rows using UPDATE.

UPDATE dbo.TableName
SET column1 = 'NewValue'
WHERE column2 > 100;

Read the full guide here.

DELETE Statements

Remove rows safely with DELETE and a proper WHERE clause.

DELETE FROM dbo.TableName
WHERE column3 IS NULL;

See more in the DELETE guide.

Joins

Combine rows from two or more tables based on related columns.

SELECT a.id, a.name, b.order_date
FROM dbo.Customers AS a
INNER JOIN dbo.Orders AS b ON a.id = b.customer_id;

Explore join types in the Joins guide.

Built‑in Functions

Leverage scalar, aggregate, and window functions for advanced data operations.

SELECT AVG(salary) OVER (PARTITION BY department) AS avg_dept_salary
FROM dbo.Employees;

Full list in the Functions guide.

Indexes

Improve query performance with appropriate indexing strategies.

CREATE NONCLUSTERED INDEX IX_Employees_LastName
ON dbo.Employees (last_name);

Read best practices here.

Transactions

Ensure data integrity using BEGIN TRANSACTION, COMMIT, and ROLLBACK.

BEGIN TRANSACTION;
UPDATE dbo.Accounts SET balance = balance - 100 WHERE id = 1;
UPDATE dbo.Accounts SET balance = balance + 100 WHERE id = 2;
IF @@ERROR <> 0
    ROLLBACK;
ELSE
    COMMIT;

Details in the Transactions guide.

Performance Tuning

Use execution plans, statistics, and query refactoring to optimize performance.

SET STATISTICS IO ON;
SELECT * FROM dbo.LargeTable WHERE indexed_column = 5;

Full tuning guide available here.