SQL Server Training Guide
1. Introduction to SQL Server
- What is SQL Server?
- SQL Server is a relational database management system (RDBMS) developed by Microsoft. It allows you to store, retrieve, and manipulate data using structured query language (SQL).
- Installation and Setup
- Download and install SQL Server (choose the appropriate edition).
- Set up authentication modes (Windows or SQL Server authentication).
2. Creating Databases and Tables
- Creating a Database
- Use the CREATE DATABASE statement to create a new database.
- Creating Tables
- Define tables with columns, data types, and constraints.
- Example:
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), Department VARCHAR(50) );
3. Basic SQL Queries
- SELECT Statement
- Retrieve data from a table:
SELECT FirstName, LastName FROM Employees;
- Retrieve data from a table:
- WHERE Clause
- Filter data based on conditions:
SELECT * FROM Employees WHERE Department = ‘IT’;
- Filter data based on conditions:
- ORDER BY Clause
- Sort data:
SELECT * FROM Employees ORDER BY LastName ASC;
- Sort data:
- GROUP BY Clause
- Aggregate data:
SELECT Department, COUNT(*) FROM Employees GROUP BY Department;
- Aggregate data:
- JOIN Clause
4. Data Manipulation
- INSERT INTO Statement
- Add new records:
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department) VALUES (101, ‘John’, ‘Doe’, ‘HR’);
- Add new records:
- UPDATE Statement
- Modify existing records:
UPDATE Employees SET Department = ‘Finance’ WHERE EmployeeID = 101;
- Modify existing records:
- DELETE Statement
- Remove records:
DELETE FROM Employees WHERE EmployeeID = 101;
- Remove records:
5. Advanced SQL Functions
- Aggregate Functions
- COUNT, SUM, AVG, MAX, MIN:
SELECT AVG(Salary) FROM Employees;
- COUNT, SUM, AVG, MAX, MIN:
- CASE Expression
- Conditional expressions:
SELECT FirstName, CASE WHEN Salary > 50000 THEN ‘High’ ELSE ‘Low’ END AS SalaryCategory FROM Employees;
- Conditional expressions:
- String Functions
- SUBSTRING, CONCAT:
SELECT CONCAT(FirstName, ‘ ‘, LastName) AS FullName FROM Employees;
- SUBSTRING, CONCAT:
- Date and Time Functions
- DATEPART, DATEDIFF:
SELECT DATEPART(YEAR, HireDate) AS HireYear FROM Employees;
- DATEPART, DATEDIFF:
6. Stored Procedures and Functions
- Stored Procedures
- Create reusable code blocks:
CREATE PROCEDURE GetEmployeeCount AS BEGIN SELECT COUNT(*) FROM Employees; END;
- Create reusable code blocks:
- User-Defined Functions
- Define custom functions:
CREATE FUNCTION CalculateBonus(@Salary DECIMAL) RETURNS DECIMAL AS BEGIN RETURN @Salary * 0.1; END;
- Define custom functions:
7. Indexes and Performance Optimization
- Indexing
- Improve query performance using indexes.
- Query Optimization
- Use execution plans to optimize queries.
8. Security and Permissions
- User Accounts
- Create logins and users.
- Permissions
- Grant or revoke permissions:
GRANT SELECT ON Employees TO HRUser;
- Grant or revoke permissions:
9. Backup and Restore
- Backup Databases
- Regularly back up your databases.
- Regularly back up your databases.
- Restore from Backups
- Restore sample databases using the provided backup files:
10. Useful URLs:
- [Microsoft Learn: AdventureWorks Sample Databases](https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view
Learn
https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver16
https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks
https://github.com/Microsoft/sql-server-samples/releases
https://learn.microsoft.com/en-us/sql/samples/sql-samples-where-are?view=sql-server-ver16
https://github.com/microsoft/sql-server-samples
https://pkg.go.dev/github.com/microsoft/sql-server-samples
https://github.com/Microsoft/sql-server-samples/blob/master/samples/databases/README.md
https://github.com/Microsoft/sql-server-samples

Leave a comment