← Back to PostgreSQL Course | Chapter 2: Basic Queries | Lesson 6 of 7

LIMIT/OFFSET का उपयोग

LIMIT rows की संख्या cap करता है और OFFSET rows skip करता है, जो साथ में pagination देते हैं।

In this page:

  1. LIMIT/OFFSET
Syntax
sql
SELECT columns
FROM table_name
ORDER BY column
LIMIT n OFFSET m;

LIMIT/OFFSET

LIMIT n ज़्यादा से ज़्यादा n rows return करता है और OFFSET m पहले m skip करता है। हमेशा इन्हें ORDER BY के साथ pair करें ताकि pages stable रहें।

बड़े offsets slow हैं क्योंकि database फिर भी skipped rows walk करता है; keyset pagination (WHERE id > last_id) बेहतर scale करता है।

PostgreSQL FETCH FIRST n ROWS ONLY भी support करता है।

Note: बड़ी tables पर keyset pagination OFFSET से बेहतर है।

उदाहरण: LIMIT/OFFSET

sql
CREATE TABLE items (id INTEGER PRIMARY KEY, label TEXT);
INSERT INTO items VALUES (1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'),(6,'f'),(7,'g');
SELECT label FROM items ORDER BY id LIMIT 3;
SELECT label FROM items ORDER BY id LIMIT 3 OFFSET 3;
SELECT label FROM items ORDER BY id DESC LIMIT 2;

-- Output:
-- label
-- a
-- b
-- c
-- label
-- d
-- e
-- f
-- label
-- g
-- f
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना ORDER BY के paging करना
  2. बहुत बड़ी OFFSET values इस्तेमाल करना
  3. यह भूल जाना कि LIMIT sorting के बाद apply होता है
चैप्टर सारांश
  • LIMIT rows cap करता है
  • OFFSET rows skip करता है
  • Stable pages के लिए ORDER BY इस्तेमाल करें
  • Keyset pagination बेहतर scale करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.