Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 52

MySQL में LIMIT और OFFSET

LIMIT और OFFSET

LIMIT returned rows cap और OFFSET ordered result से rows skip करता है। Preview, top-N और pagination में useful हैं, पर correctness deterministic ORDER BY से शुरू होती है।

Common learning data: students में Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) और Sana (X-B, 88.50, Active) हैं।
SELECT student_id, full_name, marks
FROM students
ORDER BY marks DESC, student_id ASC
LIMIT 2;
Meera | 91.00 Sana | 88.50

MySQL Syntax Forms

LIMIT row_count
LIMIT row_count OFFSET offset
LIMIT offset, row_count
Confusion से बचें: LIMIT 10 OFFSET 20 और LIMIT 20, 10 समान हैं। Teaching और maintainable code में keyword form prefer करें।

Page Calculation

Page size 2 में page 1 offset 0, page 2 offset 2 और page 3 offset 4:

offset = (page_number - 1) * page_size
SELECT student_id, full_name, marks
FROM students
ORDER BY marks DESC, student_id ASC
LIMIT 2 OFFSET 2;
Aarav | 86.50 Kabir | 74.00

Page positive integer validate करें, page_size cap करें और driver support अनुसार numeric values bind करें।

Deterministic Ordering

ORDER BY बिना execution changes rows को pages के बीच move कर सकते हैं। Marks tie हों तो marks DESC incomplete है। Unique final key जोड़ें:

ORDER BY marks DESC, student_id ASC

Page requests के बीच data change होने पर offset pages duplicate/missing rows दे सकते हैं। Application need के अनुसार consistency चुनें।

Large OFFSET और Keyset Pagination

Deep offset earlier rows scan/discard कर सकता है। student_id ordered “next” feed में:

SELECT student_id, full_name
FROM students
WHERE student_id > ?
ORDER BY student_id ASC
LIMIT 20;

Previous page की last ID bind करें। Multi-column order में complete tuple seek condition चाहिए। Keyset sequential navigation में fast/stable है, arbitrary page jump natural नहीं।

गलतियाँ और अभ्यास

  • ORDER BY बिना LIMIT।
  • Comma arguments उलटना।
  • Maximum page size न रखना।
  • Ties ignore करना।
  • Endless feed में huge OFFSET।
  1. Top three marks deterministic लौटाएँ।
  2. Size 2 की page 2 लिखें।
  3. ID-based next page को keyset में लिखें।
  4. New inserts offset pages क्यों shift करते हैं बताएँ।

त्वरित सारांश

  • LIMIT rows cap और OFFSET ordered rows skip करता है।
  • ORDER BY plus unique tie-breaker लें।
  • Pagination inputs validate और cap करें।
  • Sequential navigation में keyset large-offset cost बचाता है।

Official संदर्भ

References 14 August 2026 को review किए गए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

MySQL में LIMIT क्या करता है?
Filtering और ordering के बाद returned rows की maximum संख्या तय करता है। ORDER BY बिना कौन-सी rows first हैं, यह define नहीं करता।
OFFSET क्या है?
Limited page लौटाने से पहले ordered result की कितनी rows skip करनी हैं, यह OFFSET बताता है।
Page offset कैसे calculate होता है?
One-based page p और page size n के लिए offset = (p - 1) × n। दोनों inputs validate करें और maximum page size रखें।
Large OFFSET slow क्यों होता है?
MySQL को earlier rows locate और discard करनी पड़ सकती हैं। Few rows लौटने पर भी deep pages expensive हो सकते हैं।
Keyset pagination क्या है?
यह previous rows count/discard करने के बजाय last seen ordered key के बाद rows माँगता है, जैसे WHERE student_id > ?।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.