Python MySQL Limit
In this page:
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 करने के लिए उपयोगी है।
उदाहरण: Basic LIMIT Usage
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।
उदाहरण: Getting the "Top N" Results
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 है।
उदाहरण: Skipping Rows with OFFSET
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 करना है।
उदाहरण: Building a Reusable Pagination Function
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 के आर-पार भी नहीं जातीं।
उदाहरण: LIMIT Without OFFSET vs a Full Fetch
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")
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join