DELETE Statement in MS SQL Server
The DELETE statement in MS SQL Server deletes specified records from the table.
Syntax
MS SQL Server DELETE statement syntax is:
DELETE FROM table_name
WHERE condition;
Note: Always use the DELETE statement with WHERE clause. The WHERE clause specifies which record(s) need to be deleted. If you exclude the WHERE clause, all records in the table will be deleted.
DELETE Statement in MS SQL Server Examples
Let's look at some examples of the DELETE statement in MS SQL Server. The MS SQL Server DELETE Statement with examples will help in understanding the concept better.
First, we will create a demo SQL database and table, and use the DELETE statement on it.
Table Student
StudentName | RollNo | City |
---|---|---|
ABC | 1 | Jaipur |
DEF | 2 | Delhi |
JKL | 3 | Noida |
XYZ | 4 | Delhi |
Delete a single record using DELETE Statement
The following SQL statement deletes a row from “Student” table which has StudentName as 'ABC'.
DELETE FROM student
WHERE StudentName = 'ABC';
SELECT * FROM student;
Output
StudentName | RollNo | City |
---|---|---|
DEF | 2 | Delhi |
JKL | 3 | Noida |
XYZ | 4 | Delhi |
Delete all the records using DELETE statement
It is possible to delete all rows from a table without deleting the table. This means that the table structure, attributes, and indexes are going to be intact.
DELETE FROM student;
SELECT * FROM student;
Output:
StudentName | RollNo | City |
---|