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

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MSDN states the following: Represents a custom date and time… May 11, 2026 at 7:15 pm
  • Editorial Team
    Editorial Team added an answer I'd go with the Express editions of C# and MSSQL.… May 11, 2026 at 7:15 pm
  • Editorial Team
    Editorial Team added an answer Nevermind it was my own stupidity. In my web.config, I… May 11, 2026 at 7:15 pm

Related Questions

What unusual, unexpected consequences have occurred in terms of performance, memory, etc when switching
I have VS2005 and I am currently trying to debug an ASP.net web application.
I am trying to access a sharepoint list programmatically in a webpart, like this.
I am trying to optimize some stored procedures on a SQL Server 2000 database

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.