

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get second largest marks from a MySQL table using subquery?
Let us first create a table −
mysql> create table DemoTable( Marks int ); Query OK, 0 rows affected (1.34 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(67); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(76); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(86); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.13 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 −
+-------+ | Marks | +-------+ | 78 | | 88 | | 67 | | 76 | | 98 | | 86 | | 89 | | 99 | +-------+ 8 rows in set (0.00 sec)
Following is the query to get a second-largest mark from MySQL table −
mysql> select Marks from DemoTable where Marks=(select MAX(Marks) from DemoTable where Marks < (select MAX(Marks) from DemoTable));
This will produce the following output −
+-------+ | Marks | +-------+ | 98 | +-------+ 1 row in set (0.03 sec)
- Related Questions & Answers
- Find max and second max salary for a MySQL Employee table using subquery?
- Can we select second largest record from a table without using LIMIT clause in MySQL?
- How to get the second last record from a table in MySQL?
- Get the second last row of a table in MySQL?
- Find second max in a table using MySQL query?
- How can I use MySQL subquery as a table in FROM clause?
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- MySQL SELECT from a subquery and then perform DELETE?
- Find percentage from marks in MySQL
- Get last second of a date in MySQL?
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- How can we use a MySQL subquery with FROM clause?
- Program to find second largest digit in a string using Python
Advertisements