{"id":449,"date":"2024-11-28T11:09:28","date_gmt":"2024-11-28T04:09:28","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=449"},"modified":"2025-01-02T08:58:26","modified_gmt":"2025-01-02T01:58:26","slug":"postgresql-limit","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/","title":{"rendered":"PostgreSQL LIMIT"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the PostgreSQL <code>LIMIT<\/code> clause to retrieve only a subset of rows.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-postgresql-limit-clause'>Getting started with PostgreSQL LIMIT clause <a href=\"#getting-started-with-postgresql-limit-clause\" class=\"anchor\" id=\"getting-started-with-postgresql-limit-clause\" title=\"Anchor for Getting started with PostgreSQL LIMIT clause\">#<\/a><\/h2>\n\n\n\n<p>The <code>LIMIT<\/code> clause allows you to retrieve only a subset of rows from a <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-select\/\">SELECT<\/a><\/code> statement. It can be handy when working with a large result set.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>LIMIT<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  column1,\n  column2\n<span class=\"hljs-keyword\">FROM<\/span>\n  <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  sort_expression\n<span class=\"hljs-keyword\">LIMIT<\/span>\n  <span class=\"hljs-built_in\">row_count<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the table name where you want to retrieve data in the <code>FROM<\/code> clause.<\/li>\n\n\n\n<li>Second, provide a list of columns to return in the <code>SELECT<\/code> clause.<\/li>\n\n\n\n<li>Third, sort the rows using the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/\">ORDER BY<\/a><\/code> clause.<\/li>\n\n\n\n<li>Finally, specify the number of rows (<code>row_count<\/code>) to return in the <code>LIMIT<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>PostgreSQL evaluates the clauses in the following order:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>FROM<\/code><\/li>\n\n\n\n<li><code>SELECT<\/code><\/li>\n\n\n\n<li><code>ORDER BY<\/code><\/li>\n\n\n\n<li><code>LIMIT<\/code><\/li>\n<\/ol>\n\n\n\n<p>It&#8217;s crucial that you should always use the <code>LIMIT<\/code> clause with the <code>ORDER BY<\/code> clause to get the expected result set.<\/p>\n\n\n\n<p>If you don&#8217;t use the <code>LIMIT<\/code> clause with <code>ORDER BY<\/code> clause, the <code>SELECT<\/code> statement returns rows in an unspecified order, and the <code>LIMIT<\/code> clause will pick the top rows from the unordered rows, potentially leading to unexpected results.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='setting-up-a-sample-table'>Setting up a sample table <a href=\"#setting-up-a-sample-table\" class=\"anchor\" id=\"setting-up-a-sample-table\" title=\"Anchor for Setting up a sample table\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a new table<\/a> called <code>products<\/code>, which will store product information including their names and prices. The <code>products<\/code> table will have some sample data to practice with the <code>LIMIT<\/code> clause:<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>SQL script to create and populate data for the products table:<\/summary><pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n  product_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  price <span class=\"hljs-type\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>)\n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  products (<span class=\"hljs-type\">name<\/span>, price)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'iPhone 16 Pro Max'<\/span>, <span class=\"hljs-number\">1649.00<\/span>),\n  (<span class=\"hljs-string\">'iPhone 16'<\/span>, <span class=\"hljs-number\">829.00<\/span>),\n  (<span class=\"hljs-string\">'Galaxy S24 Ultra'<\/span>, <span class=\"hljs-number\">1299.99<\/span>),\n  (<span class=\"hljs-string\">'Galaxy S24 FE'<\/span>, <span class=\"hljs-number\">949.99<\/span>),\n  (<span class=\"hljs-string\">'Pixel 9 Pro'<\/span>, <span class=\"hljs-number\">799.00<\/span>),\n  (<span class=\"hljs-string\">'Pixel 8a'<\/span>, <span class=\"hljs-number\">399.00<\/span>),\n  (<span class=\"hljs-string\">'OnePlus 12'<\/span>, <span class=\"hljs-number\">745.00<\/span>),\n  (<span class=\"hljs-string\">'OnePlus Open'<\/span>, <span class=\"hljs-number\">1514.99<\/span>),\n  (<span class=\"hljs-string\">'Galaxy Z Fold 6'<\/span>, <span class=\"hljs-number\">2019.99<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><\/details>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>product_id<\/th><th>name<\/th><th>price<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>iPhone 16 Pro Max<\/td><td>1649.00<\/td><\/tr><tr><td>2<\/td><td>iPhone 16<\/td><td>829.00<\/td><\/tr><tr><td>3<\/td><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><\/tr><tr><td>4<\/td><td>Galaxy S24 FE<\/td><td>949.99<\/td><\/tr><tr><td>5<\/td><td>Pixel 9 Pro<\/td><td>799.00<\/td><\/tr><tr><td>6<\/td><td>Pixel 8a<\/td><td>399.00<\/td><\/tr><tr><td>7<\/td><td>OnePlus 12<\/td><td>745.00<\/td><\/tr><tr><td>8<\/td><td>OnePlus Open<\/td><td>1514.99<\/td><\/tr><tr><td>9<\/td><td>Galaxy Z Fold 6<\/td><td>2019.99<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-limit-clause-example'>PostgreSQL LIMIT clause example <a href=\"#postgresql-limit-clause-example\" class=\"anchor\" id=\"postgresql-limit-clause-example\" title=\"Anchor for PostgreSQL LIMIT clause example\">#<\/a><\/h2>\n\n\n\n<p>The following statement uses the <code>LIMIT<\/code> clause to return the top five most expensive products from the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price <span class=\"hljs-keyword\">DESC<\/span>\n<span class=\"hljs-keyword\">LIMIT<\/span> <span class=\"hljs-number\">5<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=limit&amp;q=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gcHJvZHVjdHMgT1JERVIgQlkgcHJpY2UgREVTQyBMSU1JVCA1Ow\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name        |  price\n-------------------+---------\n Galaxy Z Fold 6   | 2019.99\n iPhone 16 Pro Max | 1649.00\n OnePlus Open      | 1514.99\n Galaxy S24 Ultra  | 1299.99\n Galaxy S24 FE     |  949.99<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, the <code>ORDER BY<\/code> clause sorts the rows returned by the <code>FROM<\/code> clause by the price from high to low:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>product_id<\/th><th>name<\/th><th>price<\/th><\/tr><tr><td>9<\/td><td>Galaxy Z Fold 6<\/td><td>2019.99<\/td><\/tr><tr><td>1<\/td><td>iPhone 16 Pro Max<\/td><td>1649.00<\/td><\/tr><tr><td>8<\/td><td>OnePlus Open<\/td><td>1514.99<\/td><\/tr><tr><td>3<\/td><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><\/tr><tr><td>4<\/td><td>Galaxy S24 FE<\/td><td>949.99<\/td><\/tr><tr><td>2<\/td><td>iPhone 16<\/td><td>829.00<\/td><\/tr><tr><td>5<\/td><td>Pixel 9 Pro<\/td><td>799.00<\/td><\/tr><tr><td>7<\/td><td>OnePlus 12<\/td><td>745.00<\/td><\/tr><tr><td>6<\/td><td>Pixel 8a<\/td><td>399.00<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Second, the <code>SELECT<\/code> clause retrieves data from the <code>name<\/code> and <code>price<\/code> columns:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>name<\/th><th>price<\/th><\/tr><tr><td>Galaxy Z Fold 6<\/td><td>2019.99<\/td><\/tr><tr><td>iPhone 16 Pro Max<\/td><td>1649.00<\/td><\/tr><tr><td>OnePlus Open<\/td><td>1514.99<\/td><\/tr><tr><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><\/tr><tr><td>Galaxy S24 FE<\/td><td>949.99<\/td><\/tr><tr><td>iPhone 16<\/td><td>829.00<\/td><\/tr><tr><td>Pixel 9 Pro<\/td><td>799.00<\/td><\/tr><tr><td>OnePlus 12<\/td><td>745.00<\/td><\/tr><tr><td>Pixel 8a<\/td><td>399.00<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Third, the <code>LIMIT<\/code> clause picks the top five rows and returns them:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>name<\/th><th>price<\/th><\/tr><tr><td>Galaxy Z Fold 6<\/td><td>2019.99<\/td><\/tr><tr><td>iPhone 16 Pro Max<\/td><td>1649.00<\/td><\/tr><tr><td>OnePlus Open<\/td><td>1514.99<\/td><\/tr><tr><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><\/tr><tr><td>Galaxy S24 FE<\/td><td>949.99<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-limit-offset-clause'>PostgreSQL LIMIT OFFSET clause <a href=\"#postgresql-limit-offset-clause\" class=\"anchor\" id=\"postgresql-limit-offset-clause\" title=\"Anchor for PostgreSQL LIMIT OFFSET clause\">#<\/a><\/h2>\n\n\n\n<p>To skip some rows before returning a subset of rows, you can use the <code>OFFSET<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  column1,\n  column2\n<span class=\"hljs-keyword\">FROM<\/span>\n  <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  sort_expression\n<span class=\"hljs-keyword\">LIMIT<\/span>\n  <span class=\"hljs-built_in\">row_count<\/span>\n<span class=\"hljs-keyword\">OFFSET<\/span>\n  skip_count;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The statement works like the one without the <code>OFFSET<\/code> clause if the <code>skip_count<\/code> is zero or null.<\/p>\n\n\n\n<p>The <code>OFFSET<\/code> clause skips several rows (<code>skip_count<\/code>) before the <code>LIMIT<\/code> clause returns a subset of rows.<\/p>\n\n\n\n<p>The following example uses the <code>LIMIT OFFSET<\/code> to skip two rows before returning the next five rows from the products table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-type\">name<\/span>,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price <span class=\"hljs-keyword\">DESC<\/span>\n<span class=\"hljs-keyword\">LIMIT<\/span> <span class=\"hljs-number\">5<\/span> <span class=\"hljs-keyword\">OFFSET<\/span> <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=limit&amp;q=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gcHJvZHVjdHMgT1JERVIgQlkgcHJpY2UgREVTQyBMSU1JVCA1IE9GRlNFVCAyOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">       name       |  price\n------------------+---------\n OnePlus Open     | 1514.99\n Galaxy S24 Ultra | 1299.99\n Galaxy S24 FE    |  949.99\n iPhone 16        |  829.00\n Pixel 9 Pro      |  799.00<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, the <code>FROM<\/code> clause returns rows from the <code>products<\/code> table.<\/p>\n\n\n\n<p>Second, the <code>SELECT<\/code> clause retrieves data from the <code>name<\/code> and <code>price<\/code> columns.<\/p>\n\n\n\n<p>Third, the <code>ORDER BY<\/code> clause sorts the rows returned by the <code>FROM<\/code> clause by the price from high to low.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>price<\/th><\/tr><\/thead><tbody><tr><td>Galaxy Z Fold 6<\/td><td>2019.99<\/td><\/tr><tr><td>iPhone 16 Pro Max<\/td><td>1649.00<\/td><\/tr><tr><td>OnePlus Open<\/td><td>1514.99<\/td><\/tr><tr><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><\/tr><tr><td>Galaxy S24 FE<\/td><td>949.99<\/td><\/tr><tr><td>iPhone 16<\/td><td>829.00<\/td><\/tr><tr><td>Pixel 9 Pro<\/td><td>799.00<\/td><\/tr><tr><td>OnePlus 12<\/td><td>745.00<\/td><\/tr><tr><td>Pixel 8a<\/td><td>399.00<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Finally, the <code>OFFSET<\/code> clause skips two rows and the <code>LIMIT<\/code> clause returns the next five rows:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>LIMIT\/OFFSET<\/th><th>product_id<\/th><th>name<\/th><th>price<\/th><\/tr><\/thead><tbody><tr><td>Skipped<\/td><td>9<\/td><td>Galaxy Z Fold 6<\/td><td>2019.99<\/td><\/tr><tr><td>Skipped<\/td><td>1<\/td><td>iPhone 16 Pro Max<\/td><td>1649.00<\/td><\/tr><tr><td>Included<\/td><td>8<\/td><td>OnePlus Open<\/td><td>1514.99<\/td><\/tr><tr><td>Included<\/td><td>3<\/td><td>Galaxy S24 Ultra<\/td><td>1299.99<\/td><\/tr><tr><td>Included<\/td><td>4<\/td><td>Galaxy S24 FE<\/td><td>949.99<\/td><\/tr><tr><td>Included<\/td><td>2<\/td><td>iPhone 16<\/td><td>829.00<\/td><\/tr><tr><td>Included<\/td><td>5<\/td><td>Pixel 9 Pro<\/td><td>799.00<\/td><\/tr><tr><td><\/td><td>7<\/td><td>OnePlus 12<\/td><td>745.00<\/td><\/tr><tr><td><\/td><td>6<\/td><td>Pixel 8a<\/td><td>399.00<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-postgresql-limit-offset-for-pagination'>Using PostgreSQL LIMIT OFFSET for pagination <a href=\"#using-postgresql-limit-offset-for-pagination\" class=\"anchor\" id=\"using-postgresql-limit-offset-for-pagination\" title=\"Anchor for Using PostgreSQL LIMIT OFFSET for pagination\">#<\/a><\/h2>\n\n\n\n<p>The technique that break down a result set into smaller chunks is known as <em>pagination<\/em>. <\/p>\n\n\n\n<p>Suppose you want to display the product list on pages; each page contains a limited number of products.<\/p>\n\n\n\n<p>The following statement returns the first page with the first three products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_id,\n  <span class=\"hljs-type\">name<\/span>,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price <span class=\"hljs-keyword\">DESC<\/span>\n<span class=\"hljs-keyword\">LIMIT<\/span> <span class=\"hljs-number\">3<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=limit&amp;q=U0VMRUNUIHByb2R1Y3RfaWQsIG5hbWUsIHByaWNlIEZST00gcHJvZHVjdHMgT1JERVIgQlkgcHJpY2UgREVTQyBMSU1JVCAzOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> product_id |       name        |  price\n------------+-------------------+---------\n          9 | Galaxy Z Fold 6   | 2019.99\n          1 | iPhone 16 Pro Max | 1649.00\n          8 | OnePlus Open      | 1514.99<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To display the next three products on page two, we can use the <code>OFFSET<\/code> to skip the first three products and the <code>LIMIT<\/code> clause to return the following three products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_id,\n  <span class=\"hljs-type\">name<\/span>,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price <span class=\"hljs-keyword\">DESC<\/span>\n<span class=\"hljs-keyword\">LIMIT<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-keyword\">OFFSET<\/span> <span class=\"hljs-number\">3<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=limit&amp;q=U0VMRUNUIHByb2R1Y3RfaWQsIG5hbWUsIHByaWNlIEZST00gcHJvZHVjdHMgT1JERVIgQlkgcHJpY2UgREVTQyBMSU1JVCAzIE9GRlNFVCAzOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> product_id |       name       |  price\n------------+------------------+---------\n          3 | Galaxy S24 Ultra | 1299.99\n          4 | Galaxy S24 FE    |  949.99\n          2 | iPhone 16        |  829.00<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, retrieve the products for the last page using the <code>LIMIT<\/code> and <code>OFFSET<\/code> clauses:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_id,\n  <span class=\"hljs-type\">name<\/span>,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  products\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  price <span class=\"hljs-keyword\">DESC<\/span>\n<span class=\"hljs-keyword\">LIMIT<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-keyword\">OFFSET<\/span> <span class=\"hljs-number\">3<\/span> * <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=limit&amp;q=U0VMRUNUIHByb2R1Y3RfaWQsIG5hbWUsIHByaWNlIEZST00gcHJvZHVjdHMgT1JERVIgQlkgcHJpY2UgREVTQyBMSU1JVCAzIE9GRlNFVCAzICogMjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> product_id |    name     | price\n------------+-------------+--------\n          5 | Pixel 9 Pro | 799.00\n          7 | OnePlus 12  | 745.00\n          6 | Pixel 8a    | 399.00<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the PostgreSQL <code>LIMIT<\/code> clause to control the number of rows a query returns.<\/li>\n\n\n\n<li>Use the <code>OFFSET<\/code> clause to skip some rows before returning a subset of rows.<\/li>\n\n\n\n<li>The <code>LIMIT<\/code> and <code>OFFSET<\/code> can be helpful for pagination.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=limit\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"449\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL LIMIT\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"449\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL LIMIT\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the PostgreSQL LIMIT clause to retrieve only a subset of rows.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":19,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-449","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL LIMIT: Returning a Subset of Rows<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use the PostgreSQL LIMIT clause to retrieve only a subset of rows.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL LIMIT: Returning a Subset of Rows\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use the PostgreSQL LIMIT clause to retrieve only a subset of rows.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T01:58:26+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-limit\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-limit\\\/\",\"name\":\"PostgreSQL LIMIT: Returning a Subset of Rows\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-11-28T04:09:28+00:00\",\"dateModified\":\"2025-01-02T01:58:26+00:00\",\"description\":\"In this tutorial, you will learn how to use the PostgreSQL LIMIT clause to retrieve only a subset of rows.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-limit\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-limit\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-limit\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL LIMIT\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL LIMIT: Returning a Subset of Rows","description":"In this tutorial, you will learn how to use the PostgreSQL LIMIT clause to retrieve only a subset of rows.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL LIMIT: Returning a Subset of Rows","og_description":"In this tutorial, you will learn how to use the PostgreSQL LIMIT clause to retrieve only a subset of rows.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T01:58:26+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/","name":"PostgreSQL LIMIT: Returning a Subset of Rows","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-11-28T04:09:28+00:00","dateModified":"2025-01-02T01:58:26+00:00","description":"In this tutorial, you will learn how to use the PostgreSQL LIMIT clause to retrieve only a subset of rows.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL LIMIT"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":7,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/449\/revisions"}],"predecessor-version":[{"id":1337,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/449\/revisions\/1337"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}