13. MySQL: BETWEEN Operator

13. MySQL: BETWEEN Operator

The BETWEEN operator in MySQL is used to filter results based on a range of values. It is often used in the WHERE clause of a SELECT, UPDATE, or DELETE statement to retrieve or modify rows within a specific range.

Points To Remember:

  • The values can be text, numbers, and dates.

  • The between operator is inclusive, meaning, the start and end values are included in the result.

BETWEEN OPERATOR:

The basic syntax of the BETWEEN operator is as follows:

SELECT column_1, column_2
FROM table_name
WHERE column_name BETWEEN value_1 AND value_2;

Let's have a look at the film table before running any queries on it.

SELECT * FROM FILM;

Output:


We only have 40-50 mins to watch a movie, what can we watch?

SELECT title, release_year, length, rating,last_update
FROM film
WHERE length BETWEEN  40 AND 50 ;

Output:


We can either spend 40 minutes, or we can spend more than 50 minutes watching a movie, what can we watch?

SELECT title, release_year, length,last_update
FROM film
WHERE length NOT BETWEEN  40 AND 50 
ORDER BY length;

Output:


We want to watch a movie whose length is 60 minutes or less, but the movie has to have a rating of PG or PG-13.

SELECT title, release_year, length, rating,last_update
FROM film
WHERE length BETWEEN  0 AND 60
AND rating IN ('PG','PG-13');

Output:


When working with text it will give everything in ascending order.

SELECT title, release_year, length, rating,last_update
FROM film
WHERE title BETWEEN 'ALTER VICTORY' AND 'DEEP CRUSADE';

Output:

In conclusion, the BETWEEN operator is a useful tool for filtering results in MySQL. It allows you to retrieve or modify rows within a specific range, making it an effective tool for finding records within a given time frame or for other purposes.


'll meet again tomorrow!

To see more examples, you can always refer w3 schools; it has always been my go-to website to understand things.

References: w3schools.com/mysql/default.asp

*****All the outputs provided are a snippet of the actual result.