Freigeben über


"LIMIT" in SQL Server

I've recently come across a number of folks in different contexts who were trying to figure out how to acheive the equivalent of MySQL's "LIMIT" clause in SQL Server.  The basic scenario is that you want to return a subset of the results in a query from row number X to row number Y.

The good news is that SQL Server 2005 makes this really easy.  We introduced a new set of ranking functions into the T-SQL language that let you accomplish the basic LIMIT semantics and much more if you feel like getting fancy.

Quick example:

SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY name) as row FROM sys.databases ) a WHERE row > 5 and row <= 10

The query above returns rows 6 through 10 from sys.databases as ordered by the "name" column.  ROW_NUMBER() is the key function we're using here.  It's one of a set of ranking functions introduced in 2005.  Note that it's always accompanied by an OVER clause that specifies the ordering that the row_number should be based on. 

For details on the ROW_NUMBER() function and its use, see: https://msdn2.microsoft.com/en-us/library/ms186734.aspx

For info on other ranking functions in SQL Server 2005, see: https://msdn2.microsoft.com/en-us/library/ms189798.aspx

Comments

  • Anonymous
    December 03, 2006
    Or used with a CTE like in WITH TblDatabases  AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY Name) as Row FROM sys.databases ) SELECT * FROM TblDatabases WHERE Row>5 and Row<10

  • Anonymous
    March 14, 2007
    Wouldn't it just be better to implement a LIMIT clause into the next version of SQL Server?!?

  • Anonymous
    March 21, 2007
    no kidding.. LIMIT and OFFSET are so useful

  • Anonymous
    April 02, 2007
    I also agree with you Lloyd. What if I have to use SqlServer 2000?

  • Anonymous
    April 08, 2007
    ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss'''''''''jklklklklklklklklklklklklklklklklklklklklklklklkl

  • Anonymous
    May 03, 2007
    For paging pre 2005... http://www.4guysfromrolla.com/webtech/042606-1.shtml

  • Anonymous
    May 14, 2007
    How to use Limit Function in sql, give me a some sample codes Thank u

  • Anonymous
    August 07, 2007
    MySQL SELECT emp_id,lname,fname FROM employee LIMIT 20,10 SQL Server select * from ( select top 10 emp_id,lname,fname from (    select top 30 emp_id,lname,fname    from employee   order by lname asc ) as newtbl order by lname desc ) as newtbl2 order by lname asc from http://www.fluzo.org/post/300

  • Anonymous
    August 15, 2007
    i am trying to get to ADVFN.CO.UK THIS IS WHAT I AM GETTING ANY ONE HELP Bad Request Your browser sent a request that this server could not understand. Size of a request header field exceeds server limit. Cookie: OASISID=41e97894d34f5; OASISCAP=a%3A76%3A%7Bi%3A111%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1105819796%3B%7Di%3A292%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1105819990%3B%7Di%3A293%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1105820109%3B%7Di%3A228%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1105915958%3B%7Di%3A295%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1105878340%3B%7Di%3A294%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1105878349%3B%7Di%3A596%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1113210421%3B%7Di%3A499%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1113338548%3B%7Di%3A777%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1113507993%3B%7Di%3A653%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1113507285%3B%7Di%3A1340%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1132218022%3B%7Di%3A1313%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1123483384%3B%7Di%3A592%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1136333534%3B%7Di%3A1353%3Ba%3A2%3A%7Bi%3A0%3Bi%3A1%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0%3Bi%3A1%3Bi%3A1%3Bi%3A1187165308%3Bi%3A2%3Bi%3A1187251708%3B%7Di%3A6490%3Ba%3A3%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1187167028%3Bi%3A2%3Bi%3A1187253428%3B%7Di%3A6584%3Ba%3A3%3A%7Bi%3A0%3Bi%3A2%3Bi%3A1%3Bi%3A1187167212%3Bi%3A2%3Bi%3A1187253612%3B%7Di%3A5972%3Ba%3A3%3A%7Bi%3A0%3Bi%3A2%3Bi%3A1%3Bi%3A1187169163%3Bi%3A2%3Bi%3A1187255563%3B%7Di%3A6642%3Ba%3A3%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1187170572%3Bi%3A2%3Bi%3A1187256972%3B%7Di%3A5883%3Ba%3A3%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1187171228%3Bi%3A2%3Bi%3A1187257628%3B%7Di%3A6643%3Ba%3A3%3A%7Bi%3A0%3Bi%3A1%3Bi%3A1%3Bi%3A1187172060%

  • Anonymous
    August 22, 2007
    Thank you! Your example was the most "clean" and easy all around he pages that I've checked! To Peter Schneider, CTE is good and clean... but is not working over ASP pages :)

  • Anonymous
    September 13, 2007
    Are there any intentions of implementing a real LIMIT statement into SQL Server? It is an SQL standard after all.

  • Anonymous
    October 14, 2007
    I really hope SQL Server adds LIMIT and OFFSET

  • Anonymous
    November 20, 2007
    > The good news is that SQL Server 2005 makes this really easy No it doesn't.  Implement the LIMIT statement.

  • Anonymous
    December 18, 2007
    Yeah, frankly, compared to MySQL here: SELECT * from bar WHERE blah LIMIT 10,20 -- gets records 10-20 SQL Server is DISGUSTING and horribly deficient.

  • Anonymous
    January 02, 2008
    @Ryan: we don't think sql server is disgusting. The other way round! For our emailmarketing solution we switched from MySql to SqlServer 2005 because was MUCH more stable (our windows services can now run without having to restart it every 3 days) and performant.

  • Anonymous
    January 26, 2008
    Dirk: Your comment really doesnt have anything to do with fact that Sql Server "trick" for LIMIT is disguisting. Btw, if you were forced to restart MySql every 3 days, problem is probably in your application, not the DB engine itself.

  • Anonymous
    February 18, 2008
    This might look nice and seems to work well, however it does involve a full table scan, which in my case means traversing over a billion records. As I can remember mysql limit does not need a FTS, so with a lot of rows, mysql performs better. Which off course brings back the question already asked: Why not implement limit into SQL server?

  • Anonymous
    February 20, 2008
    LIMIT is so much easier and cleaner.  We should send emails to SQL Server feedback team about it.

  • Anonymous
    February 21, 2008
    Rather than changing the sql2005, they added a new function row_number() apparently, and supposedly it involves many changes in the SQL interpretation side to add LIMIT clause.. Stupidity arises when you have 1million records! (Because m$ sql has only use of TOP keyword) if you have to select a 1000 records in the middle of the recordset, you have to select half a million records in ASC order, then reverse the subquery using DESC and you get your 1000 records. They simply didn't implement the db engine performance in mind, they must be using very old source code and they don't want to change loads of stuff because of "our silly little "LIMIT" clause as users. I'm sure they'll implement it when THEY need it or feel like that!! Good luck! :( I use m$ sql but try to avoid it where possible. Give way to real databases such as PostgreSQL and Firebird! :)))

  • Anonymous
    February 21, 2008
    Why just not implement LIMIT? I've been working with MySQL for a few years now, and the only reason I'm using SQL Server now is because we use it at work. I tried to run some SQL queries written for MySQL, but it keeps complaining on my LIMIT clauses, which is very annoying and disturbing.

  • Anonymous
    March 24, 2008
    "The good news is that SQL Server 2005 makes this really easy" Really easy compared to SQL Server 2000, which is a nightmare to make a simple LIMIT query. If ROW_NUMBER() is wider than LIMIT, it's much more complex to use and LIMIT solves 99% of our daily problems, why bother with ROW_NUMBER() ? I'm starting to regreat using MSSQL simply because you didn't implement something so OBVIOUS as LIMIT. I'll probably move to MySQL because of this. I have lots of code to write on paging and MSSQL is just horrable for this OBVIOUS task. What's wrong with you guys ?

  • Anonymous
    April 07, 2008
    Thanks for the example.  I will be using it. As it is, I am sympathetic with the angst in this thread: even SQLite has LIMIT and OFFSET.  Your links are not entirely unhelpful, but I would be grateful for a straightforward cookbook of SQL Server "quirk-arounds" built for folk like me who lack the motivation, time, or the chops, (or all of the above), to become M$SS gurus.

  • Anonymous
    June 17, 2008
    He has the gall to call that inverted, ten-times-longer solution "really easy".

  • Anonymous
    August 28, 2008
    I my experience with MySQL, there is a decay in performance when using LIMIT to skip over large record sets; so I wouldn't use MySQL's implementation of LIMIT as the gold standard.  Nevertheless, the ease of use of LIMIT is enviable.  I try, if possible, to use a BETWEEN on a primary key.  It's faster on large record sets.

  • Anonymous
    November 17, 2008
    Thank you for this awesome article. You have answered a very confusing question, in a very understanding manner... Thanx

  • Anonymous
    December 10, 2008
    Thanks Dan, good to learn some new things. Microsoft always have to do things in their own way, never follow standards: that's the motto, always was and always will be. It's not always bad, but it surely explains why there will never be real "LIMIT" in SQL Server. ;)

  • Anonymous
    March 12, 2009
    HEY , IT IS VERY USEFUL TO MY PROJECT AND MY KNOWLEDGE.. THANKS GUY

  • Anonymous
    March 19, 2009
    The comment has been removed

  • Anonymous
    June 23, 2009
    Chris: There's no reason for personal attacks and it's up to you if you want to feel sorry for people who are perfectly happy with a solution that works for them. Limit in MySQL doesn't have to go through every record.  That's kind of the point of using it, to save your database some work and improve performance. And it's frustrating for users coming from other database systems, not just MySQL - PostgreSQL is matches every stability argument for MsSQL and supports limiting queries in a simple syntax. Not supporting a limiting clause slows down database queries, and doesn't run as efficiently for your application.  Inefficiencies like this mean you have to have a lot more memory than your system really ought to be able to run on. This is a perfectly reasonable request for users to have as many other databases, (even SQLite as mentioned above) support limit.

  • Anonymous
    July 30, 2009
    very useful this post... but if you need ROW_NUMBER() inside a joined tables see this http://www.cto247.com/blog/post/2009/03/02/Using-Row_Number()-in-SQL-2005-When-Joining-Multiple-Tables.aspx PARTITION BY awesome thanks for this post very usefull

  • Anonymous
    July 30, 2009
    In writing ORM middleware(s) to interact with multiple databases, symmetry in code helps to reduce complexity in the ware(s). Microsoft has the resources to provide the convenience (and easy of adaptability) that developers need when working with multiple databases, and has the responsibiity to make sure that the implementation is efficient. So, the SLQ Server Stratey team should implement the LIMIT clause (or an equivalent if there is an IP issue) and do so in a way that will make everyone happy.

  • Anonymous
    August 16, 2009
    This mssql is like ie6 that give problems to developers when it came to compatibility with other softwares. Hard to belive that Microsoft let us hard to adjust for thier products.

  • Anonymous
    August 16, 2009
    This mssql is like ie6 that give problems to developers when it came to compatibility with other softwares. Hard to belive that Microsoft let us hard to adjust for thier products.

  • Anonymous
    August 27, 2009
    Thanks, danwinn. I used this code and it worked perfectly for me. Dumb, and probably unrelated question, though - I noticed that it doesn't work at all without the alias ("a" in the example), even though it's not referenced anywhere else. Anyone know why this is?

  • Anonymous
    September 23, 2009
    MySql limit is useless for dynamic limit values. example is: select * from licenses limit @limit. better to create your own way to select records with limits. MSSQL allow dynamic Top values Example is: select top (@limit) * from licenses

  • Anonymous
    October 19, 2009
    The comment has been removed

  • Anonymous
    December 09, 2009
    Hi , Limit is not useful in stored procedures, You cannot assign dynamic limit range on MySQL unlike The equivalent of it in MsSQL. I created a sample of MsSQL here http://epiece.net/article/MSSQL_Limit_equivalent_to_MYSQL_limit_function_.html . You can use it as reference. Thanks. PREPARE stmt FROM 'SELECT * FROM licenses LIMIT ?'; will not be useful. We should avoid practicing that way when we prioritize speed.

  • Anonymous
    January 15, 2010
    The comment has been removed

  • Anonymous
    March 30, 2010
    @Mike Fulton   Thanks... I've just shared what I'm thinking. Based on my experience using MSSQL and MYSQL, LIMIT is more easier to use than that of ROW_NUMBER() since you need to define all order column. But ROW_NUMBER() is more powerfull :) . Programmer love challenging syntax ...

  • Anonymous
    March 30, 2010
    Here is another example i posted on http://epiece.net/article/MSSQL_Limit_equivalent_to_MYSQL_limit_function_.html declare @tbl as table( idx int identity(1,1), name varchar(10) ) insert into @tbl SELECT name FROM sys.databases select * from  @tbl where idx between 10 and 20 this may help for small resultset.

  • Anonymous
    May 11, 2010
    The comment has been removed

  • Anonymous
    September 25, 2010
    Don't be afraid of a few lines of simple code!

  • Anonymous
    March 11, 2011
    I made a testing comment before this one so don't think I am a spammer cause I have never posted here before. anyway as a suggest I would say don't use limit instead use this. int i=0; Where(reader.read()) {  if(i == 5) { break; } } this would make your work easy. infact it doesn't take much time cause the read() method (in C#) read one by one record (that means read next record and so fourth) so when it reaches the i==5 condition you can break the reading loop. and close the connection. it's much easier than this tutorial.

  • Anonymous
    March 31, 2011
    This would be easy enough to understand Worked on SQL Server 2005< i dunno about 2000 WITH LIMIT AS( SELECT *,ROW_NUMBER() OVER(ORDER BY brandName) AS rowNum FROM brandTable) SELECT * FROM LIMIT WHERE 0<rowNum AND rowNum<=3; understand the logic first of all the WITH can be used to categorize the area from where you get the records it likes FROM  tableName and  in here SELECT *,ROW_NUMBER() OVER(ORDER BY brandName) AS rowNum SELECT returns both record and the row number, but remember in this time u sort out the table by brandName in Ascending order. cause Ascending is the default. it can be even Descending. ORDER BY just does sort out the output records and OVER can be used to manipulate that. ROW_NUMBER() function returns that sorted out records ROW NUMBER from 1 to < so SELECT * FROM LIMIT when u use this like that this would return two things first the records, the record number remember you use something called rowNum which is called an Alias and can be give to any statement to make it short AS rowNum obviously then you can manipulate the condition after WHERE. you can be even use BETWEEN 1 AND 2 then it would be like this WITH LIMIT AS( SELECT *,ROW_NUMBER() OVER(ORDER BY brandName) AS rowNum FROM brandTable) SELECT * FROM LIMIT WHERE rowNum BETWEEN 0 AND 3; you see it's easy.!!

  • Anonymous
    April 13, 2011
    You can use eg. SELECT TOP 5 ... to return only 5 results, if you must do that.

  • Anonymous
    June 26, 2011
    Try this link: www.arunraj.co.in/index.php You'll find 4 types of paging in SQL Server and a C# function which will allow you to convert any query to a paging query. You just have to pass the start and end values.

  • Anonymous
    November 07, 2013
    This is hilarious. Thanks for making it 'easy'