EN
MS SQL Server - LIMIT clause
0 points
In this article, we would like to show you how to use LIMIT
clause in MS SQL Server.
Quick solution:
xxxxxxxxxx
1
SELECT [column1], [column2], ...
2
FROM [table_name]
3
WHERE condition
4
LIMIT number
5
OFFSET number;
To show how the LIMIT
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 all information about the first two users.
Query:
xxxxxxxxxx
1
SELECT *
2
FROM [users]
3
LIMIT 2;
Output:

In this example, we will also display all information about users limited to 2, but this time we set an OFFSET
to skip the first user.
Query:
xxxxxxxxxx
1
SELECT *
2
FROM [users]
3
LIMIT 2
4
OFFSET 1;
Output:

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');