To get a specific number of records TOP clause is used.
It is helpful when the data is large because returning a large data can impact performance.
Note=> All database systems does not support the TOP clause. MySQL supports the LIMIT clause, while Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM.
Syntax(In MYSQL):
SELECT * FROM table_name
WHERE condition
LIMIT number;
SAMPLE STUDENTS DATABASE:
Roll_No | Name | Marks |
1 | Dev | 35 |
2 | Ayush | 45 |
3 | Ram | 55 |
4 | Pyush | 65 |
5 | Nazim | 75 |
Example: To select 3 students from the students table.
SELECT * FROM students
LIMIT 3;
This will fetch the values as shown below:
Roll_No | Name | Marks |
1 | Dev | 35 |
2 | Ayush | 45 |
3 | Ram | 55 |
SYNTAX (SQL Server / MS Access):
SELECT TOP number|percent * FROM table_name
WHERE condition;
SYNTAX (Oracle 12):
SELECT * FROM table_name
FETCH FIRST number ROWS ONLY;