How to use LIMIT keyword in SQL Server 2005?

user11445 picture user11445 · Jan 18, 2009 · Viewed 55.4k times · Source

I have found a way to select random rows from a table in this post. A suggestion is to use the following query:

SELECT * FROM employee ORDER BY RAND() LIMIT 1

But when I run this query in MS SQL 2005, I get the following error message

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'LIMIT'.

Can anyone tell me where am I wrong? Doesn't MS SQL support LIMIT? If so, then how can I do this?

Answer

Frederik Gheysels picture Frederik Gheysels · Jan 18, 2009

If you take a look at the SELECT statement in SQL Server Books Online, then you'll see that you can limit the resultset by using the TOP keyword.

SELECT TOP 1 * FROM employee