SQL Delete Records within a specific Range

SqlSql DeleteRecords

Sql Problem Overview


This is probably a very simple question for somebody with experience, but I just wanted to know the safest way to delete a couple of hundred records in an SQL table that fall between a specific range.

For example I need to delete rows with an ID between 79 & 296:

My worry is if I say delete everything with an ID (>79 AND < 296) then it may literally wipe the whole table.

Sql Solutions


Solution 1 - Sql

If you use Sql Server

delete from Table where id between 79 and 296

Note : the between statement is inclusive, so rows 79 and 296 will also be deleted

After your edit : you now clarified that you want :

ID (>79 AND < 296)

So use this :

delete from Table where id > 79 and id < 296

Solution 2 - Sql

You gave a condition ID (>79 and < 296) then the answer is:

delete from tab
where id > 79 and id < 296

this is the same as:

delete from tab
where id between 80 and 295

if id is an integer.

All answered:

delete from tab
where id between 79 and 296

this is the same as:

delete from tab
where id => 79 and id <= 296

Mind the difference.

Solution 3 - Sql

DELETE FROM table_name 
WHERE id BETWEEN 79 AND 296;

Solution 4 - Sql

you can also just change your delete to a select *

and test your selection

the records selected will be the same as the ones deleted

you can also wrap your statement in a begin / rollback if you are not sure - test the statement then if all is good remove rollback

for example

SELECT * FROM table WHERE id BETWEEN 79 AND 296

will show all the records matching the where if they are the wants you 'really' want to delete then use

DELETE FROM table WHERE id BETWEEN 79 AND 296

You can also create a trigger / which catches deletes and puts them into a history table

so if you delete something by mistake you can always get it back

(keep your history table records no older than say 6 months or whatever business rules say)

Solution 5 - Sql

If you write it as the following in SQL server then there would be no danger of wiping the database table unless all of the values in that table happen to actually be between those values:

DELETE FROM [dbo].[TableName] WHERE [TableName].[IdField] BETWEEN 79 AND 296 

Solution 6 - Sql

> My worry is if I say delete evertything with an ID (>79 AND < 296) > then it may literally wipe the whole table...

That wont happen because you will have a where clause. What happens is that, if you have a statement like delete * from Table1 where id between 70 and 1296 , the first thing that sql query processor will do is to scan the table and look for those records in that range and then apply a delete.

Solution 7 - Sql

You can use this way because id can not be sequential in all cases.

SELECT * 
FROM  `ht_news` 
LIMIT 0 , 30

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser964769View Question on Stackoverflow
Solution 1 - SqlRoyi NamirView Answer on Stackoverflow
Solution 2 - SqlMichał PowagaView Answer on Stackoverflow
Solution 3 - SqlyoshView Answer on Stackoverflow
Solution 4 - SqlTheTigerView Answer on Stackoverflow
Solution 5 - SqlCSharpenedView Answer on Stackoverflow
Solution 6 - SqllloydomView Answer on Stackoverflow
Solution 7 - SqlAliView Answer on Stackoverflow