Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete certain amount of last rows in the table in PostgreSQL?

Tags:

postgresql

Without referencing the SERIAL id.

Something like:

delete from users LAST 3

which would delete last 3 rows from the table.

like image 716
Eduard Avatar asked Mar 25 '18 14:03

Eduard


People also ask

How do I delete a specific row in PostgreSQL?

The PostgreSQL DELETE statement allows you to delete one or more rows from a table. First, specify the name of the table from which you want to delete data after the DELETE FROM keywords. Second, use a condition in the WHERE clause to specify which rows from the table to delete. The WHERE clause is optional.

How many rows can be deleted from a PostgreSQL primary key?

There could be thousands, potentially millions of rows to delete. I have a file of primary keys, one per line. The two options I was thinking of were along the lines of the below, but I don't know/understand enough of the internals of PostgreSQL to make an informed decision which would be best.

How do I delete all rows in a table in SQL?

In DELETE query, you can also use clauses like WHERE, LIKE, IN, NOT IN, etc., to select the rows for which the DELETE operation will be performed. The use of WHERE clause is optional. If you do not provide any selection criteria for the rows, then all the rows of the table would be deleted.

Which statement is used to delete only select rows from a table?

Summary: 1 The DELETE statement is used for deleting one or more records from a table. 2 To delete only select rows from a table, you can combine the DELETE statement with the WHERE clause. 3 If the DELETE clause is used without the WHERE clause, it deletes all records from the table. More items...


1 Answers

This will work :

  DELETE
  FROM users 
  WHERE id in (
      SELECT id 
      FROM users 
      ORDER BY id desc
      LIMIT 3
     )
like image 105
Bilal Dekar Avatar answered Nov 15 '22 03:11

Bilal Dekar