EN
MS SQL Server - BETWEEN clause
0 points
In this article, we would like to show you how to use BETWEEN
clause in MS SQL Server.
Quick solution:
xxxxxxxxxx
1
SELECT * FROM [table_name] WHERE [column_name] BETWEEN value_1 AND value_2;
To show how BETWEEN
clause works, we will use the following table:

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will display users with id
column values between 2
and 4
.
Query:
xxxxxxxxxx
1
SELECT * FROM [users] WHERE [id] BETWEEN 2 AND 4;
Result:

Note:
BETWEEN clause is used very often for dates.
create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE [users] (
2
[id] INT IDENTITY(1,1),
3
[name] VARCHAR(100) NOT NULL,
4
[role] VARCHAR(15) NOT NULL,
5
PRIMARY KEY ([id])
6
);
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO [users]
2
([name], [role])
3
VALUES
4
('John', 'admin'),
5
('Chris', 'moderator'),
6
('Kate', 'user'),
7
('Denis', 'moderator');