MySQL Tutorial
The BETWEEN operator selects values within a certain range.
The BETWEEN operator is inclusive: begin and end values are included.
The values can be numbers, text, or dates.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Let's put these statements into real use.
We've a table named Students in our database that contains the following records:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | JAVA |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara | JAVA |
Now lets select values in a certain range from Students table.
SELECT * FROM Students
WHERE ROLL_NO BETWEEN 2 AND 5;
The above MySQL statement selects all students that their ROLL_NO is BETWEEN 2 and 5.
ROLL_NO | NAME | SUBJECT |
---|---|---|
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
Now lets select values in a outside the range from Students table.
SELECT * FROM Students
WHERE ROLL_NO NOT BETWEEN 2 AND 5;
The above MySQL statement selects all students that their ROLL_NO is NOT BETWEEN 2 and 5.
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | JAVA |
6 | Lara | JAVA |