Yes, we can select second largest record from a table without using LIMIT clause. Let us first see an example and create a table −
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.66 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(67); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(92); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(86); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.14 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 78 | | 67 | | 92 | | 98 | | 88 | | 86 | | 89 | +--------+ 7 rows in set (0.00 sec)
Following is the query to select second largest number from a table without using LIMIT clause −
mysql> SELECT MAX(Number) FROM DemoTable WHERE Number < (SELECT MAX(Number) FROM DemoTable);
This will produce the following output −
+-------------+ | MAX(Number) | +-------------+ | 92 | +-------------+ 1 row in set (0.00 sec)