What is the alternative of BETWEEN clause to fetch values between a range in MySQL?

MySQLi DatabaseMySQL

To avoid using the BETWEEN clause, you can use the AND to fetch the values between a range. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(20);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values(60);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(100);
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable values(90);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(85);
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 −

+--------+
| Number |
+--------+
|     10 |
|     20 |
|     60 |
|    100 |
|     90 |
|     85 |
+--------+
6 rows in set (0.00 sec)

Following is the query to fetch values between a range without using the BETWEEN clause −

mysql> select *from DemoTable where 15 <=Number and Number <= 80;

This will produce the following output −

+--------+
| Number |
+--------+
|     20 |
|     60 |
+--------+
2 rows in set (0.00 sec)
raja
Published on 01-Oct-2019 08:10:06
Advertisements