{"id":459,"date":"2024-11-28T15:43:27","date_gmt":"2024-11-28T08:43:27","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=459"},"modified":"2025-01-22T10:05:52","modified_gmt":"2025-01-22T03:05:52","slug":"postgresql-fetch","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/","title":{"rendered":"PostgreSQL FETCH"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the PostgreSQL <code>FETCH<\/code> clause to retrieve a subset of rows from a query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-postgresql-fetch-clause'>Introduction to PostgreSQL FETCH clause <a href=\"#introduction-to-postgresql-fetch-clause\" class=\"anchor\" id=\"introduction-to-postgresql-fetch-clause\" title=\"Anchor for Introduction to PostgreSQL FETCH clause\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, the <code>OFFSET<\/code>clause works like the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-limit\/\">LIMIT<\/a><\/code>clause. The <code>FETCH<\/code> clause allows you to limit the number of rows to return from a query.<\/p>\n\n\n\n<p>The <code>LIMIT<\/code> clause is not a part of the SQL standard. However, the <code>FETCH<\/code> clause is a part of the SQL:2008 standard. If you want your application to support other databases in the future, use <code>FETCH<\/code> instead of <code>LIMIT<\/code> because other database vendors will likely support it.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>OFFSET FETCH<\/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> column1, column2\n<span class=\"hljs-keyword\">FROM<\/span> <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> sort_expression\n<span class=\"hljs-keyword\">OFFSET<\/span> skip_count\n<span class=\"hljs-keyword\">FETCH<\/span> { FIRST | NEXT } ] &#91;<span class=\"hljs-built_in\">row_count<\/span>] {<span class=\"hljs-keyword\">ROW<\/span> | <span class=\"hljs-keyword\">ROWS<\/span> } <span class=\"hljs-keyword\">ONLY<\/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<p>First, specify the name of the table you want to retrieve data in the <code>FROM<\/code> clause.<\/p>\n\n\n\n<p>Second, restrict which columns from the rows to include in the final result set in the <code>SELECT<\/code> clause.<\/p>\n\n\n\n<p>Third, use the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-order-by\/\">ORDER BY<\/a><\/code> clause to sort the rows by values in one or more columns.<\/p>\n\n\n\n<p>Fourth, provide the number of rows to skip in the <code>OFFSET<\/code> clause before the <code>FETCH<\/code> clause returns a subset of rows:<\/p>\n\n\n<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\">OFFSET<\/span> skip_count<\/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>\n\n\n<p>The <code>skip_count<\/code> determines the number of rows to skip. It can be a zero or a positive integer. If <code>skip_count<\/code> is zero, the query will not skip any rows.<\/p>\n\n\n\n<p>The <code>OFFSET<\/code> clause is optional. If you omit it, the query will also not skip any rows.<\/p>\n\n\n\n<p>Fifth, set the number of rows to return (<code>row_count<\/code>) in the <code>FETCH<\/code> clause.<\/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\">FETCH<\/span> { FIRST | NEXT } ] &#91;<span class=\"hljs-built_in\">row_count<\/span>] {<span class=\"hljs-keyword\">ROW<\/span> | <span class=\"hljs-keyword\">ROWS<\/span> } <span class=\"hljs-keyword\">ONLY<\/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>The <code>row_count<\/code> defaults to 1, meaning that if you omit it, the <code>FETCH<\/code> will return one row from the query.<\/p>\n\n\n\n<p>You can use <code>FIRST<\/code> and <code>NEXT<\/code>, <code>ROW<\/code>, and <code>ROWS<\/code> interchangeably because they are synonyms.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" 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\">FETCH FIRST<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">ROW<\/span> <span class=\"hljs-keyword\">ONLY<\/span>;\n<span class=\"hljs-keyword\">FETCH FIRST<\/span> <span class=\"hljs-keyword\">ROW<\/span> <span class=\"hljs-keyword\">ONLY<\/span>;\n<span class=\"hljs-keyword\">FETCH FIRST<\/span> <span class=\"hljs-number\">10<\/span> <span class=\"hljs-keyword\">ROWS<\/span> <span class=\"hljs-keyword\">ONLY<\/span>;\n<span class=\"hljs-keyword\">FETCH NEXT<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">ROW<\/span> <span class=\"hljs-keyword\">ONLY<\/span>;\n<span class=\"hljs-keyword\">FETCH NEXT<\/span> <span class=\"hljs-keyword\">ROW<\/span> <span class=\"hljs-keyword\">ONLY<\/span>;\n<span class=\"hljs-keyword\">FETCH NEXT<\/span> <span class=\"hljs-number\">10<\/span> <span class=\"hljs-keyword\">ROWS<\/span> <span class=\"hljs-keyword\">ONLY<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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<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 use the <code>products<\/code> table created in the <code>LIMIT<\/code> tutorial to practice with the <code>FETCH<\/code> clause.<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Here&#8217;s the SQL script to create the products table and insert some rows.<\/summary><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\">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-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><\/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='fetching-the-first-row'>Fetching the first row <a href=\"#fetching-the-first-row\" class=\"anchor\" id=\"fetching-the-first-row\" title=\"Anchor for Fetching the first row\">#<\/a><\/h2>\n\n\n\n<p>The following statement uses the <code>FETCH<\/code> clause to retrieve the most expensive product from the <code>products<\/code> 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\">FETCH FIRST<\/span> <span class=\"hljs-keyword\">ROW<\/span> <span class=\"hljs-keyword\">ONLY<\/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=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gcHJvZHVjdHMgT1JERVIgQlkgcHJpY2UgREVTQyBGRVRDSCBGSVJTVCBST1cgT05MWTs\" 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=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">      <span class=\"hljs-type\">name<\/span>       |  price\n<span class=\"hljs-comment\">-----------------+---------<\/span>\n Galaxy Z Fold <span class=\"hljs-number\">6<\/span> | <span class=\"hljs-number\">2019.99<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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<h2 class=\"wp-block-heading\" id='fetching-some-rows'>Fetching some rows <a href=\"#fetching-some-rows\" class=\"anchor\" id=\"fetching-some-rows\" title=\"Anchor for Fetching some rows\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>FETCH<\/code> clause to retrieve the top three most expensive products from the <code>products<\/code> table:<\/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  <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\">FETCH FIRST<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-keyword\">ROWS<\/span> <span class=\"hljs-keyword\">ONLY<\/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=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gcHJvZHVjdHMgT1JERVIgQlkgcHJpY2UgREVTQyBGRVRDSCBGSVJTVCAzIFJPV1MgT05MWTs\" 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=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">       <span class=\"hljs-type\">name<\/span>        |  price\n<span class=\"hljs-comment\">-------------------+---------<\/span>\n Galaxy Z Fold <span class=\"hljs-number\">6<\/span>   | <span class=\"hljs-number\">2019.99<\/span>\n iPhone <span class=\"hljs-number\">16<\/span> Pro Max | <span class=\"hljs-number\">1649.00<\/span>\n OnePlus <span class=\"hljs-keyword\">Open<\/span>      | <span class=\"hljs-number\">1514.99<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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<h2 class=\"wp-block-heading\" id='skipping-some-rows-before-fetching'>Skipping some rows before fetching <a href=\"#skipping-some-rows-before-fetching\" class=\"anchor\" id=\"skipping-some-rows-before-fetching\" title=\"Anchor for Skipping some rows before fetching\">#<\/a><\/h2>\n\n\n\n<p>The following statement uses the <code>OFFSET<\/code> and <code>FETCH<\/code> clauses to get the second most expensive products from the <code>products<\/code> table:<\/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  <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\">OFFSET<\/span> <span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-keyword\">FETCH NEXT<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">ROW<\/span> <span class=\"hljs-keyword\">ONLY<\/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=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gcHJvZHVjdHMgT1JERVIgQlkgcHJpY2UgREVTQyBPRkZTRVQgMSBGRVRDSCBORVhUIDEgUk9XIE9OTFk7\" 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=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">       <span class=\"hljs-type\">name<\/span>        |  price\n<span class=\"hljs-comment\">-------------------+---------<\/span>\n iPhone <span class=\"hljs-number\">16<\/span> Pro Max | <span class=\"hljs-number\">1649.00<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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<h2 class=\"wp-block-heading\" id='using-postgresql-fetch-clause-for-pagination'>Using PostgreSQL FETCH clause for pagination <a href=\"#using-postgresql-fetch-clause-for-pagination\" class=\"anchor\" id=\"using-postgresql-fetch-clause-for-pagination\" title=\"Anchor for Using PostgreSQL FETCH clause for pagination\">#<\/a><\/h2>\n\n\n\n<p>The <code>FETCH<\/code> clause can be helpful for pagination:<\/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\">OFFSET<\/span> record_per_page * (page_no - <span class=\"hljs-number\">1<\/span>)\n<span class=\"hljs-keyword\">FETCH NEXT<\/span> record_per_page <span class=\"hljs-keyword\">ROWS<\/span> <span class=\"hljs-keyword\">ONLY<\/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>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>record_per_page<\/code> is the number of records per page.<\/li>\n\n\n\n<li><code>page_no<\/code> is the page number like 1, 2, and 3.<\/li>\n<\/ul>\n\n\n\n<p>Suppose you want to display three products (<code>record_per_page<\/code>) per page and want to retrieve products for page 2 (<code>page_no<\/code>); you can use the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" 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\">OFFSET<\/span> <span class=\"hljs-number\">3<\/span> * (<span class=\"hljs-number\">2<\/span> - <span class=\"hljs-number\">1<\/span>)\n<span class=\"hljs-keyword\">FETCH NEXT<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-keyword\">ROW<\/span> <span class=\"hljs-keyword\">ONLY<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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=U0VMRUNUIG5hbWUsIHByaWNlIEZST00gaW52ZW50b3JpZXMgT1JERVIgQlkgcHJpY2UgREVTQyBPRkZTRVQgMyAqICgyIC0gMSkgRkVUQ0ggTkVYVCAzIFJPVyBPTkxZOw\" 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-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">       <span class=\"hljs-type\">name<\/span>       |  price\n<span class=\"hljs-comment\">------------------+---------<\/span>\n Galaxy S24 Ultra | <span class=\"hljs-number\">1299.99<\/span>\n Galaxy S24 FE    |  <span class=\"hljs-number\">949.99<\/span>\n iPhone <span class=\"hljs-number\">16<\/span>        |  <span class=\"hljs-number\">829.00<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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<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>FETCH<\/code> to return a subset of rows from a query.<\/li>\n\n\n\n<li>Use the <code>OFFSET FETCH<\/code> to skip some rows before returning a subset of rows.<\/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=fetch\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\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=\"459\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL FETCH\"\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=\"459\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL FETCH\"\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>Summary: in this tutorial, you will learn how to use the PostgreSQL FETCH clause to retrieve a subset of rows from a query. Introduction to PostgreSQL FETCH clause # In PostgreSQL, the OFFSETclause works like the LIMITclause. The FETCH clause allows you to limit the number of rows to return from a query. The LIMIT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":20,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-459","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 FETCH<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use the PostgreSQL FETCH clause to retrieve a subset of rows from a query.\" \/>\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-fetch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL FETCH\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use the PostgreSQL FETCH clause to retrieve a subset of rows from a query.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-22T03:05:52+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=\"3 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-fetch\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-fetch\\\/\",\"name\":\"PostgreSQL FETCH\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-11-28T08:43:27+00:00\",\"dateModified\":\"2025-01-22T03:05:52+00:00\",\"description\":\"In this tutorial, you will learn how to use the PostgreSQL FETCH clause to retrieve a subset of rows from a query.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-fetch\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-fetch\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-fetch\\\/#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 FETCH\"}]},{\"@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 FETCH","description":"In this tutorial, you will learn how to use the PostgreSQL FETCH clause to retrieve a subset of rows from a query.","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-fetch\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL FETCH","og_description":"In this tutorial, you will learn how to use the PostgreSQL FETCH clause to retrieve a subset of rows from a query.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-22T03:05:52+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/","name":"PostgreSQL FETCH","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-11-28T08:43:27+00:00","dateModified":"2025-01-22T03:05:52+00:00","description":"In this tutorial, you will learn how to use the PostgreSQL FETCH clause to retrieve a subset of rows from a query.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-fetch\/#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 FETCH"}]},{"@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\/459","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=459"}],"version-history":[{"count":8,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/459\/revisions"}],"predecessor-version":[{"id":1785,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/459\/revisions\/1785"}],"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=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}