Summary: In this tutorial, you will learn how to use MySQL LIMIT clause to constrain number of returned records in SQL SELECT statement.

Most of the times, when you work with master data tables which contain thousand to millions of records and you don’t want to write a query to get all the data from those tables because of application’s performance and high traffic between database server and application server. MySQL supports a cool feature called LIMIT to allow you to constrain the returned records with SELECT statement. Let take a look at the MySQL LIMIT syntax:

Let’s say you have a database table with 10000 records and you want to get just first N records, you can use the following query:

1 SELECT * FROM table
2 LIMIT N

The MySQL LIMIT also allows you to get a range of records where you decide starting record number and how many records you want to retrieve. Here is the syntax of MySQL LIMIT to select a range of records:

1 SELECT columns
2 FROM table
3 LIMIT S, N

In the query above, S is the starting record index. MySQL specifies that the first record starts with 0.  N is the number of records you want to select.

Let’s practice MySQL LIMIT with several examples to have a better understanding.

If you want to get the first five employees in the table employees, you can use the following query:

1 SELECT firstname,lastname
2 FROM employees
3 LIMIT 5
+-----------+-----------+
| firstname | lastname  |
+-----------+-----------+
| Diane     | Murphy    |
| Mary      | Patterson |
| Jeff      | Firrelli  |
| William   | Patterson |
| Gerard    | Bondur    |
+-----------+-----------+
5 rows in set (0.00 sec)

Now if you want to get five employees from employee number 10 you can use MySQL LIMIT with offset as follows:

1 SELECT firstname,lastname
2 FROM employees
3 LIMIT 10,5
+-----------+-----------+
| firstname | lastname  |
+-----------+-----------+
| Foon Yue  | Tseng     |
| George    | Vanauf    |
| Loui      | Bondur    |
| Gerard    | Hernandez |
| Pamela    | Castillo  |
+-----------+-----------+
5 rows in set (0.00 sec)

Related posts:

  1. Using MySQL SELECT Statement to Query Data
  2. How to Use MySQL Distinct to Eliminate Duplicate Rows
  3. Working with Database Table – Part I
  4. Managing Databases in MySQL
  5. Managing Database Index in MySQL

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/