{"id":197237,"date":"2025-07-14T05:30:00","date_gmt":"2025-07-14T04:30:00","guid":{"rendered":"https:\/\/liora.io\/en\/?p=197237"},"modified":"2026-08-09T19:54:55","modified_gmt":"2026-08-09T18:54:55","slug":"sql-limit-and-offset","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/sql-limit-and-offset","title":{"rendered":"SQL LIMIT and OFFSET: Controlling Query Results"},"content":{"rendered":"\n<p><strong>In most contexts, a table can contain thousands or even millions of rows. If you run a query that returns all records, it can overload your database, slow down performance, and make debugging harder.<\/strong><\/p>\n\n\n<p><strong>To prevent unnecessary load and display only what you need, SQL provides tools to limit your query results ,  namely, the LIMIT and OFFSET clauses.<\/strong><\/p>\n\n\n<p class=\"is-style-h2\"><strong>Why Use LIMIT?<\/strong><\/p>\n\n\n<p>Here are common use cases where LIMIT is extremely useful:<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"situation-1-data-preview\">\u2705 Situation 1: Data Preview<\/h3>\n\n\n<p>When you are just getting started and want to take a quick look at a table\u2019s content, there is no need to load the entire dataset. Use LIMIT to display the first few rows for a data preview.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"situation-2-top-n-analysis-with-order-by\">\u2705 Situation 2: Top-N Analysis with ORDER BY<\/h3>\n\n\n<p>If you want to know the top 10 bestsellers, most recent records, or highest-rated products, you can combine ORDER BY and LIMIT.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"situation-3-data-sampling\">\u2705 Situation 3: Data Sampling<\/h3>\n\n\n<p>If you are conducting exploratory analysis or testing a model, you may want to sample a subset of rows.<\/p>\n\n\n<p>\u27a1\ufe0f This gives you a manageable chunk of data to work with, especially from large tables.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"situation-4-data-pagination\">\u2705 Situation 4: Data Pagination<\/h3>\n\n\n<p>When working with dashboards, APIs, or web applications, you often need to paginate results ,  e.g., show 10 results per page.<\/p>\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Un utilisateur consultant un tableau de clients sur un \u00e9cran d'ordinateur, illustrant l'utilisation de SQL LIMIT et OFFSET pour contr\u00f4ler les r\u00e9sultats de requ\u00eates.\" decoding=\"async\" height=\"1024\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2025\/07\/sql-limit-offset-controlling-query-results-datascientest-1.webp\" style=\"width:100%;height:auto\" width=\"1536\"\/><\/figure>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-analyst\">Get to grips with SQL<\/a><\/div>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"example-table\">Example Table<\/h2>\n\n\n<p>Let\u2019s say you are working with this customer table:<\/p>\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<thead>\n<tr>\n<th>customer_id<\/th>\n<th>first_name<\/th>\n<th>last_name<\/th>\n<th>email<\/th>\n<th>email_verified<\/th>\n<th>phone_number<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>101<\/td>\n<td>John<\/td>\n<td>Doe<\/td>\n<td>john@email.com<\/td>\n<td>Y<\/td>\n<td>0912&#8230;<\/td>\n<\/tr>\n<tr>\n<td>102<\/td>\n<td>Jane<\/td>\n<td>Smith<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>0933&#8230;<\/td>\n<\/tr>\n<tr>\n<td>103<\/td>\n<td>Mike<\/td>\n<td>Lee<\/td>\n<td>&#8221;<\/td>\n<td>N<\/td>\n<td>\u00a0<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/figure>\n\n\n<pre class=\"wp-block-code\"><code>SELECT *\u00a0\nFROM customers\nORDER BY customer_id\nLIMIT 2;<\/code><\/pre>\n\n\n<p><strong>\u27a1\ufe0f This would return rows 1 and 2.<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code>SELECT *\u00a0\nFROM customers\nORDER BY customer_id\nLIMIT 2 OFFSET 1;<\/code><\/pre>\n\n\n<p><strong>\u27a1\ufe0f This would return rows 2 and 3, effectively paginating the results.<\/strong><\/p>\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Un analyste de donn\u00e9es examinant une table dans une interface de requ\u00eate SQL, illustrant l'utilisation des commandes LIMIT et OFFSET pour le contr\u00f4le des r\u00e9sultats de requ\u00eate.\" decoding=\"async\" height=\"1024\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2025\/07\/sql-limit-offset-controlling-query-results-datascientest-2.webp\" style=\"width:100%;height:auto\" width=\"1536\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-is-limit-different-from-top-and-fetch-first\">How is LIMIT Different from TOP and FETCH FIRST?<\/h2>\n\n\n<p>Different SQL dialects use different syntax for limiting rows:<\/p>\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<thead>\n<tr>\n<th>Syntax<\/th>\n<th>SQL Flavor<\/th>\n<th>Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>LIMIT \/ OFFSET<\/strong><\/td>\n<td>MySQL, PostgreSQL, SQLite<\/td>\n<td>\n          SELECT *<br\/>\n          FROM table<br\/>\n          LIMIT 10 OFFSET 20;\n        <\/td>\n<\/tr>\n<tr>\n<td><strong>TOP<\/strong><\/td>\n<td>SQL Server<\/td>\n<td>\n          SELECT TOP 10 *<br\/>\n          FROM table;\n        <\/td>\n<\/tr>\n<tr>\n<td><strong>FETCH FIRST<\/strong><\/td>\n<td>ANSI SQL \/ Oracle \/ newer SQL Server versions<\/td>\n<td>\n          SELECT *<br\/>\n          FROM table<br\/>\n          ORDER BY id<br\/>\n          FETCH FIRST 10 ROWS ONLY;\n        <\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/figure>\n\n\n<p>\u26a0\ufe0f Key Differences:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>LIMIT is widely used in MySQL, PostgreSQL, and SQLite.<\/li>\n<li>TOP works only in SQL Server (and can&#8217;t be used with OFFSET).<\/li>\n<li>FETCH FIRST &#8230; ROWS ONLY is ANSI-standard, more verbose, but also more portable in newer systems (like Oracle or DB2).<\/li>\n<\/ul>\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">\u2705 Conclusion<\/h2>\n\n\n<p>Whether you are previewing data, performing top-N analysis, sampling rows, or implementing pagination, knowing how to control the number of results returned by your SQL queries is essential. The LIMIT clause (often used with OFFSET) gives you a simple yet powerful way to manage result sets efficiently.<\/p>\n\n\n<p>Different SQL dialects may use LIMIT, TOP, or FETCH FIRST, but the goal remains the same: optimize your queries, reduce load on your database, and retrieve only what you need.<\/p>\n\n\n<p>By mastering these tools, you not only improve performance but also gain greater flexibility in exploring and presenting your data.<\/p>\n\n\n<figure class=\"wp-block-image size-full\" style=\"margin-top:32px;margin-bottom:32px\"><img alt=\"Illustration for \u2705 Conclusion\" decoding=\"async\" height=\"1024\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2025\/07\/sql-limit-offset-controlling-query-results-datascientest-3.webp\" style=\"width:100%;height:auto\" width=\"1536\"\/><\/figure>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-engineer\">Start your training with Liora<\/a><\/div>\n<\/div>\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [\n    {\n      \"@type\": \"Question\",\n      \"name\": \"Example Table\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"Let\u2019s say you are working with this customer table:\\n\\ncustomer_id first_name last_name email email_verified phone_number  \\n101 John Doe [email protected] Y 0912\u2026  \\n102 Jane Smith NULL NULL 0933\u2026  \\n103 Mike Lee \u201d N   \\n\\n    SELECT *  \\n    FROM customers\\n    ORDER BY customer_id\\n    LIMIT 2;\\n\\n\u27a1\ufe0f This would return rows 1 and 2.\\n\\n    SELECT *  \\n    FROM customers\\n    ORDER BY customer_id\\n    LIMIT 2 OFFSET 1;\\n\\n\u27a1\ufe0f This would return rows 2 and 3, effectively paginating the results.\" \n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How is LIMIT Different from TOP and FETCH FIRST?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"Different SQL dialects use different syntax for limiting rows:\\n\\nSyntax SQL Flavor Example  \\nLIMIT \/ OFFSET MySQL, PostgreSQL, SQLite SELECT *  \\nFROM table  \\nLIMIT 10 OFFSET 20;   \\nTOP SQL Server SELECT TOP 10 *  \\nFROM table;   \\nFETCH FIRST ANSI SQL \/ Oracle \/ newer SQL Server versions SELECT *  \\nFROM table  \\nORDER BY id  \\nFETCH FIRST 10 ROWS ONLY;   \\n\\n\u26a0\ufe0f Key Differences:\\n\\n  * LIMIT is widely used in MySQL, PostgreSQL, and SQLite.\\n  * TOP works only in SQL Server (and can\u2019t be used with OFFSET).\\n  * FETCH FIRST \u2026 ROWS ONLY is ANSI-standard, more verbose, but also more portable in newer systems (like Oracle or DB2).\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"\u2714 Conclusion\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"Whether you are previewing data, performing top-N analysis, sampling rows, or implementing pagination, knowing how to control the number of results returned by your SQL queries is essential. The LIMIT clause \u2014 often used with OFFSET \u2014 gives you a simple yet powerful way to manage result sets efficiently.\\n\\nDifferent SQL dialects may use LIMIT, TOP, or FETCH FIRST, but the goal remains the same: optimize your queries, reduce load on your database, and retrieve only what you need.\\n\\nBy mastering these tools, you not only improve performance but also gain greater flexibility in exploring and presenting your data.\"\n      }\n    }\n  ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>In most contexts, a table can contain thousands or even millions of rows. If you run a query that returns all records, it can overload your database, slow down performance, and make debugging harder. To prevent unnecessary load and display only what you need, SQL provides tools to limit your query results , namely, the [&hellip;]<\/p>\n","protected":false},"author":99,"featured_media":197555,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2434],"class_list":["post-197237","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-dev"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/197237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/99"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=197237"}],"version-history":[{"count":5,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/197237\/revisions"}],"predecessor-version":[{"id":211202,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/197237\/revisions\/211202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/197555"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=197237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=197237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}