Select a specific value between two column values in MySQL?


Let us first create a table −

mysql> create table DemoTable787 (
   Score1 int,
   Score2 int,
   Name varchar(100)
);
Query OK, 0 rows affected (0.84 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable787 values(34,56,'Chris');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable787 values(73,86,'Robert');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable787 values(90,99,'David');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable787 values(80,89,'Adam');
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable787;

This will produce the following output -

+--------+--------+--------+
| Score1 | Score2 | Name   |
+--------+--------+--------+
| 34     | 56     | Chris  |
| 73     | 86     | Robert |
| 90     | 99     | David  |
| 80     | 89     | Adam   |
+--------+--------+--------+
4 rows in set (0.00 sec)

Following is the query to select a specific value between values of two columns −

mysql> select Name from DemoTable787 where 56 between Score1 and Score2;

This will produce the following output -

+-------+
| Name  |
+-------+
| Chris |
+-------+
1 row in set (0.00 sec)

Updated on: 09-Sep-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements