Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Delete Records within a specific Range [duplicate]

People also ask

How do you delete one record from duplicates in SQL?

DELETE Duplicate Records Using ROWCOUNT So to delete the duplicate record with SQL Server we can use the SET ROWCOUNT command to limit the number of rows affected by a query. By setting it to 1 we can just delete one of these rows in the table.

How do you delete duplicate records by group in SQL?

To delete the duplicate rows from the table in SQL Server, you follow these steps: Find duplicate rows using GROUP BY clause or ROW_NUMBER() function. Use DELETE statement to remove the duplicate rows.

How can I delete rows with duplicate values in one column in SQL?

Use DISTINCT operator in the SELECT clause to remove duplicate rows from the result set.

How delete multiple duplicate rows in SQL?

RANK function to SQL delete duplicate rows We can use the SQL RANK function to remove the duplicate rows as well. SQL RANK function gives unique row ID for each row irrespective of the duplicate row. In the following query, we use a RANK function with the PARTITION BY clause.


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


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.


DELETE FROM table_name 
WHERE id BETWEEN 79 AND 296;

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)