MS Access Query – How to Count and Delete Rows Greater Than N

ms access

Can someone help me with a SQL (Access) query to delete all rows greater than n entries ordered by ID?

I.e. say I have 1200 rows of data with ID incremented . I need delete the first amount leaving only 200.

If I have only 199 rows of data, then I need to preserve them all.

Best Answer

I don't have access to a working copy of MS Access to test this but what you might be looking for is this:

delete * from table_name where ID not in(
select top 200 ID from table_name order by id desc
);