← Back to Python Course | Chapter 15: Python & MySQL | Lesson 11 of 12

Python MySQL Limit

एक LIMIT clause यह सीमित करता है कि कोई SELECT statement कितनी rows return करता है -- pagination के लिए, किसी बड़े table को preview करने के लिए, या बस सबसे ऊपर की कुछ results पाने के लिए (अक्सर ORDER BY के साथ मिलाकर, जैसे सबसे हाल की या सबसे high-value 5 rows पाना) यह ज़रूरी है।
Syntax
python
cursor.execute("SELECT * FROM table_name LIMIT number")
cursor.execute("SELECT * FROM table_name LIMIT number OFFSET start")

Basic LIMIT इस्तेमाल

cursor.execute("SELECT * FROM customers LIMIT 5") query अन्यथा जितनी भी rows return करती, उनमें से ज़्यादा से ज़्यादा पहली 5 rows return करता है, चाहे कुल कितनी भी rows असल में मेल खाती हों -- यह हर row खींचे बिना किसी बड़े table को जल्दी preview करने के लिए उपयोगी है।

Note: Development के दौरान किसी बड़े table को preview या sample करते समय हमेशा LIMIT जोड़ें, ताकि गलती से हज़ारों rows memory या terminal में खींचने से बचा जा सके।
Warning: बिना साथ में ORDER BY के, LIMIT ठीक कौन सी rows return करता है इसकी कोई meaningful या runs के बीच consistent guarantee नहीं है।

उदाहरण: Basic LIMIT Usage

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers LIMIT 5")  # caps the result at 5 rows
print("Returns at most 5 rows")

"Top N" Results पाना

ORDER BY ... DESC को LIMIT n के साथ मिलाना किसी measure से top N results पाने का standard pattern है -- उदाहरण के लिए, सबसे महंगे 5 products, या सबसे हाल के 10 orders।

Note: जब लक्ष्य genuinely "किसी criteria से top N" हो, तो हमेशा LIMIT को एक explicit ORDER BY के साथ जोड़ें -- अकेला LIMIT किसी particular ordering की guarantee नहीं देता।
Warning: LIMIT की सीमा पर ties (cutoff point पर identical sort value वाली कई rows) यह कुछ हद तक arbitrary बना सकती हैं कि ठीक कौन सी row आखिरी में दिखे, जब तक कि ORDER BY में एक tie-breaking column न जोड़ी जाए।

उदाहरण: Getting the "Top N" Results

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM products ORDER BY price DESC LIMIT 5")  # highest prices first, capped at 5
print("The 5 most expensive products")

OFFSET से Rows Skip करना

LIMIT n OFFSET m पहली m matching rows को पूरी तरह skip कर देता है, फिर उसके बाद से शुरू होकर n rows तक return करता है -- यही paginated results, जैसे किसी search results list के "page 2", के पीछे का fundamental mechanism है।

Note: Pagination implement करते समय OFFSET को (page_number - 1) * page_size के रूप में calculate करें, यह एक आम formula है जो याद रखने लायक है।
Warning: किसी बहुत बड़े table पर एक बहुत बड़ी OFFSET value noticeably धीमी हो सकती है, क्योंकि actually return होने वाली rows तक पहुँचने से पहले MySQL अब भी internally हर skipped row को process और discard करता है।

उदाहरण: Skipping Rows with OFFSET

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers ORDER BY id LIMIT 10 OFFSET 10")  # skips the first 10, then takes 10 more
print("Page 2 of results, 10 per page")

एक Reusable Pagination Function बनाना

LIMIT/OFFSET pattern को एक छोटे Python function में wrap करना जो एक page number और page size accept करता है, pagination logic को किसी application की हर उस जगह में reusable और consistent बनाता है जहाँ results के through page करना है।

Note: हर call site पर हाथ से LIMIT और OFFSET दोबारा calculate करने की बजाय, pagination logic को एक reusable function में centralize करें, जिससे off-by-one page-size bug की संभावना कम हो।
Warning: OFFSET compute करने से पहले हमेशा verify करें कि page_number कम से कम 1 है -- 0 या negative page_number एक बेतुकी या invalid negative OFFSET देता है।

उदाहरण: Building a Reusable Pagination Function

python
def paginate(page_number, page_size=10):
    offset = (page_number - 1) * page_size  # rows to skip for this page
    return f"LIMIT {page_size} OFFSET {offset}"

print(paginate(2))

बिना OFFSET के LIMIT बनाम Full Fetch

SQL level पर LIMIT लागू करना (database को खुद सिर्फ ज़रूरी rows return करने देना), पूरे table को fetchall() से fetch करके फिर resulting Python list को size तक slice करने से कहीं ज़्यादा efficient है, क्योंकि अनचाही rows कभी network के आर-पार भी नहीं जातीं।

Note: Row-limiting logic को हमेशा SQL query में ही LIMIT से push करें, न कि सब कुछ fetch करके बाद में Python में result slice करें।
Warning: result = cursor.fetchall()[:5] अब भी हर single row को memory में transfer और hold करता है इससे पहले कि 5 को छोड़कर बाकी सब discard हो -- यह किसी बड़े table पर एक असली, टाला जा सकने वाला performance cost है।

उदाहरण: LIMIT Without OFFSET vs a Full Fetch

python
from unittest.mock import MagicMock

conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers LIMIT 5")  # efficient: filtered in SQL
print("Only 5 rows ever cross the network")
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. #}

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.