delete last 10 rows in sql server

Delete the top 10 rows from a table using SQL Server

The CTE most efficient ways to delete rows from table as per your need.

Example 1,

;WITH CTE AS
(
   SELECT TOP 10*
     FROM UserDetail
)
DELETE FROM CTE


If you want to delete specific subset of rows instead of arbitrary subset, you should explicitly specify order to sub-query:

Example 2,

DELETE FROM UserDetail
WHERE UserID IN
(
  SELECT TOP 10
      UserID
  FROM [UserDetail]
  WHERE UserID =10
  ORDER BY UserID DESC
)

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^