MySQL Nth Highest & Lowest Values: Query Guide

To search for the Nth highest or Nth lowest value, you can use MySQL’s subquery and ORDER BY statement.

To find the Nth highest value, you can use the following query statement:

SELECT DISTINCT column_name 
FROM table_name 
ORDER BY column_name DESC 
LIMIT N-1, 1;

To find the Nth lowest value, you can use the following query statement:

SELECT DISTINCT column_name 
FROM table_name 
ORDER BY column_name ASC 
LIMIT N-1, 1;

In the above query, column_name is the name of the column to be searched, table_name is the name of the table to be queried, and N is the position of the value to be found. The ORDER BY statement is used to sort the results in ascending or descending order by column, with DESC indicating descending order and ASC indicating ascending order. LIMIT is used to limit the number of results, with the first parameter as the offset and the second parameter as the number of rows to return.

bannerAds