Site icon Tech Dreams

How To Delete Top 100 Rows From SQL Server Tables

If you want to delete top 100/n records from an SQL Server table, it is very easy with the following query:

DELETE FROM MyTable
WHERE PK_Column
IN(
    SELECT TOP 100 PK_Column
    FROM MyTable
    ORDER BY creation
   )

Why Would You Require To Delete Top 100 Records?

I often delete a top n records of a table when number of rows in the are too huge. Lets say if I’ve 1000000000 records in a table, deleting 10000 rows at a time in a loop is faster than trying to delete all the 1000000000  at a time.

What ever may be reason, if you ever come across a requirement of deleting a bunch of rows at a time, this query will be helpful to you.

Exit mobile version