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 129639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:49:15+00:00 2026-05-11T05:49:15+00:00

I am currently trying to optimize a few queries and scripts, and I wonder

  • 0

I am currently trying to optimize a few queries and scripts, and I wonder if there is a way to cut off a certain percentage of a MySQL Result Set?

for example, I have a Query like this:

SELECT TrackingID, OpenTime, FirstPoint FROM PointsTable 

I need the ‘middle’ 74%, ordered by the difference between FirstPointGMT and OpenTimeGMT, which means: I need to cut off the top and bottom 13%.

At the moment, a Script will just get all Rows, calculate and order by duration and then cut them off, but I’d like to do it in an SQL Query if possible to make the script clearer.

I know about the LIMIT Clause, but that does not seem to support relative values like ’13 percent’.

Can anyone tell if a) if that is actually possible only with SQL and b) how it could be achieved?

MySQL is 5.0.45.

PS: And yes, I know that 13% seems like a very strange number, but that’s all part of a bigger process.

  • 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. 2026-05-11T05:49:16+00:00Added an answer on May 11, 2026 at 5:49 am

    You need to LIMIT the data you are returning by what percentile the returned rows in the resultset are at.

    Try this:

    SELECT TrackingID, OpenTime, FirstPoint FROM PointsTable ORDER BY FirstPointGMT - OpenTimeGMT LIMIT (13*((SELECT COUNT(*) FROM PointsTable) +1) / 100),  ((SELECT COUNT(*) FROM PointsTable)  -  (26*((SELECT COUNT(*) FROM PointsTable) +1) / 100) ) 

    Explanation of the above

    The only tricky thing here is the Limit clause, and its syntax:

    > LIMIT start_row,number_of_rows_to_return 

    Our start_row needs to be the first element after the first record 13/100ths (13% of the way) into the result set. To do that, we use this formula:

    rownumber_or_number_of_rows_in_resultset_Npercent_in =  ( Npercent * ( number_of_rows_in_resultset + 1) / 100) 

    which is (13*((SELECT COUNT(*) FROM PointsTable) +1) / 100).

    Our number_of_rows_to_return has to be the total number of rows, minus 26% of the rows (13% from the bottom and 13% from the top), or

    (total_number_of_rows - number_of_rows_26percent_of_the_way_in) 

    Thus the actual calculation is:

    ( (SELECT COUNT(*) FROM PointsTable)   -    (26*((SELECT COUNT(*) FROM PointsTable) +1) / 100)  ) 

    EDIT: After looking over Niyaz’s response, I realized that the query could be rewritten as:

    SELECT TrackingID, OpenTime, FirstPoint FROM PointsTable ORDER BY FirstPointGMT - OpenTimeGMT LIMIT (13*((SELECT COUNT(*) FROM PointsTable) +1) / 100),   (74*((SELECT COUNT(*) FROM PointsTable) +1) / 100) 

    This is cleaner, as it eliminates a third nested query, and should be functionally equivalent. Limit rows, start at the rows 13% from top, and show 74% of the total rows out of the table from that point on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to optimize an database by combining queries. But I keep hitting
I currently have a website that I am trying to optimize in terms of
I'm currently trying to optimize the sluggish process of retrieving a page of log
I have been trying to optimize some code which handles raw pixel data. Currently
I'm trying to optimize my website. I have a few plugins to include (jquery
I am currently trying to optimize some bobj reports where our backend is Teradata.
I am currently trying to optimize some DSP related code with Shark and found
We are currently trying to optimize the performance of an ASP.NET based website which
I've currently been trying to optimize my app with RGB_565 textures rather than RGBA_8888
I'm currently trying to optimize my website, which is run on the Google App

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.