I have an SQLite database that I need to do the following: Keep only last N records, sorted by date. How do you do that?
-
Need the columns, if any are autoincrement... – OMG Ponies Jun 29 '11 at 22:53
-
Duplicate: http://stackoverflow.com/q/1977341/132382 – pilcrow Jul 14 '11 at 03:02
5 Answers
To delete all but the latest 10 records.
delete
from test
where id not in (
select id
from test
order by date desc
limit 10
)

- 9,062
- 2
- 25
- 43
-
-
3
-
-
1
-
I tried this exact query (cut and paste) and got an error returned. I then tried putting "desc" after "order by date" (as suggested by @Drejc) and it worked. – cheshirekow May 24 '12 at 15:02
-
@KierenJohnstone I was using datetime("XYZ") to sort which deleting the latest result. Removing it give the result fine. I am saving the string of format yyyy-MM-dd HH:mm:ss – Shubham AgaRwal Jan 17 '19 at 17:36
According to the SQLite documentation:
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.
(...)
If the DELETE statement has an ORDER BY clause, then all rows that would be deleted in the absence of the LIMIT clause are sorted according to the ORDER BY. The first M rows, where M is the value found by evaluating the OFFSET clause expression, are skipped, and the following N, where N is the value of the LIMIT expression, are deleted. If there are less than N rows remaining after taking the OFFSET clause into account, or if the LIMIT clause evaluated to a negative value, then all remaining rows are deleted.
This would allow you to write:
DELETE FROM table WHERE expr ORDER BY date DESC LIMIT -1 OFFSET 10

- 1
- 1

- 1,318
- 10
- 8
-
~"If there are less than N rows remaining". What does -1 mean here? – IgorGanapolsky Dec 13 '13 at 16:00
-
@IgorGanapolsky Look at the last sentence of the quoted paragraph: _if the LIMIT clause evaluated to a negative value, then all remaining rows are deleted_. – Martín Valdés de León Dec 13 '13 at 20:39
-
So if there are less than 10 rows in the table altogether, then all of them will be deleted? – IgorGanapolsky Dec 14 '13 at 17:27
-
1@IgorGanapolsky If there are less than 10 rows in the table altogether, then none of them will be deleted. – Martín Valdés de León Dec 15 '13 at 14:57
-
@VladimirCh maybe that's because this question is for SQLite, not mariaDB. – Évelyne Lachance Sep 14 '18 at 04:19
to keep only the last 10 records, think inverted.
To delete the older 10 records:
DELETE FROM Table_name
WHERE date in (SELECT date FROM Table_name ORDER BY Date Desc Limit -1
OFFSET (select count(*)-10 from Table_name) );
Let me know how it worked for you!

- 11,166
- 11
- 56
- 86

- 21
- 1
Assuming you have an id
column which is a sequential number (AUTO INCREMENT
), you can use the following:
DELETE FROM table_name
WHERE id < (
SELECT MIN(id)
FROM (SELECT id
FROM table_name
ORDER BY id DESC
LIMIT num_of_records_to_keep))
The same query can be used when using a timestamp column (simply replace id
with your timestamp column)

- 8,070
- 5
- 54
- 88
I wonder if this is faster or slower than Jackob solution. sqlitebrowser says 0ms for both...
DELETE FROM test WHERE date < ( SELECT date FROM test ORDER BY date DESC LIMIT 1 OFFSET 9 )

- 171
- 1
- 1
- 15