Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

SQL Command Categories

SQL commands are divided into different categories based on the type of operation they perform. Understanding these categories makes it easier to identify which SQL command should be used for a particular database operation.

In SQL Server, commands are commonly studied under five major categories: DDL, DML, DQL, DCL and TCL. Each category has a different responsibility when working with databases and data.

SQL Command Categories Overview

Category Full Form Main Purpose Common Commands
DDL Data Definition Language Defines and changes the structure of database objects. CREATE, ALTER, DROP, TRUNCATE
DML Data Manipulation Language Adds, modifies and removes data. INSERT, UPDATE, DELETE
DQL Data Query Language Retrieves data from tables. SELECT
DCL Data Control Language Controls permissions and access. GRANT, REVOKE
TCL Transaction Control Language Manages transactions. COMMIT, ROLLBACK

1. DDL – Data Definition Language

DDL stands for Data Definition Language. DDL commands are mainly used to define and modify the structure of database objects.

Database objects include databases, tables, views and other objects. DDL commands do not primarily deal with changing individual records; they work with the structure of objects.

Common DDL Commands
  • CREATE – Creates a database object.
  • ALTER – Changes the structure of an existing object.
  • DROP – Removes a database object.
  • TRUNCATE – Removes all rows from a table while keeping the table structure.
CREATE Example
SQL
CREATE TABLE Students
(
    StudentId INT,
    StudentName VARCHAR(100),
    Age INT
);
ALTER Example
SQL
ALTER TABLE Students
ADD Email VARCHAR(100);
DROP Example
SQL
DROP TABLE Students;

2. DML – Data Manipulation Language

DML stands for Data Manipulation Language. DML commands are used to work with the actual records stored inside database tables.

When we want to add a new record, change an existing record or remove a record, we use DML commands.

Common DML Commands
  • INSERT – Adds new records.
  • UPDATE – Modifies existing records.
  • DELETE – Removes records.
INSERT Example
SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age
)
VALUES
(
    101,
    'Rahul',
    22
);
UPDATE Example
SQL
UPDATE Students
SET Age = 23
WHERE StudentId = 101;
DELETE Example
SQL
DELETE FROM Students
WHERE StudentId = 101;
Important: Always use a proper WHERE condition with UPDATE and DELETE when you intend to affect specific records.

3. DQL – Data Query Language

DQL stands for Data Query Language. It is used to retrieve data from database tables.

The main command associated with data retrieval is SELECT.

SELECT Example
SQL
SELECT *
FROM Students;
Selecting Specific Columns
SQL
SELECT StudentName, Age
FROM Students;
Filtering Data
SQL
SELECT *
FROM Students
WHERE Age > 18;

4. DCL – Data Control Language

DCL stands for Data Control Language. DCL commands are used to control access and permissions on database objects.

Database administrators can use permissions to determine what operations a user or database principal is allowed to perform.

Common DCL Commands
  • GRANT – Gives permissions.
  • REVOKE – Removes previously granted permissions.
GRANT Example
SQL
GRANT SELECT
ON Students
TO UserName;
REVOKE Example
SQL
REVOKE SELECT
ON Students
FROM UserName;

5. TCL – Transaction Control Language

TCL stands for Transaction Control Language. It is used to manage transactions and control changes made during a transaction.

A transaction is a logical unit of work containing one or more database operations.

Common TCL Commands
  • COMMIT – Permanently saves the changes made in a transaction.
  • ROLLBACK – Undoes changes according to the transaction state.
  • SAVE TRANSACTION – Creates a savepoint within a transaction.
Transaction Example
SQL
BEGIN TRANSACTION;

UPDATE Students
SET Age = 25
WHERE StudentId = 101;

COMMIT;
ROLLBACK Example
SQL
BEGIN TRANSACTION;

UPDATE Students
SET Age = 30
WHERE StudentId = 101;

ROLLBACK;

SQL Command Categories Flow

The complete classification can be understood using the following diagram:

SQL

                         SQL Commands
                              |
          ---------------------------------------------
          |             |             |        |      |
         DDL           DML           DQL      DCL    TCL
          |             |             |        |      |
      Structure        Data        Retrieve  Access  Transaction
          |             |             |        |      |
    CREATE          INSERT        SELECT    GRANT   COMMIT
    ALTER           UPDATE                  REVOKE  ROLLBACK
    DROP            DELETE
    TRUNCATE
            

Real-World Understanding

Suppose we are developing a Student Management System. Different SQL categories will be used for different tasks.

Requirement Category Command
Create Students table DDL CREATE
Add a new student DML INSERT
Display students DQL SELECT
Change student's age DML UPDATE
Delete a student DML DELETE
Give SELECT permission DCL GRANT
Save transaction changes TCL COMMIT

Quick Comparison

Category Works With Main Purpose
DDL Database Structure Create / Change / Remove structure
DML Table Data Insert / Update / Delete records
DQL Table Data Retrieve records
DCL Permissions Control access
TCL Transactions Manage transaction changes
Important Points
  • DDL mainly deals with database object structure.
  • DML works with records stored in tables.
  • SELECT is used to retrieve data.
  • DCL is related to database permissions.
  • TCL is used to manage transactions.

Summary

In this section, we learned the major SQL command categories: DDL, DML, DQL, DCL and TCL. We understood the purpose of each category, studied their common commands and looked at practical examples of where each category is used in SQL Server.