MySQL BETWEEN Operator

MySQL Tutorial

MySQL BETWEEN Operator

The BETWEEN operator in MySQL is used in a WHERE clause to filter the result set within a specific range of values. It can be used with numbers, dates, or text.


Syntax


  • Returns all rows where the column value is greater than or equal to value1 and less than or equal to value2.

  • Inclusive of both ends (value1 and value2).


🧠 Example Table: students

idnameagemarks
1John2085
2Emma2190
3Raj2276
4Arjun2392
5Sara1988

✅ Example 1: Numbers (Range of Ages)


✔ Returns students with age 20, 21, or 22:

idnameagemarks
1John2085
2Emma2190
3Raj2276

✅ Example 2: Numbers (Range of Marks)


✔ Returns students with marks between 80 and 90 (inclusive):

idnamemarks
1John85
2Emma90
5Sara88

✅ Example 3: Dates

Assume a table orders:

idorder_dateamount
12025-11-01500
22025-11-05700
32025-11-10300
42025-11-15900

✔ Returns orders on Nov 5 and Nov 10 as well as dates in between.


✅ Example 4: NOT BETWEEN

You can exclude values outside a range:


✔ Returns students younger than 20 or older than 22:

idnameage
4Arjun23
5Sara19

🏁 Summary

OperatorPurpose
BETWEENSelect values within a range (inclusive)
NOT BETWEENSelect values outside a range

You may also like...