Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete by row number in SQL

I need to delete from row number 475 to 948 due to them being duplicates of rows 1-474. It would be something close to this, I presume, or is there more to it?

DELETE FROM dbo.industry WHERE row_number between 475 and 948
like image 346
Tim Wilcox Avatar asked Oct 06 '17 18:10

Tim Wilcox


People also ask

How do I delete a specific number of rows in SQL?

If you wanted to delete a number of rows within a range, you can use the AND operator with the BETWEEN operator. DELETE FROM table_name WHERE column_name BETWEEN value 1 AND value 2; Another way to delete multiple rows is to use the IN operator.

How do I delete Rownum?

SQL> SQL> insert /*+ append */ into T 2 select mod(rownum,10), rownum 3 from dual 4 connect by level <= 100000; 100000 rows created. SQL> SQL> commit; Commit complete. SQL> SQL> delete from T 2 where rowid not in 3 ( 4 select rid 5 from 6 ( 7 select t.

What is ROW_NUMBER () function in SQL?

ROW_NUMBER function is a SQL ranking function that assigns a sequential rank number to each new record in a partition. When the SQL Server ROW NUMBER function detects two identical values in the same partition, it assigns different rank numbers to both.

How do I delete a specific number of rows in MySQL?

To delete rows in a MySQL table, use the DELETE FROM statement: DELETE FROM products WHERE product_id=1; The WHERE clause is optional, but you'll usually want it, unless you really want to delete every row from the table.


1 Answers

May be it is too late, but I am usually doing this

; with cte(rownum)as(
    select row_number () over(partition by [Col1], [Col2] order by Col3) from [table]
)
delete from cte where rownum > 1
like image 142
fly_ua Avatar answered Sep 18 '22 14:09

fly_ua