Question

How can I LIMIT the results to 10 in the following query? I use SQLSRV.

SELECT Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?
Was it helpful?

Solution

Use TOP:

SELECT TOP 10 Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?

OTHER TIPS

use

select top(10) Id, Firstname, Surname ....

The answer by kevingessner is certainly the easiest way.

I just thought I would throw some alternatives in for fun.

SET ROWCOUNT 10

 SELECT Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?

SET ROWCOUNT 0

Or a more convoluted way:

With q 
as 
(
Select ROW_NUMBER() Over(Order by Id) as rn,
        Id,
        Firstname,
        Surname 
FROM Person WHERE Firstname LIKE ?
)
Select * 
From q
where q.rn <= 10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top