The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause
Let's see an example. If a table has a large number of data, select top statement determines that how many rows will be retrieved from the given table.
SELECT TOP number|percent column_name(s) FROM table_name;
MySQL Syntax
SELECT column_name(s) FROM table_name LIMIT number;
SELECT * FROM Persons LIMIT 5;
SELECT column_name(s) FROM table_name WHERE ROWNUM <= number;
SELECT * FROM Persons WHERE ROWNUM <=5;
There is an example of employee table:
Emp_Id | Name | Sir_Name | User_Name |
---|---|---|---|
1 | Mithiliesh | Kushwaha | Mithi |
2 | Harish | Sharma | Sharma.harish |
3 | Girish | Guddu | Girishs |
Let's see the syntax for the select top statement.
SELECT COUNT (expression)
Let's see the example of sql select top statement.
SELECT TOP 2 * FROM employee
It will return the following table:
Emp_Id | Name | Sir_Name | User_Name |
---|---|---|---|
1 | Neha | Singh | Nehgasingh |
2 | Rahul | Singh | rahul |