SQL

SQL Between

SQL Between

This page covers SQL Between.

The “between” operator is part of the logical operators.

As the name suggests, the operator “between” retrieves data between a given range of values.

The keyword is “between” and the statement requires start and end values. Both values are included.

For more information on operators here.

Let’s use the table “student_info” from the previous tutorials.

idfirstNamelastNameage
1JohnSmith21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL
5PatrickDumont21

Between

“Between” works well with the “where” statement.

The following is an example of the “between” logical operator.

SELECT * FROM student_info
WHERE age BETWEEN 15 AND 25;

The above statement selects all students, with the filter of age between 15 and 25 years old.

Let’s see the result.

idfirstNamelastNameage
1JohnSmith21
3SamAdams19
5PatrickDumont21

Not Between

The operator “between” can also be combined with the logical operator “not”.

The combination reverses the effect of the filter, meaning it displays records outside of the given range.

Below is an example of the “not between” combination.

SELECT * FROM student_info
WHERE age NOT BETWEEN 15 AND 25;

The outcome outputs one student.

idfirstNamelastNameage
2PatrickCasey26

The filter above retrieves all student data that is not within the range of values. In other words, all students that are not 15 years old or 25 years old, or anything between those 2 values.


Next: SQL In

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.