DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Recover a Deleted Table in a SQL Server Database
  • Restoring the MS SQL Server Database in Easy Steps
  • How To Convert MySQL Database to SQL Server
  • How To Fix SQL Database Restore Failed, Database Is in Use

Trending

  • Problems With Angular Migration
  • My LLM Journey as a Software Engineer Exploring a New Domain
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Failure Handling Mechanisms in Microservices and Their Importance
  1. DZone
  2. Data Engineering
  3. Databases
  4. Pagination in MS SQL Server

Pagination in MS SQL Server

What is pagination and how does it work in Microsoft SQL Server? In this article, we go over these questions and other important points to consider.

By 
NaveenKumar M user avatar
NaveenKumar M
·
Updated Mar. 04, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
29.3K Views

Join the DZone community and get the full member experience.

Join For Free

Pagination is the process of dividing large data into smaller data sets in discrete pages. It is widely used in web applications.

How Does Pagination Work in MS SQL Server?

In MS SQL Server, we can achieve the pagination functionality by using OFFSET and FETCH clauses with ORDER BY in a SELECT statement.

  • OFFSET: Represents the number of rows to be skipped from the result set. It should be 0 or greater than 0.
  • FETCH: Represents the number of rows to be displayed in the result.

Important Points to Consider While Using OFFSET and FETCH:

  • ORDER BY is mandatory to the use OFFSET FETCH Clause.
  • OFFSET is mandatory and FETCH is optional.
  • The TOP clause cannot be used in the SELECT statement with OFFSET FETCH.

Let's see examples:

In the below example, OFFSET 0 and FETCH NEXT 5 ROWS means skip no rows and return the next 5 rows in the dataset, which are the first 5 rows in the dataset.

MS SQL
 
SELECT EmployeeKey,FirstName,LastName,Title,EmailAddress,
Phone,EmergencyContactName,DepartmentName
FROM dbo.DimEmployee WITH(NOLOCK)
ORDER BY EmployeeKey 
OFFSET 0 ROWS
FETCH NEXT 5 ROWS ONLY



OFFSET 0 and FETCH NEXT 5 ROWS


In the below example, OFFSET 3 and FETCH NEXT 5 ROWS mean skip the first 3 rows and return the next 5 rows in the dataset.

MS SQL
 
SELECT EmployeeKey,FirstName,LastName,Title,EmailAddress,
Phone,EmergencyContactName,DepartmentName
FROM dbo.DimEmployee WITH(NOLOCK)
ORDER BY EmployeeKey 
OFFSET 3 ROWS
FETCH NEXT 5 ROWS ONLY



OFFSET 3 and FETCH NEXT 5 ROWS

PageNumberRowsOfPage

  • PageNumber — Represents the page number
  • RowsOfPage — Represents the no of rows on a page

Now, to calculate the number of rows to be skipped when we move to the next page the (@PageNumber-1) * @RowsOfPage formula is being used in the OFFSET, and the number of rows will be returned in FETCH.

Here, PageNumber is 2 and RowsOfPage is 5, which means return the dataset for page number 2.

MS SQL
 
DECLARE @PageNumber AS INT
       ,@RowsOfPage AS INT

SET @PageNumber=2
SET @RowsOfPage=5

SELECT EmployeeKey,FirstName,LastName,Title,EmailAddress,
Phone,EmergencyContactName,DepartmentName
FROM dbo.DimEmployee WITH(NOLOCK)
ORDER BY EmployeeKey
OFFSET (@PageNumber-1) * @RowsOfPage ROWS
FETCH NEXT @RowsOfPage ROWS ONLY



OFFSET (@PageNumber-1) * @ RowsofPage and FETCH NEXT @RowsOfPage ROWS


Conclusion

In the real-time application, this complete logic can be written in the stored procedure which is called by clicking the "Next" or page number button in the web application to display the set of records on the page. 

Microsoft SQL Server sql Database

Opinions expressed by DZone contributors are their own.

Related

  • How to Recover a Deleted Table in a SQL Server Database
  • Restoring the MS SQL Server Database in Easy Steps
  • How To Convert MySQL Database to SQL Server
  • How To Fix SQL Database Restore Failed, Database Is in Use

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor