SQLite Forum

Problem with Sqlite DELETE documentation
Login
The ORDER BY needs to be given when you want correct records to be deleted.

Example:

`CREATE TABLE test(i INTEGER PRIMARY KEY);`

`INSERT INTO test VALUES (1),(2),(3),(4),(5);`

To delete all records:

`DELETE FROM test;`

But this produces an error (which is OK):

`DELETE FROM test ORDER BY i;`

But suppose we want to delete two records?

The only way to know WHICH records are deleted is to specify a WHERE clause like:

`DELETE FROM TEST WHERE i IN(1,2);`

Or, to specify an 'ORDER BY' and a 'LIMIT'

To delete the first two records:

`DELETE FROM TEST ORDER BY i ASC LIMIT 2;`

To delete the last two records:

`DELETE FROM TEST ORDER BY i DESC LIMIT 2;`

But these last two lines produce this ERROR:
Error: near "ORDER": syntax error
(Windows 10, SQLite 3.32.3 )


But the reason for this error is explained in the docs:
If SQLite is compiled with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option, then the syntax of the DELETE statement is extended by the addition of optional ORDER BY and LIMIT clauses
(https://www.sqlite.org/lang_delete.html)