SQL Server is a relational database management system (RDBMS) developed by Microsoft. It allows users to create, manage, and interact with databases using SQL (Structured Query Language). SQL Server uses a dialect of SQL called T-SQL (Transact-SQL), which includes additional features beyond standard SQL.
Tool used to manage and interact with SQL Server:
- SQL Server Management Studio (SSMS)
- Go to the official download page: https://www.microsoft.com/en-us/sql-server/sql-server-downloads
- Download SQL Server 2022 Express (free version for development and learning).
- Run the installer and choose Basic Installation.
- After installation, install SQL Server Management Studio (SSMS) from: https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms
- Open SSMS and connect to your local SQL Server instance (usually named
localhost
or(localdb)\MSSQLLocalDB
).
CREATE DATABASE SchoolDB;
Creates a new database named SchoolDB
.
USE SchoolDB;
Switches the context to SchoolDB
so that all subsequent commands target this database.
DROP DATABASE SchoolDB;
Deletes the entire database including its contents.
CREATE TABLE Students (
Id INT PRIMARY KEY IDENTITY(1,1),
Name NVARCHAR(100),
Age INT,
Email NVARCHAR(100)
);
Creates a table Students
with four columns. Id
auto-increments (SQL Server-specific: IDENTITY
).
SELECT * FROM INFORMATION_SCHEMA.TABLES; -- Standard SQL
SELECT * FROM sys.tables; -- SQL Server specific
Lists all tables in the current database.
DROP TABLE Students;
Deletes the Students
table and all its data.
TRUNCATE TABLE Students;
Removes all rows from the table quickly without logging each row deletion.
INSERT INTO Students (Name, Age, Email)
VALUES ('Alice', 20, 'alice@example.com');
Adds a new student to the Students
table.
SELECT * FROM Students;
Displays all records from the Students
table.
SELECT * FROM Students WHERE Age > 18;
Displays only students older than 18.
UPDATE Students
SET Age = 21
WHERE Name = 'Alice';
Changes Alice's age to 21.
DELETE FROM Students
WHERE Name = 'Alice';
Removes the student named Alice from the table.
Data Type | Example | Description |
---|---|---|
INT | 1, 100 | Whole numbers |
DECIMAL(10,2) | 99.99 | Decimal numbers |
NVARCHAR(50) | 'John' | Unicode character string |
DATETIME | '2025-01-01 10:00:00' | Date and time |
BIT | 0 or 1 | Boolean (true/false) |
Constraints enforce rules on data.
CREATE TABLE Courses (
Id INT PRIMARY KEY IDENTITY,
Title NVARCHAR(100) NOT NULL,
Fee DECIMAL(10,2) CHECK(Fee >= 0),
CreatedAt DATETIME DEFAULT GETDATE()
);
PRIMARY KEY
ensures each row is unique.NOT NULL
prevents null values.CHECK
validates a condition.DEFAULT
sets a default value.
SELECT * FROM Students WHERE Age >= 18;
Filters results based on a condition.
SELECT * FROM Students ORDER BY Age DESC;
Sorts the results by age in descending order.
SELECT TOP 5 * FROM Students;
Returns only the first 5 rows.
SELECT Age, COUNT(*) AS Count FROM Students GROUP BY Age;
Groups students by age and counts how many per group.
SUM(column)
- total sumAVG(column)
- average valueMIN(column)
- smallest valueMAX(column)
- largest value
SELECT s.Name, c.Title
FROM Students s
INNER JOIN Courses c ON s.CourseId = c.Id;
Shows students along with their course title if a match exists.
SELECT s.Name, c.Title
FROM Students s
LEFT JOIN Courses c ON s.CourseId = c.Id;
Shows all students and their courses (null if no course).
CREATE VIEW AdultStudents AS
SELECT * FROM Students WHERE Age >= 18;
SELECT * FROM AdultStudents;
A view is a virtual table based on a SELECT query.
CREATE PROCEDURE GetStudentsByAge @MinAge INT
AS
BEGIN
SELECT * FROM Students WHERE Age >= @MinAge;
END
EXEC GetStudentsByAge @MinAge = 18;
Stored procedures are reusable blocks of SQL code.
CREATE FUNCTION GetStudentCount()
RETURNS INT
AS
BEGIN
RETURN (SELECT COUNT(*) FROM Students);
END
SELECT dbo.GetStudentCount();
Functions return a single value and can be used inside queries.
BEGIN TRANSACTION;
UPDATE Students SET Age = Age + 1 WHERE Id = 1;
COMMIT;
-- or use ROLLBACK to undo
Used to make changes atomic and reversible.
Function | Description |
---|---|
GETDATE() | Current date and time (SQL Server) |
DATEADD() | Adds interval to a date |
DATEDIFF() | Calculates date difference |
LEN(column) | Gets length of string |
ISNULL(col, val) | Replaces NULL with another value |
SELECT name FROM sys.databases;
SELECT DB_NAME();
EXEC sp_columns Students;
SP_HELP Students;