SQL BETWEEN Operator



SQL BETWEEN Operator :

  1. SQL BETWEEN Operator is used to select the values within the range.
  2. SQL BETWEEN operator is used along with WHERE clause
  3. SQL BETWEEN uses numbers, text, or dates values to select range.

Syntax :

Syntax for the BETWEEN Operator is as below –

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);

Demo Database :

Consider the following demo table –

+----+----------+-----+-----------+----------+
| ID | ENAME    | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Kota      |  2000.00 |
|  2 | Raj      |  23 | Delhi     |  1500.00 |
|  3 | Ram      |  21 | Karachi   |  2000.00 |
|  4 | Saurabh  |  25 | Mumbai    |  6500.00 |
|  5 | Poonam   |  29 | Bhopal    |  8500.00 |
|  6 | Komal    |  23 | Pune      |  4500.00 |
|  7 | Arnav    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

SQL BETWEEN Operators : Using Number

Consider below query on demo database –

SELECT * FROM EMPLOYEE
WHERE AGE BETWEEN 21 AND 25;

will result into

+----+----------+-----+-----------+----------+
| ID | ENAME    | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  2 | Raj      |  23 | Delhi     |  1500.00 |
|  3 | Ram      |  21 | Karachi   |  2000.00 |
|  4 | Saurabh  |  25 | Mumbai    |  6500.00 |
|  6 | Komal    |  23 | Pune      |  4500.00 |
|  7 | Arnav    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

Above SQL BETWEEN statement will be used to select all the records between the age 21 and 25

SQL BETWEEN Operators : Using String

Consider below query on demo database –

SELECT * FROM EMPLOYEE
WHERE ADDRESS BETWEEN 'K' AND 'P';

will result into

+----+----------+-----+-----------+----------+
| ID | ENAME    | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Kota      |  2000.00 |
|  3 | Ram      |  21 | Karachi   |  2000.00 |
|  4 | Saurabh  |  25 | Mumbai    |  6500.00 |
|  6 | Komal    |  23 | Pune      |  4500.00 |
+----+----------+-----+-----------+----------+

SQL BETWEEN statement will be used to select all records with the ADDRESS beginning with any of the letter BETWEEN ‘K’ and ‘P’