This repository contains a comprehensive collection of T-SQL scripts for Microsoft SQL Server, designed as a practical learning resource and reference guide. From basic database creation to advanced stored procedures and functions, these scripts cover essential SQL Server concepts through hands-on, executable examples.
Perfect for database administrators, developers, and students, this collection provides ready-to-use code snippets that demonstrate real-world SQL Server operations and best practices.
| File | Topic | Description |
|---|---|---|
| 01-Create.sql | Database Creation | Creating new databases and understanding options |
| 03-Create_table.sql | Table Creation | Creating tables with various data types and constraints |
| 02-DELETE_Drop.sql | Deletion Operations | Properly dropping databases, tables, and data |
| File | Topic | Description |
|---|---|---|
| 04-Defult_constrain.sql | Default Constraints | Setting default values for columns |
| 05-Defult_constrain.sql | Advanced Defaults | Complex default constraint scenarios |
| 06-Cascadingreferentialintegrityconstraint.sql | Referential Integrity | Implementing cascading updates/deletes |
| 07-Check_constraint.sql | Check Constraints | Data validation using check constraints |
| File | Topic | Description |
|---|---|---|
| 08-Retrive_identity_column_Valuessql.sql | Identity Values | Retrieving identity column values after insertion |
| 09-Retrivint_Identity.sql | Identity Management | Working with IDENTITY columns and SCOPE_IDENTITY() |
| File | Topic | Description |
|---|---|---|
| 10-All_About_Select.sql | SELECT Statements | Comprehensive SELECT query examples |
| 11-Group_by.sql | GROUP BY & Aggregation | Data aggregation and grouping operations |
| 15-ReplacingNullValues.sql | NULL Handling | Techniques for managing NULL values |
| 16-CoalesceFunction.sql | COALESCE Function | Using COALESCE for NULL value replacement |
| File | Topic | Description |
|---|---|---|
| 12-Joins.sql | Basic Joins | INNER, LEFT, RIGHT, FULL OUTER joins |
| 13-AdvancedORintelligentJoin.sql | Advanced Joins | Complex join scenarios and optimizations |
| 14-Self_join.sql | Self Joins | Joining a table to itself |
| 17-Union_UnionAll.sql | Set Operations | UNION, UNION ALL operations |
| File | Topic | Description |
|---|---|---|
| 18-StoreProcedures.sql | Basic Stored Procedures | Creating and executing stored procedures |
| 19-Store_procedureWithOutputParameter.sql | Output Parameters | Procedures with output parameters |
| 20-StoreProcedureOutputParameterOrReturnValues.sql | Return Values | Procedures returning values |
| 21-AdvantagesOfStoreProcedure.sql | Procedure Benefits | Advantages and use cases for stored procedures |
| 22-BuiltInFunction.sql | Built-in Functions | SQL Server's built-in function library |
| 23-Function.sql | User-defined Functions | Creating custom functions |
| File | Topic | Description |
|---|---|---|
| 24-Math-Numeic.sql | Mathematical Functions | Math and numeric operations |
| 25-Date_Function.sql | Date Functions | Date and time manipulations |
| 26-SQL_Advanced_FUnction.sql | Advanced Functions | Window functions, ranking, and analytics |
- Microsoft SQL Server (2016 or later recommended)
- SQL Server Management Studio (SSMS) or Azure Data Studio
- Basic understanding of database concepts
- (Optional) AdventureWorks sample database for practice
-- Open any .sql file in SSMS
-- Execute the entire script or selected portions
-- Modify parameters as needed for your environment# Execute a script using sqlcmd
sqlcmd -S your_server -d your_database -i 01-Create.sql-- 1. Create a practice database
CREATE DATABASE SQLPractice;
-- 2. Switch to the new database
USE SQLPractice;
-- 3. Execute learning scripts in sequence
-- Start with 01-Create.sql, then proceed numerically- Days 1-2: Database and table creation (01-03)
- Days 3-4: Constraints and data integrity (04-07)
- Days 5-7: Basic queries and SELECT statements (10-11)
- Days 8-10: Joins and relationships (12-14)
- Days 11-12: Advanced query techniques (15-17)
- Days 13-14: Identity and special values (08-09)
- Days 15-17: Stored procedures (18-21)
- Days 18-20: Functions (22-23, 26)
- Days 21-22: Specialized functions (24-25)
-- From 03-Create_table.sql and 07-Check_constraint.sql
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY IDENTITY(1,1),
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
Age INT CHECK (Age >= 18),
HireDate DATE DEFAULT GETDATE(),
DepartmentID INT FOREIGN KEY REFERENCES Departments(DepartmentID)
);-- From 13-AdvancedORintelligentJoin.sql
SELECT
e.FirstName,
e.LastName,
d.DepartmentName,
m.FirstName AS ManagerFirstName,
m.LastName AS ManagerLastName
FROM Employees e
LEFT JOIN Departments d ON e.DepartmentID = d.DepartmentID
LEFT JOIN Employees m ON e.ManagerID = m.EmployeeID
WHERE e.HireDate > '2020-01-01';-- From 19-Store_procedureWithOutputParameter.sql
CREATE PROCEDURE GetEmployeeCount
@DepartmentID INT,
@EmployeeCount INT OUTPUT
AS
BEGIN
SELECT @EmployeeCount = COUNT(*)
FROM Employees
WHERE DepartmentID = @DepartmentID;
END;| Metric | Value | Details |
|---|---|---|
| Total Scripts | 26 | Comprehensive coverage |
| Language | 100% T-SQL | SQL Server Transact-SQL |
| Total Commits | 27 | Active development |
| Development Period | March 2022 | Intensive learning phase |
| Lines of Code | 1,000+ (estimated) | Substantial code base |
| License | MIT | Open source |
- Clear, descriptive file names
- Logical progression from simple to complex
- Consistent formatting and commenting
- Separation of concerns (DDL, DML, procedures)
- Efficient query writing
- Proper indexing strategies
- Set-based operations over cursors
- Parameterized queries for security
- SQL injection prevention techniques
- Principle of least privilege in permissions
- Secure coding patterns
- Error handling implementation
- Add More Examples: Additional use cases for each topic
- Improve Documentation: Better comments and explanations
- Add Performance Tips: Optimization techniques
- Include New Features: SQL Server 2019/2022 features
- Fix Issues: Bug reports and corrections
- Fork the repository
- Create a feature branch (
git checkout -b feature/enhancement) - Commit changes (
git commit -m 'Add: description of improvement') - Push to branch (
git push origin feature/enhancement) - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for full details.
- β Commercial use
- β Modification
- β Distribution
- β Private use
- Include original copyright and license notice
- No liability
- No warranty
- "T-SQL Fundamentals" by Itzik Ben-Gan
- "SQL Server Internals" by Kalen Delaney
- "Pro SQL Server Internals" by Dmitri Korotkevitch
This collection builds upon the extensive knowledge shared by the SQL Server community:
- Microsoft SQL Server Team for creating a robust database platform
- SQL Server MVPs and Experts for sharing knowledge and best practices
- Database Community for continuous learning and improvement
- Educators and Authors who make complex concepts accessible
ποΈ "Data is a precious thing and will last longer than the systems themselves." - Tim Berners-Lee
Master SQL Server with these practical scripts! π
Maintained by Feroz455 | Last Updated: December 2024