How to find a value between range in MySQL?


For this, use BETWEEN operator in MySQL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Start int,
   -> End int
   -> );
Query OK, 0 rows affected (0.91 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Start,End) values(100,200);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable(Start,End) values(400,500);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable(Start,End) values(210,350);
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 −

+----+-------+------+
| Id | Start | End |
+----+-------+------+
|  1 |   100 | 200 |
|  2 |   400 | 500 |
|  3 |   210 | 350 |
+----+-------+------+
3 rows in set (0.00 sec)

Following is the query to find value between range in MySQL −

mysql> select *from DemoTable WHERE 432 BETWEEN Start AND End;

This will produce the following output −

+----+-------+------+
| Id | Start | End  |
+----+-------+------+
|  2 |   400 | 500  |
+----+-------+------+
1 row in set (0.04 sec)

Updated on: 30-Jul-2019

563 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements