
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to find nth highest value of a MySQL column?
To find the nth highest value of a column, you need to use ORDER BY DESC with LIMIT clause. If you want the second highest value of a column, use the below syntax:
SELECT *FROM yourTableName ORDER BY DESC yourColumnName LIMIT 1,1;
If you want the fourth highest value of a column, use the below syntax:
SELECT *FROM yourTableName ORDER BY DESC yourColumnName LIMIT 3,1;
If you want the first highest value of a column, use the below syntax:
SELECT *FROM yourTableName ORDER BY DESC yourColumnName LIMIT 1;
As discussed in the above syntax, you need to change only in LIMIT clause. To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table NthSalaryDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(10), -> Salary int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.03 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into NthSalaryDemo(Name,Salary) values('Larry',5700); Query OK, 1 row affected (0.41 sec) mysql> insert into NthSalaryDemo(Name,Salary) values('Sam',6000); Query OK, 1 row affected (0.16 sec) mysql> insert into NthSalaryDemo(Name,Salary) values('Mike',5800); Query OK, 1 row affected (0.16 sec) mysql> insert into NthSalaryDemo(Name,Salary) values('Carol',4500); Query OK, 1 row affected (0.17 sec) mysql> insert into NthSalaryDemo(Name,Salary) values('Bob',4900); Query OK, 1 row affected (0.20 sec) mysql> insert into NthSalaryDemo(Name,Salary) values('David',5400); Query OK, 1 row affected (0.27 sec) mysql> insert into NthSalaryDemo(Name,Salary) values('Maxwell',5300); Query OK, 1 row affected (0.21 sec) mysql> insert into NthSalaryDemo(Name,Salary) values('James',4000); Query OK, 1 row affected (0.19 sec) mysql> insert into NthSalaryDemo(Name,Salary) values('Robert',4600); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from NthSalaryDemo;
The following is the output:
+----+---------+--------+ | Id | Name | Salary | +----+---------+--------+ | 1 | Larry | 5700 | | 2 | Sam | 6000 | | 3 | Mike | 5800 | | 4 | Carol | 4500 | | 5 | Bob | 4900 | | 6 | David | 5400 | | 7 | Maxwell | 5300 | | 8 | James | 4000 | | 9 | Robert | 4600 | +----+---------+--------+ 9 rows in set (0.00 sec)
Case 1: Here is the query to get the nth highest value of a column.
The below query will give the fourth highest value of a column ‘Salary’:
mysql> select *from NthSalaryDemo order by Salary desc limit 3,1;
The following is the output:
+----+-------+--------+ | Id | Name | Salary | +----+-------+--------+ | 6 | David | 5400 | +----+-------+--------+ 1 row in set (0.00 sec)
Case 2: Here is the query to get the second highest value of a column ‘Salary’:
mysql> select *from NthSalaryDemo order by Salary desc limit 1,1;
The following is the output:
+----+------+--------+ | Id | Name | Salary | +----+------+--------+ | 3 | Mike | 5800 | +----+------+--------+ 1 row in set (0.00 sec)
Case 3: Here is the query to get the first highest value of a column:
mysql> select *from NthSalaryDemo order by Salary desc limit 1;
The following is the output:
+----+------+--------+ | Id | Name | Salary | +----+------+--------+ | 2 | Sam | 6000 | +----+------+--------+ 1 row in set (0.00 sec)
Case 4: If you want to get the 8th highest value of a column ‘Salary’, use the following query:
mysql> select *from NthSalaryDemo order by Salary desc limit 7,1;
The following is the output:
+----+-------+--------+ | Id | Name | Salary | +----+-------+--------+ | 4 | Carol | 4500 | +----+-------+--------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to select the nth highest value in a column by skipping values
- Select nth highest value in MySQL
- MongoDB query to find the highest numeric value of a column?
- How to find the highest number in a column?
- Query to find Nth maximum value in MySQL
- Getting the highest value of a column in MongoDB?
- How to find highest value in Android sqlite?
- How to get the fourth highest value using MySQL query?
- MySQL order by from highest to lowest value?
- MySQL query to get first two highest column values from a table?
- How to select the maximum value of a column in MySQL?
- How to substring value in a MySQL table column?
- How to append 000 in a MySQL column value?
- Get the highest score value from a single column and the greatest from two columns in MySQL
- How to order by the highest value from two columns in MySQL?
