Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
SQL

SQL: Use LIMIT and OFFSET for Efficient Pagination

- 13.06.26 - ErcanOPAK

📄 Don’t Load 10,000 Rows at Once

Loading all rows is slow. LIMIT and OFFSET load only needed rows. Faster queries, better user experience.

📝 Basic Pagination

-- PostgreSQL / MySQL / SQLite
SELECT * FROM users
ORDER BY id
LIMIT 10 OFFSET 0;  -- Page 1 (rows 1-10)

SELECT * FROM users
ORDER BY id
LIMIT 10 OFFSET 10; -- Page 2 (rows 11-20)

-- SQL Server (OFFSET FETCH)
SELECT * FROM users
ORDER BY id
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

-- MySQL alternative
SELECT * FROM users
ORDER BY id
LIMIT 10, 10;  -- LIMIT offset, count

🎯 Cursor-Based Pagination (Better for Large Data)

-- Instead of OFFSET (which scans rows), use WHERE
SELECT * FROM users
WHERE id > 100  -- last seen ID
ORDER BY id
LIMIT 10;

-- With date cursor
SELECT * FROM orders
WHERE order_date > '2024-01-15'
ORDER BY order_date
LIMIT 20;

-- Performance: OFFSET 10000 is slow (scans 10000 rows)
-- Cursor: only scans 10 rows (much faster)

💡 Best Practices

  • Always use ORDER BY with LIMIT (consistent results)
  • Small page sizes (10-50 rows for UI, 100-500 for APIs)
  • For large datasets, use cursor-based pagination (keyset pagination)
  • Index the ORDER BY column for fast pagination

“Query loaded 50,000 rows. UI crashed. Added LIMIT 50. API response 2s → 50ms. Pagination is essential for any list view.”

— Backend Developer

Related posts:

SQL: Use LATERAL Joins to Reference Previous Rows

SQL: Optimizing Multiple WHERE Clauses with Index Intersection

SQL: Multi-tenant Security with Row-Level Security (RLS)

Post Views: 2

Post navigation

.NET Core: Configure CORS to Allow API Access from Other Domains
C#: Use Null Forgiving Operator (!) When You Know Better Than Compiler

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (858)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (597)
  • Add Constraint to SQL Table to ensure email contains @ (582)
  • Average of all values in a column that are not zero in SQL (545)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (512)
  • Find numbers with more than two decimal places in SQL (460)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (858)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com