How To Limit The Number Of Rows Returned In MySQL (2024)

March 29, 2023 By Matthew Rathbone *

As a database administrator or a web developer, you may have faced the situation where you need to retrieve only a limited number of rows from a large dataset in your MySQL database. In this post, I will show you how to use the LIMIT clause in MySQL to achieve this.

How To Limit The Number Of Rows Returned In MySQL (1) A Database Manager That Is Modern, Fast, & Easy To Use

Tried a few tools. Beekeeper was the only one that I found that felt right. Most had a very 1990's feel to them - Allan

I built Beekeeper Studio because, like Allan, I wanted something more intuitive and modern than all the existing clunky apps I could find. My customers agree - they love using Beekeeper and they tell me every day! Give it a try, I bet you'll like it too.

The MySQL LIMIT Clause

The LIMIT clause is used to limit the number of rows returned in a SELECT statement in MySQL. It is a very useful clause when you are working with large datasets, as it allows you to return only the number of rows that you need, which can improve the performance of your queries and save valuable resources.

LIMIT Clause Syntax

The syntax of the LIMIT clause is very simple. You just need to add the keyword LIMIT followed by the number of rows you want to retrieve. For example, to retrieve the first 10 rows from the employees table, you can use the following query:

SELECT * FROM employees LIMIT 10;

LIMIT With OFFSET

You can also specify the starting point of the rows you want to retrieve by adding an offset value to the LIMIT clause. For example, to retrieve the 11th to the 20th rows from the employees table, you can use the following query:

SELECT * FROM employees LIMIT 10 OFFSET 10;

Use Cases For LIMIT

The LIMIT clause has a wide range of use cases, from simple pagination of data in a web application to complex reporting and analysis. Here are a few common use cases:

Pagination With LIMIT and OFFSET

One of the most common use cases of the LIMIT clause is pagination of data in a web application. You can use the LIMIT clause to retrieve a limited number of rows for each page and display them to the user. For example, to display the first 10 rows in the first page, the 11th to the 20th rows in the second page, and so on, you can use the following query:

SELECT * FROM employees LIMIT 10 OFFSET (10 * ($page_number - 1));

Reporting

Another common use case of the LIMIT clause is reporting and analysis. You can use the LIMIT clause to limit the number of rows returned in a report to a specific number of rows, for example, the top 10 or 20 rows based on a certain criteria. For example, to retrieve the top 10 employees with the highest salaries, you can use the following query:

SELECT * FROM employees ORDER BY salary DESC LIMIT 10;

Debugging

The LIMIT clause can also be used for debugging purposes. You can use the LIMIT clause to retrieve a limited number of rows to test and debug your queries before executing them on the entire dataset. For example, to retrieve the first 5 rows from the employees table to test your query, you can use the following query:

SELECT * FROM employees LIMIT 5;

Conclusion

The LIMIT clause is a simple and powerful tool that allows you to limit the number of rows returned in a SELECT statement in MySQL. Whether you are paginating data in a web application, creating reports, or debugging your queries, the LIMIT clause is a useful and essential tool for working with large datasets in MySQL

How To Limit The Number Of Rows Returned In MySQL (2024)

FAQs

How To Limit The Number Of Rows Returned In MySQL? ›

To to limit the number of rows in a MySQL result set, use the LIMIT clause. This clause takes two arguments: the number of rows to return, and an optional offset.

How do I limit row output in SQL? ›

If you don't need to omit any rows, you can use SQL Server's TOP clause to limit the rows returned. It is placed immediately after SELECT . The TOP keyword is followed by integer indicating the number of rows to return. In our example, we ordered by price and then limited the returned rows to 3.

How to get 10 rows in MySQL? ›

SELECT * FROM your_table ORDER BY RAND() LIMIT 10; This query selects 10 random rows from your_table . The ORDER BY RAND() clause randomizes the row order, and the LIMIT 10 clause restricts the output to 10 rows.

What MySQL clause is used to restrict the rows returned by a query? ›

The LIMIT clause is used to limit the number of rows returned in a SELECT statement in MySQL.

How to limit values in MySQL? ›

In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set. The Limit Clause accepts one or two arguments which are offset and count. The value of both the parameters can be zero or positive integers.

How to fetch last 10 rows in SQL? ›

To get the last 10 rows in SQL, you can use the ORDER BY clause with the DESC (descending) or ASC (ascending) keyword to sort the rows, and then use the LIMIT clause to limit the result set to the last 10 rows.

Does SQL limit return random rows? ›

The LIMIT clause randomly picks rows to be returned unless ORDER BY clause exists together with the LIMIT clause. In other words, the ORDER BY as well as the LIMIT clause must be part of the same SQL statement and not like the case where one is part of main query and other is part of subquery.

What is the maximum row count limit in MySQL? ›

The MySQL maximum row size limit of 65,535 bytes is demonstrated in the following InnoDB and MyISAM examples. The limit is enforced regardless of storage engine, even though the storage engine may be capable of supporting larger rows.

How to keep only last 10 records in MySQL? ›

The following is the syntax to get the last 10 records from the table. Here, we have used LIMIT clause. SELECT * FROM ( SELECT * FROM yourTableName ORDER BY id DESC LIMIT 10 )Var1 ORDER BY id ASC; Let us now implement the above query.

What is the limit clause in MySQL? ›

The MySQL LIMIT Clause

The LIMIT clause is used to specify the number of records to return. The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

What is 0 rows returned in MySQL? ›

Return Values

Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed.

Which of the clauses is used to limit the number of rows? ›

In the SELECT argument, the LIMIT clause is used to LIMIT the number of rows to be returned.

Which SQL command is used to restrict the number of rows returned from a query? ›

The SQL LIMIT clause is a powerful tool for managing and retrieving data efficiently in databases. It allows you to restrict the number of records returned by a SQL query, which is particularly useful when dealing with large datasets where performance and speed are critical.

How to limit rows in SQL Workbench? ›

You can easily change this limit by going to MySQL Workbench >> Edit >> Preferences >> SQL Queries tab. Over here you will option to Limit Rows. You can set this to very high value or uncheck the option.

Which keyword should be used to restrict the number of rows returned? ›

The limit keyword is used to limit the number of rows returned in a query result.

How do I restrict values in a column in MySQL? ›

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

How do I limit rows in SQL Server query? ›

If you wanted to limit the result set to two rows, you would use the following query: SELECT TOP 2 * FROM users; This query would return the first two rows of the table (John 25 and Jane 30).

How do I limit results to 100 in SQL? ›

In Mode's Query Editor, you may have noticed the checkbox next to "Run" that says "Limit 100". As you might expect, the limit restricts how many rows the SQL query returns. The default value is 100; when this box is checked, it's telling the database to only return the first 100 rows of the query.

Does SQL have a row limit? ›

SQL Server does not limit the number of rows in a table (different from columns), nor does it have a specific maximum number of rows.

Which keyword is used to limit the number of rows in the query output? ›

In the SELECT argument, the LIMIT clause is used to LIMIT the number of rows to be returned.

Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 5434

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.