3

Simple DELETE with safety LIMIT throws a syntax error:

DELETE FROM system."products" LIMIT 1

ERROR: Syntax ERROR at "LIMIT"

SQL state: 42601

How do I limit the amount of rows to delete?

(PostgresSQL version 9.6)

3
  • Of course, LIMIT in any query makes much more sense when you have ORDER BY Commented Feb 19, 2020 at 15:25
  • @a_horse_with_no_name I thought my query is simple ANSI SQL working in all ANSI compatible systems Commented Feb 19, 2020 at 15:27
  • 1
    There is no LIMIT in ANSI SQL (not for DELETE, nor for any other SQL command) Commented Feb 19, 2020 at 15:28

1 Answer 1

11

You can use LIMIT only in the SELECT statement.

Try this:

DELETE FROM system."products" WHERE id IN (SELECT id FROM system."products" LIMIT 1)

Otherwise you can have something like this

DELETE FROM system."products" WHERE id bewteen ? and ?

Obviously this can work only if there is in your table an unique column (named id in my examples)!

Sign up to request clarification or add additional context in comments.

11 Comments

IN() is evil, is there no other way?
@DanielW.: why do you think IN () is evil - especially if it only contains a single value
@DanielW.: then post the actual query. If you show us a (over) simplified problem, how do you expect to get an answer for the real problem
@DanielW. Has the table a primary key?
this is not a solution if there is no unique column in the table
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.