{"id":3921,"date":"2025-06-06T20:29:18","date_gmt":"2025-06-06T20:29:18","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3921"},"modified":"2025-06-07T09:56:28","modified_gmt":"2025-06-07T09:56:28","slug":"sql-limit-top-fetch-first","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-limit-top-fetch-first\/","title":{"rendered":"SQL LIMIT \/ TOP \/ FETCH FIRST"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">These keywords let you <strong>limit how many rows<\/strong> your query returns. Usage differs slightly by database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Syntax by DBMS<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- \u2705 MySQL \/ PostgreSQL\nSELECT * FROM employees\nORDER BY salary DESC\nLIMIT 5;\n\n-- \u2705 SQL Server\nSELECT TOP 5 * FROM employees\nORDER BY salary DESC;\n\n-- \u2705 Oracle 12c+ \/ PostgreSQL (Standard SQL)\nSELECT * FROM employees\nORDER BY salary DESC\nFETCH FIRST 5 ROWS ONLY;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Pagination Example (MySQL\/PostgreSQL)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Get 10 rows starting from row 21 (page 3 if page size is 10)\nSELECT * FROM employees\nORDER BY name\nLIMIT 10 OFFSET 20;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>DBMS<\/th><th>Keyword Used<\/th><th>Notes<\/th><\/tr><\/thead><tbody><tr><td>MySQL<\/td><td><code>LIMIT<\/code><\/td><td>With optional <code>OFFSET<\/code> for paging<\/td><\/tr><tr><td>PostgreSQL<\/td><td><code>LIMIT<\/code> \/ <code>OFFSET<\/code> or <code>FETCH<\/code><\/td><td>Both options supported<\/td><\/tr><tr><td>SQL Server<\/td><td><code>TOP<\/code><\/td><td>Use <code>TOP N<\/code> in SELECT<\/td><\/tr><tr><td>Oracle 12c+<\/td><td><code>FETCH FIRST<\/code><\/td><td>Use with <code>ORDER BY<\/code> for proper results<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Use this to show only top results or paginate large datasets<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>These keywords let you limit how many rows your query returns. Usage differs slightly by database. \ud83d\udd39 Syntax by DBMS \ud83d\udd39 Pagination Example (MySQL\/PostgreSQL) \ud83e\udde0 Quick Recap DBMS Keyword Used Notes MySQL LIMIT With optional OFFSET for paging PostgreSQL LIMIT \/ OFFSET or FETCH Both options supported SQL Server TOP Use TOP N in SELECT [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3921","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}