LIMIT
is an optional clause that can be combined with SELECT
statements used for retrieving records from one or more tables. It basically specifies the number of records a query should return after filtering the data.
LIMIT
clause syntax:
column_list
: The columns or calculations that you wish to retrieve.table_name
: The tables that you want to retrieve records from.FROM
clause.ORDER BY
: It is an expression used to order the results as you wish to return. The expression could be ascending (ASC) or descending (DESC)LIMIT row_count
: It specifies a limited number of rows to be returned based on row_count.row_count
value is NULL, the query will produce a similar outcome because it does not contain the LIMIT
clause.row_count
is zero, the statement will return an empty set.LIMIT
clause.
Here we are creating one new table called comporders using the CREATE TABLE
command and inserting some values into the table using the INSERT
command:
LIMIT
with the ORDER BY
ExpressionLIMIT
clause to get the first four orders sorted by order_id
:
LIMIT
with ASC/DESCLIMIT
clause to select rows with the highest or lowest values from a table.
LIMIT
clause to get the first 5 orders.LIMIT
clause to get the first 5 orders.LIMIT
with OFFSET
LIMIT
and OFFSET
clauses to get 5 orders using the below query:
order_id
= 1002 & 1003 aren’t displayed because we put the OFFSET
value with 2. So the first 2 lines are ignored.order_id
= 1009 & 1010 aren’t displayed because the LIMIT
value is 5, which will display only 5 rows.