CREATE TABLE STUDENT |
CREATE TABLE STUDENT ( Id INT IDENTITY(1,1), Name VARCHAR(20) ) GO |
INSERT ROWS INTO STUDENT TABLE |
INSERT INTO STUDENT VALUES ('Manish'); INSERT INTO STUDENT VALUES ('Kumar'); INSERT INTO STUDENT VALUES ('John'); INSERT INTO STUDENT VALUES ('Smith'); INSERT INTO STUDENT VALUES ('Vikas'); INSERT INTO STUDENT VALUES ('Sandeep'); INSERT INTO STUDENT VALUES ('Gautam'); INSERT INTO STUDENT VALUES ('Prasad'); INSERT INTO STUDENT VALUES ('Dinesh'); INSERT INTO STUDENT VALUES ('Nidhi'); GO; |
QUERYING THE TABLE USING OFFSET AND FETCH |
--In below query, we are offsetting the rows by 3 records (starting at 4th record) and returning the next 5 rows. SELECT * FROM STUDENT ORDER BY Id OFFSET 3 ROWS FETCH NEXT 5 ROWS ONLY GO; --You will get below result Id Name 4 Smith 5 Vikas 6 Sandeep 7 Gautam 8 Prasad (5 row(s) affected) --you can also use variables with offset and fetch clauses. DECLARE @offset INT=3, @fetch INT=5 SELECT * FROM STUDENT ORDER BY Id OFFSET @offset ROWS FETCH NEXT @fetch ROWS ONLY GO; |