- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements