TOP Statement

SELECT TOP 10 * FROM [AdventureWorks].[Person].[Person];

DECLARE @Limit AS INT = 6;
SELECT TOP (@Limit) * 
FROM [AdventureWorks].[Person].[Person];

SET @Limit = 4;
SELECT TOP (@Limit) WITH TIES * 
FROM [AdventureWorks].[Person].[Person]
ORDER BY [LastName];

SELECT TOP (SELECT COUNT(*) FROM [AdventureWorks].[Person].[Address]) *
FROM [AdventureWorks].[Person].[Person];

UPDATE TOP (@Limit) [AdventureWorks].[Person].[Person]
SET [LastName] = [LastName] + 'Test'
WHERE BusinessEntityID = 3;

Pagination After SQL Server 2012

DECLARE
	@RowsPage AS INT = 20,
	@PageNumber AS INT = 10;

SELECT *
FROM [Test]
ORDER BY [Id]
OFFSET (@PageNumber - 1) * @RowsPage ROWS
FETCH NEXT @RowsPage ROWS ONLY;

Pagination Before SQL Server 2012

DECLARE
	@RowsPage AS INT = 20,
	@PageNumber AS INT = 10;

SELECT 
	[Id],
	[Name]
FROM
(
	SELECT 
		ROW_NUMBER() OVER(ORDER BY [Id]) AS [Number],
		[Id], 
		[Name]
	FROM [Test]
) AS [Tbl]
WHERE [Number] BETWEEN ((@PageNumber - 1) * @RowsPage + 1) AND (@PageNumber * @RowsPage)
ORDER BY [Id]

RANK

Ranking rows within a partition

SELECT
	RANK() OVER (PARTITION BY [LocationID] ORDER BY [Quantity] DESC) AS [Rank],
	[LocationID],
	[Quantity]
FROM Production.ProductInventory
WHERE LocationID BETWEEN 3 AND 4
ORDER BY [LocationID], [Quantity] DESC;

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4c308359-290f-40f6-8566-071e6d172ebd/Untitled.png

DENSE_RANK