DELETE TOP command in SQL Server
The DELETE TOP command in SQL Server is used to delete records from a table in SQL and limits the number of records based on an existing value or percentage.
DELETE TOP command syntax
DELETETOP (giatri_dau) [PERCENT]
FROM bang
[WHERE dieu
_kien];
Variable name or variable value
state
Table wants to delete records from there.
WHERE dieu_kien
Option. The condition that the record must meet to be deleted.
TOP (giatri_dau)
This command will delete the first row in the result set based on giatri_dau. For example, TOP (10) will delete the top 10 that meet the criteria.
PERCENT
Option. If specified, the first rows are based on the percentage of giatri_dau of the result set. For example, TOP (10) PERCENT will insert 10% of the first value in the result set.
Note
There is no need to list all fields in the DELETE statement because it will delete all rows in the table.
For example - use the keyword TOP
DELETE TOP(10)
FROM nhanvien
WHERE ho
= 'Anderson';
In this example, the DELETE TOP command will delete the first 10 records in the table with the employee family, Anderson. If there are other records that also have the family name of Anderson, they will not be deleted.
For example - use the keyword TOP PERCENT
DELETE TOP (25) PERCENT
FROM nhanvien
WHERE ten = 'Sarah';
In this example, 25% of the DELETE TOP response's delete records will be deleted. The remaining 75% will not be deleted.
Previous article: SELECT INTO command in SQL Server
The following article: UNION operator in SQL Server
You should read it
- VIEW in SQL Server
- Foreign Key with Cascade Delete in SQL Server
- Delete the foreign key in SQL Server
- The TRUNCATE TABLE command in SQL Server
- The difference between Truncate and Delete in Microsoft SQL Server
- How to delete the database in MS SQL Server
- DROP USER command in SQL Server
- FUNCTION (Function) in SQL Server
May be interested
- UNION operator in SQL Serverthis tutorial explains how to use the union operator in sql server with specific syntax and examples.
- UNION ALL operator in SQL Serverthe union all operator is used to combine the result set from 2 or more select statements in sql server.
- How to decentralize users in MS SQL Serveradministrators can create, delete or deny permissions in ms sql server. the permissions here refer to database access (csdl). you can create, delete or deny permissions in ms sql server.
- OR conditions in SQL Serverthe or condition in sql server (transact-sql) is used to check multiple conditions.
- Combine AND and OR conditions in SQL Serverthe article explains how to use and conditions and or conditions in sql server (transact-sql).
- DISTINCT clause in SQL Serverthe distinct clause is used to remove duplicates in the result set.