I want to make a database query with pagination. So, I used a common-table expression and a ranked function to achieve this. Look at the example below.
DECLARE @pageNumber INT = 1 ,
@RowsPerPage INT = 20
SELECT *
FROM TableName
ORDER BY Id
OFFSET ( ( @pageNumber - 1 ) * @RowsPerPage ) ROWS
FETCH NEXT @RowsPerPage ROWS ONLY;