How to get the 2nd highest value from a table with Student Score?


To fetch the 2nd highest value, use ORDER BY DESC with LIMIT 1,1. Let us first create a table −

mysql> create table DemoTable
(
   StudentScore int
);
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(89);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(69);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(97);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(99);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+
| StudentScore |
+--------------+
|           89 |
|           69 |
|           97 |
|           99 |
+--------------+
4 rows in set (0.00 sec)

Following is the query to get the 2nd highest value −

mysql> select *from DemoTable order by StudentScore DESC limit 1,1;

This will produce the following output −

+--------------+
| StudentScore |
+--------------+
|           97 |
+--------------+
1 row in set (0.00 sec)

Updated on: 25-Sep-2019

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements