Home > Back-end >  SQL syntax error at or near "order" using Postgres
SQL syntax error at or near "order" using Postgres

Time:07-18

Here is my query.

I'm trying to delete the last N rows.

DELETE from Employee order by EmployeeID desc limit 3;

ERROR: syntax error at or near "order"
LINE 1: DELETE from Employee order by EmployeeID desc limit 3

CodePudding user response:

Try this:

DELETE
from Employee -- target table
where EmployeeID in (SELECT EmployeeID
                     from Employee
                     order by EmployeeID desc
                     limit 3); -- subquery in condition

You should follow the SQL delete statement syntax and specify the target table in the from <table> and then the condition to delete in the where <statement>.

Use subqueries to achieve the correct condition.

CodePudding user response:

try

DELETE FROM Employee
WHERE EmployeeID IN (
    SELECT EmployeeID
    FROM Employee
    ORDER BY EmployeeID
    LIMIT 3
)
  • Related