SQL: SELECT LIMIT Statement
This SQL tutorial explains how to use the SELECT LIMIT statement in SQL with syntax and examples.
Description
The SQL SELECT LIMIT statement is used to retrieve records from one or more tables in a database and limit the number of records returned based on a limit value.
Note: SELECT LIMIT is not supported in all SQL databases. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM, SQL Server or MSAccess use TOP statement to limit your results. The SELECT TOP statement is Microsoft's proprietary equivalent to the SELECT LIMIT statement.
The syntax for the SELECT LIMIT statement in SQL is:
SELECT expressions FROM tables [WHERE conditions] [ORDER BY expression [ ASC | DESC ]] LIMIT number_rows [ OFFSET offset_value ];
Parameters or Arguments
- expressions
- The columns or calculations that you wish to retrieve.
- tables
- The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
- WHERE conditions
- Optional. The conditions that must be met for the records to be selected.
- ORDER BY expression
- Optional. It is used in the SELECT LIMIT statement so that you can order the results and target those records that you wish to return. ASC is ascending order and DESC is descending order.
- LIMIT number_rows
- It specifies a limited number of rows in the result set to be returned based on number_rows. For example, LIMIT 10 would return the first 10 rows matching the SELECT criteria. This is where sort order matters so be sure to use an ORDER BY clause appropriately.
- OFFSET offset_value
- Optional. The first row returned by LIMIT will be determined by offset_value.
No comments:
Post a Comment