Tuesday 2 January 2018

Selecting Specific set of Rows using OFFSET and FETCH in SQL

We can use OFFSET and FETCH keywords to select specific number of rows in SQL Select query. 

SELECT *  
FROM Tablename  
ORDER BY colname  
OFFSET 0 ROWS  
FETCH NEXT 10 ROWS ONLY  

The above query will returns first 10 records. 

SELECT *  
FROM Tablename  
ORDER BY colname  
OFFSET 10 ROWS  
FETCH NEXT 10 ROWS ONLY  

Above query will returns records 11 to 20.

Likewise you can increase OFFSET values to get next set of records. 

Hope this will be helpful!

No comments:

Post a Comment