SQL Queries

 

🔹 Introduction

This page contains a collection of commonly used T-SQL examples for everyday database development and administration tasks in Microsoft SQL Server.Each example is explained clearly to help both beginners and experts improve productivity and query writing skills.


📘 1️⃣ Database & Table Operations

➤ Create a Database

CREATE DATABASE SampleDB;

➤ Create a Table

CREATE TABLE Employees ( EmpID INT PRIMARY KEY, EmpName VARCHAR(100), Department VARCHAR(50), Salary DECIMAL(10,2) );

➤ Insert Data

INSERT INTO Employees (EmpID, EmpName, Department, Salary) VALUES (1, 'John Doe', 'IT', 65000.00), (2, 'Sara Lee', 'HR', 52000.00);

📊 2️⃣ Select & Filtering Queries

➤ Select All

SELECT * FROM Employees;

➤ Select with Filter

SELECT EmpName, Salary FROM Employees WHERE Department = 'IT';

➤ Sorting Results

SELECT * FROM Employees ORDER BY Salary DESC;

🔄 3️⃣ Update & Delete

➤ Update Data

UPDATE Employees SET Salary = Salary + 5000 WHERE EmpID = 1;

➤ Delete Data

DELETE FROM Employees WHERE EmpID = 2;

🔁 4️⃣ Joins

➤ Inner Join Example

SELECT e.EmpName, d.DepartmentName FROM Employees e INNER JOIN Departments d ON e.Department = d.DepartmentCode;

🧠 5️⃣ Stored Procedure Example

CREATE PROCEDURE GetEmployeeByID @EmpID INT AS BEGIN SELECT * FROM Employees WHERE EmpID = @EmpID; END;

🏷 6️⃣ Useful System Queries

➤ Check SQL Server Version

SELECT @@VERSION;

➤ List All Databases

SELECT name FROM sys.databases;

✔ Final Note

This page is updated regularly with new T-SQL examples covering:

Bookmark this page and check back often!

0 comments:

Post a Comment