MySQL LIMIT Clause

MySQL LIMIT Clause

The LIMIT clause in MySQL is used to specify the maximum number of records to return from a query. It is commonly used when displaying paginated results or when you only need a portion of the data.


Syntax


  • number = how many rows you want to return.


🧠 Example Table: students

id name age city
1 John 20 New York
2 Emma 21 Chicago
3 Raj 22 Mumbai
4 Arjun 23 Delhi
5 Sara 19 London

✅ Example 1: Return First 3 Rows


✔ Returns rows 1, 2, and 3.


LIMIT with OFFSET

To skip some rows before fetching results, use:


  • offset → The number of rows to skip.

  • count → Number of rows to return.


✅ Example 2: Skip First 2 Rows, Return Next 2


✔ Returns rows: 3 and 4.


😀 Example 3: Pagination Example

Suppose each page shows 5 records.

  • Page 1:


  • Page 2:


  • Page 3:



🧠 Often Used with ORDER BY


✔ Returns the student with the highest age.


🎯 Summary Table

Feature Description
LIMIT n Returns first n rows
LIMIT offset, count Skips offset rows and returns count rows
Used with ORDER BY To get sorted limited results
Common use case Pagination, Top records

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

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