Custom sort before ROW_NUMBER() for paging I have the following

SELECT results.*, coinCat.*, cat.* 
FROM (SELECT c.*, ROW_NUMBER() OVER (ORDER BY c.CoinId) AS RowNumber 
      FROM coins c) AS results
INNER JOIN CoinCategoriesCategories coinCat ON coinCat.CoinId = results.CoinId
INNER JOIN CoinCategories cat ON cat.CategoryId = coinCat.CategoryId
WHERE RowNumber&gt;= @pageNumber * 1 AND RowNumber&lt;@pageNumber *@pageSize
ORDER BY RowNumber


The statement must be nested in order to please Dapper (ORM),

Before applying/paging, I would like my results to be sorted by Coins.Rank but not sure where to stick that part so that my query can run properly and paging works happily

Also would this statement be any different between SQL Server vs PostgresSQL as an example?
instead of ORDER BY c.CoinId i just did ORDER BY c.Rank and that worked

SELECT c.*, ROW_NUMBER() OVER (ORDER BY c.Rank) AS RowNumber FROM coins c