Find the next lowest number higher than a certain number in MySQL?

MySQLMySQLi Database

For this, use aggregate function MIN() along with WHERE clause. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values(11);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values(9);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(12);
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Number |
+--------+
|     11 |
|      9 |
|     10 |
|     12 |
+--------+
4 rows in set (0.00 sec)

Following is the query to find the next lowest number higher than a certain number in MySQL −

mysql> select min(Number) from DemoTable where Number > 10;

This will produce the following output −

+-------------+
| min(Number) |
+-------------+
|          11 |
+-------------+
1 row in set (0.00 sec)
raja
Published on 27-Sep-2019 07:10:19
Advertisements