MySQL Tutorial

Define MySQL

MySQL Create Database

MySQL Create Table

MySQL Constraints

MySQL Case Sensitivity

MySQL Insert

MySQL Select

MySQL Data Types

MySQL OrderBy


MySQL Distinct

MySQL Update

MySQL Delete

MySQL Truncate

MySQL Drop

MySQL Alter

MySQL Join

MySQL Aggregate Function

MySQL String Function

MySQL Date Function

Page Stats

Visitor: 278

MySQL Top Clause

If you want to retrieve the top 10 employees who recently joined the organization, or top 3 students who got the highest score, or something like that, to handle such situations, you can use MYSQL's LIMIT clause in SELECT statement to restrict the number of rows returned by a query.

SELECT * FROM employees ORDER BY salary DESC LIMIT 3;

Setting Row Offset in LIMIT Clause

The LIMIT clause accepts an optional second parameter. When two parameters are specified, the first parameter specifies the offset of the first row to return i.e. the starting point, whereas the second parameter specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1). For example: if you want to find out the third-highest paid employee, you can do the following:

SELECT * FROM employees ORDER BY salary DESC LIMIT 2, 1;