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

 DELETE  TOP (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

 DELET E 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) PER CENT 
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

3.7 ★ | 14 Vote

May be interested

  • CASE function in SQL Server (part 1)CASE function in SQL Server (part 1)
    in sql server, the case function verifies the value based on the list of given conditions, then returns one or more results. in this article we will illustrate a number of different uses of this function in different situations.
  • The sfc command in WindowsThe sfc command in Windows
    (applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
  • The cacls command in WindowsThe cacls command in Windows
    the cacls command displays or modifies an arbitrary access control list (dacl) on the specified file. the command applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012.
  • CASE statement in SQL ServerCASE statement in SQL Server
    this article will show you in detail how to use the case statement handling function in sql server with specific syntax and examples to better visualize and capture functions.
  • ALTER LOGIN command in SQL ServerALTER LOGIN command in SQL Server
    the alter login command is used to modify the identity used to connect to sql server.
  • SELECT TOP command in SQL ServerSELECT TOP command in SQL Server
    in sql server, the select top command is used to retrieve records from one or more tables in sql server and limit the number of return records based on a fixed value or percentage.
  • SELECT INTO command in SQL ServerSELECT INTO command in SQL Server
    the article guides using select into command in sql server with syntax and examples.
  • Foreign Key with Cascade Delete in SQL ServerForeign Key with Cascade Delete in SQL Server
    this article explains how to use foreign key with cascade delete in sql server with syntax and examples.
  • Prncnfg command in WindowsPrncnfg command in Windows
    (applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012
  • The route_ws2008 command in WindowsThe route_ws2008 command in Windows
    (applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)