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

INSERT, UPDATE & DELETE

INSERT, UPDATE and DELETE are important SQL commands used to add, modify and remove records from database tables.

These commands are part of Data Manipulation Language (DML). They allow applications and developers to work with the actual data stored inside SQL Server tables.

What is DML?

DML stands for Data Manipulation Language. It is used to manipulate the records stored inside database tables.

The three commonly used DML commands covered in this section are:

  • INSERT – Used to add new records.
  • UPDATE – Used to modify existing records.
  • DELETE – Used to remove records.

Sample Students Table

We will use the following Students table to understand INSERT, UPDATE and DELETE operations.

StudentId StudentName Age City
101 Rahul 22 Pune
102 Priya 23 Mumbai
103 Amit 21 Pune

1. INSERT Statement

The INSERT statement is used to add new records into a database table.

When a new student, employee, customer or product needs to be stored in a table, we can use the INSERT statement.

INSERT Syntax

SQL
INSERT INTO table_name
(
    column1,
    column2,
    column3
)
VALUES
(
    value1,
    value2,
    value3
);

INSERT Example

Suppose we want to add a new student to the Students table.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    City
)
VALUES
(
    104,
    'Neha',
    22,
    'Nashik'
);

Output

StudentId StudentName Age City
101 Rahul 22 Pune
102 Priya 23 Mumbai
103 Amit 21 Pune
104 Neha 22 Nashik

Insert Multiple Records

SQL Server also allows multiple records to be inserted using a single INSERT statement.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    City
)
VALUES
(105, 'Kiran', 24, 'Pune'),
(106, 'Sneha', 22, 'Mumbai'),
(107, 'Aakash', 23, 'Nashik');

2. UPDATE Statement

The UPDATE statement is used to modify existing records in a table.

For example, if a student's city or age changes, we can update the existing record instead of inserting a new one.

UPDATE Syntax

SQL
UPDATE table_name
SET column_name = value
WHERE condition;

UPDATE Example

Suppose Rahul's age changes from 22 to 23.

SQL
UPDATE Students
SET Age = 23
WHERE StudentId = 101;

Output

StudentId StudentName Age City
101 Rahul 23 Pune

Update Multiple Columns

Multiple columns can be modified using the same UPDATE statement.

SQL
UPDATE Students
SET Age = 24,
    City = 'Mumbai'
WHERE StudentId = 101;

This query changes both the Age and City of the student whose StudentId is 101.

UPDATE Without WHERE

If the WHERE clause is not specified, the UPDATE statement can modify the selected column for every row in the table.

SQL
UPDATE Students
SET City = 'Pune';
Important: Always check the WHERE condition before executing an UPDATE when only specific records should be changed.

3. DELETE Statement

The DELETE statement is used to remove records from a database table.

We can delete a specific record by using a WHERE condition.

DELETE Syntax

SQL
DELETE FROM table_name
WHERE condition;

DELETE Example

Suppose we want to remove the student whose StudentId is 104.

SQL
DELETE FROM Students
WHERE StudentId = 104;

Output

After executing the query, the record with StudentId 104 will no longer exist in the table.

DELETE Without WHERE

If DELETE is executed without a WHERE condition, all rows from the table are targeted for deletion.

SQL
DELETE FROM Students;
Warning: This statement deletes all rows from the Students table. Use it only when deleting all records is actually intended.

INSERT vs UPDATE vs DELETE

Command Purpose Operation Example
INSERT Adds new records. Creates rows. Add new student
UPDATE Modifies existing records. Changes rows. Change student age
DELETE Removes records. Deletes rows. Remove student

INSERT, UPDATE & DELETE Flow

These three commands perform different operations on table data.

SQL

                    Table
                      |
          ------------------------
          |          |           |
        INSERT     UPDATE      DELETE
          |          |           |
         Add       Modify      Remove
          |          |           |
          ------------------------
                      |
                 Table Data
            

Example 🌍🤓

Consider a Student Management System.

Situation SQL Command
New student joins the institute INSERT
Student changes city UPDATE
Student record needs to be removed DELETE
Important Points
  • INSERT is used to add new records.
  • UPDATE is used to modify existing records.
  • DELETE is used to remove records.
  • Use WHERE carefully with UPDATE.
  • Use WHERE carefully with DELETE.

Summary

In this section, we learned how INSERT is used to add records, UPDATE is used to modify records and DELETE is used to remove records. We also learned their syntax, practical examples, output and important precautions when using WHERE conditions.