Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:17:53+00:00 2026-05-29T06:17:53+00:00

I have a table with data that I don’t need to keep for very

  • 0

I have a table with data that I don’t need to keep for very long, so every night I want to remove all rows except the last 20.

To do that, I found the following query:

DELETE FROM Table WHERE ID NOT IN (
    SELECT id FROM (
        SELECT TOP 10 ID FROM Table
    ) AS x
)

MySQL doesn’t support the TOP function, so I rewrote it to use LIMIT instead:

DELETE FROM Table WHERE ID NOT IN (
    SELECT id FROM (
        SELECT ID FROM Table ORDER BY ID DESC LIMIT 10
    ) AS x
)

Unfortunately, MySQL doesn’t seem to support the LIMIT function within subqueries. So what do I do now?

How do I select all except the 10 rows with the highest ID?

I could probably just delete all the records that are older than a day or something, but it feels like I should be able to do it this way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:17:54+00:00Added an answer on May 29, 2026 at 6:17 am

    Another way:

    DELETE t
    FROM 
        TableX AS t
      CROSS JOIN
        ( SELECT Id
          FROM TableX
          ORDER BY Id DESC
          LIMIT 1 OFFSET 9
        ) AS tenth
    WHERE t.Id < tenth.id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long list of data that I want to display in table
I have a table of data that I need to dynamically add a column
I have table to test score data that I need to pivot and I
I want to have a database table that keeps data with revision history (like
In a nutshell: I have some database table data that I want to populate
I have a table with a lot of repeated data that I'd like to
I have an Excel table with some data that I would like to export
I have a sql table of payroll data that has wage rates and effective
I have a table of data which represents a series of events that persons
I have table that I insert data with following query (from c# code): INSERT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.