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

DELETE Operation

The DELETE statement is used to remove existing records from a SQL Server table.

When a record is no longer required, the DELETE statement can be used to remove it from the table. We can delete a single record or multiple records based on a condition.

What is DELETE?

The DELETE statement removes one or more existing rows from a table.

The WHERE clause is normally used to specify which records should be deleted.

DELETE Syntax

SQL
DELETE FROM table_name
WHERE condition;
Important: Always check the WHERE condition before executing a DELETE statement. Without WHERE, all rows in the table can be deleted.

Example Table

We will use the following Students table for the examples.

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

Delete a Single Record

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

SQL
DELETE FROM Students
WHERE StudentId = 104;

Only the record with StudentId 104 will be removed.

Output

After executing the DELETE statement, the record with StudentId 104 is removed from the table.

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

DELETE Using a Condition

We can delete multiple records when they satisfy a particular condition.

SQL
DELETE FROM Students
WHERE City = 'Mumbai';

This statement deletes all students whose City is Mumbai.

DELETE Using a Numeric Condition

Numeric columns can also be used in the WHERE condition.

SQL
DELETE FROM Students
WHERE Age < 22;

This removes all students whose age is less than 22.

DELETE Without WHERE

If the WHERE clause is omitted, the DELETE statement removes all rows from the table.

SQL
DELETE FROM Students;
Warning

The above statement deletes all records from the Students table. The table structure itself remains.

DELETE vs DROP

DELETE DROP
Removes rows from a table. Removes the database object itself.
Table structure remains. Table structure is removed.
Can use WHERE to remove selected rows. Removes the complete table.

Practical Example

Suppose a student permanently leaves an institute and their record needs to be removed from the Students table.

SQL
DELETE FROM Students
WHERE StudentId = 103;

The record belonging to StudentId 103 will be removed.

Important Points

  • DELETE is used to remove existing records.
  • WHERE is used to identify the records to delete.
  • Multiple records can be deleted using a condition.
  • Without WHERE, all rows can be deleted.
  • DELETE removes rows but does not remove the table structure.
  • Always verify the WHERE condition before executing DELETE.

Summary

In this section, we learned how the DELETE statement removes records from a SQL Server table. We covered deleting a single record, deleting multiple records using conditions, and the importance of using the WHERE clause.