How to Limit Rows in a MySQL Result Set Using SQL
Limiting Rows in a MySQL Result Set in SQL
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.
For example, if you wanted to return the first 10 rows of a result set, you would use the following query:
SELECT * FROM table_name LIMIT 10;
If you wanted to return the next 10 rows, you would use the following query:
SELECT * FROM table_name LIMIT 10, 10;
Examples
Let's look at a few examples of how this clause can be used. Suppose you have a table called users with the following data:
name | age |
---|---|
John | 25 |
Jane | 30 |
John | 25 |
Bob | 20 |
If you wanted to return the first two rows of this table, you would use the following query:
SELECT * FROM users LIMIT 2;
This query would return the following result:
name | age |
---|---|
John | 25 |
Jane | 30 |
Additional Info about Limiting Rows in MySQL Result Sets
The LIMIT clause is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database.