{"id":32770,"date":"2023-10-16T11:54:15","date_gmt":"2023-10-16T01:54:15","guid":{"rendered":"https:\/\/database.guide\/?p=32770"},"modified":"2023-10-16T11:54:16","modified_gmt":"2023-10-16T01:54:16","slug":"understanding-the-limit-clause-in-sql","status":"publish","type":"post","link":"https:\/\/database.guide\/understanding-the-limit-clause-in-sql\/","title":{"rendered":"Understanding the LIMIT Clause in SQL"},"content":{"rendered":"\n<p>Some of the major relational database management systems (<a href=\"https:\/\/database.guide\/what-is-an-rdbms\/\" data-type=\"post\" data-id=\"222\">RDBMS<\/a>s) have a <code>LIMIT<\/code> clause that enables us to reduce the number of rows returned by a query. <\/p>\n\n\n\n<p>The way it works is that we provide the number of rows we want to be returned by the query. We can also provide an offset to specify which row to start the count from.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we have a table with the following data:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n    ProductId,\n    ProductName\nFROM Products\nORDER BY ProductId;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------+---------------------------------+\n| ProductId | ProductName                     |\n+-----------+---------------------------------+\n|         1 | Left handed screwdriver         |\n|         2 | Right handed screwdriver        |\n|         3 | Long Weight (blue)              |\n|         4 | Long Weight (green)             |\n|         5 | Sledge Hammer                   |\n|         6 | Chainsaw                        |\n|         7 | Straw Dog Box                   |\n|         8 | Bottomless Coffee Mugs (4 Pack) |\n|         9 | Hammock                         |\n|        10 | Tea Pot                         |\n+-----------+---------------------------------+\n10 rows in set (0.11 sec)<\/pre>\n\n\n\n<p>This query returned ten rows.<\/p>\n\n\n\n<p>We can use the <code>LIMIT<\/code> clause to reduce the number of rows returned:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n    ProductId,\n    ProductName\nFROM Products\nORDER BY ProductId\nLIMIT 3;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------+--------------------------+\n| ProductId | ProductName              |\n+-----------+--------------------------+\n|         1 | Left handed screwdriver  |\n|         2 | Right handed screwdriver |\n|         3 | Long Weight (blue)       |\n+-----------+--------------------------+\n3 rows in set (0.00 sec)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using an Offset<\/h2>\n\n\n\n<p>Most (if not all) RDBMSs that provide a <code>LIMIT<\/code> clause allow us to skip rows at the beginning of the output. We do this by specifying an offset.<\/p>\n\n\n\n<p>This is probably explained best by an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n    ProductId,\n    ProductName\nFROM Products\nORDER BY ProductId\nLIMIT 3 OFFSET 2;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------+---------------------+\n| ProductId | ProductName         |\n+-----------+---------------------+\n|         3 | Long Weight (blue)  |\n|         4 | Long Weight (green) |\n|         5 | Sledge Hammer       |\n+-----------+---------------------+\n3 rows in set (0.00 sec)<\/pre>\n\n\n\n<p>In this case I specified an offset of <code>2<\/code>. This meant that the <code>LIMIT<\/code> conditions were applied after skipping two rows. <\/p>\n\n\n\n<p>So in this example we still got three rows. We just skipped two rows before counting the three rows. So we didn&#8217;t get products 1 through 3 like in the previous example. We got products 3 through 5 instead.<\/p>\n\n\n\n<p>Some RDBMSs (such as MySQL, MariaDB, and SQLite) accept a shortened form for including offset. In such cases we can rewrite the above query as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n    ProductId,\n    ProductName\nFROM Products\nORDER BY ProductId\nLIMIT 2, 3;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------+---------------------+\n| ProductId | ProductName         |\n+-----------+---------------------+\n|         3 | Long Weight (blue)  |\n|         4 | Long Weight (green) |\n|         5 | Sledge Hammer       |\n+-----------+---------------------+\n3 rows in set (0.06 sec)<\/pre>\n\n\n\n<p>So in this case the offset is the first value, and the row count limit is the second.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Alternatives to the <code>LIMIT<\/code> Clause<\/h2>\n\n\n\n<p>The <code>LIMIT<\/code> clause is not supported in all RDBMSs. However, some RDBMSs provide an alternative method for limiting the rows returned by a query, and for setting an offset.<\/p>\n\n\n\n<p>In SQL Server, we can include <a href=\"https:\/\/database.guide\/limit-the-rows-returned-in-a-sql-server-query-top-clause\/\" data-type=\"post\" data-id=\"5949\"><code>TOP(<em>n<\/em>)<\/code> in our <code>SELECT<\/code> list to return the top <em>n<\/em> rows<\/a>. SQL Server also supports a <code>FETCH<\/code> and <code>OFFSET<\/code> clause that can be used to limit the rows returned with an offset. We can <a href=\"https:\/\/database.guide\/pagination-in-sql-server-using-offset-fetch\/\" data-type=\"post\" data-id=\"5966\">use these two clauses to implement pagination functionality<\/a>.<\/p>\n\n\n\n<p>Oracle Database also implements a <a href=\"https:\/\/database.guide\/understanding-the-fetch-clause-in-oracle\/\" data-type=\"post\" data-id=\"32781\"><code>FETCH<\/code> row limiting clause<\/a> (which includes an <code>OFFSET<\/code> clause).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some of the major relational database management systems (RDBMSs) have a LIMIT clause that enables us to reduce the number of rows returned by a query. The way it works is that we provide the number of rows we want to be returned by the query. We can also provide an offset to specify which &#8230; <a title=\"Understanding the LIMIT Clause in SQL\" class=\"read-more\" href=\"https:\/\/database.guide\/understanding-the-limit-clause-in-sql\/\" aria-label=\"Read more about Understanding the LIMIT Clause in SQL\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[15,20],"class_list":["post-32770","post","type-post","status-publish","format-standard","hentry","category-sql","tag-create-query","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/32770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=32770"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/32770\/revisions"}],"predecessor-version":[{"id":32856,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/32770\/revisions\/32856"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=32770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=32770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=32770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}