The BETWEENÂ operator allows us to select values within a range.
Syntax :
SELECT (column name) FROM table_name
WHERE (column name) BETWEEN 'value1' AND 'value2';
Script:
--first lets drop the table Test (if she exists, if not skip the drop line).
Drop table test;
create table test (id int,name varchar(20),email varchar(20),salary int ,de char(20));
--insert values into table
insert into test values (1,'Eve','[email protected]',1500,'HR');
insert into test values (2,'Jon','[email protected]',2500,'AD');
insert into test values (3,'Mike','[email protected]',3000,'AD');
insert into test values (4,'Paul','[email protected]',3200,'HR');
insert into test values (5,'Mary','[email protected]',1800,'IT');
insert into test values (6,'Jane','[email protected]',2200,'IT');
SQLselect * from test ;
ID NAME EMAIL SALARY DE
---------- -------------------- -------------------- ---------- -- --------
1 Eve [email protected] 1500 HR
2 Jon [email protected] 2500 AD
3 Mike [email protected] 3000 AD
4 Paul [email protected] 3200 HR
5 Mary [email protected] 1800 IT
6 Jane [email protected] 2200 IT
Example 1:
SQLselect name from test where id between 1 and 3;
NAME
--------------------
Eve
Jon
Mike
Example 2 :
-now we will use string values:SQLselect name from test where name between 'Eve' and 'Mike';
NAME
--------------------
Eve
Jon
Mike
Mary
Jane
Here is the script
Create table aaa( letter char(10));
Insert into aaa values('a');
Insert into aaa values('b');
Insert into aaa values('c');
Insert into aaa values('d');
Insert into aaa values('e');
Insert into aaa values('f');
Insert into aaa values('g');
Insert into aaa values('h');
Example :
SQLSelect * from aaa
where leter between 'a' and 'd';Letter-ab cd
Example :
SQLSelect * from aaa
where leter between 'a' and 'x';
Letter
--------------------
a
b
c
d
e
f
g
h