
- 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
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 Articles
- 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?
- How can I use MySQL subquery as a table in FROM clause?
- Get the second last row of a table in MySQL?
- Find second max in a table using MySQL query?
- 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?
- What is the concept of a derived table concerned with MySQL subquery?
- How can we use a MySQL subquery with FROM clause?
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- How to get username using ID from another table in MySQL database?
- A single MySQL query to insert records (not all) in the second table from the first table
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- How can we fetch a second highest salary of an employee from a MySQL table?

Advertisements