Home

Queries that use TOP should have an ORDER BY

Rule description

  • Queries that use TOP should have an ORDER BY

 

Non-compliant Code Example

SELECT TOP(10)JobTitle, HireDate   --Non compliant code (Query use TOP without Order By clause, which will select the first 10 random employees.)
FROM HumanResources.Employee;  
GO  

Compliant Code Example

SELECT TOP(10)JobTitle, HireDate   --Compliant code (Query use TOP with Order By clause, which will select the first 10 employees hired most recently.  )
FROM HumanResources.Employee  
ORDER BY HireDate DESC;  
GO
Visual Expert 2020
 VETSQLRULE13