In this tutorial, we will learn how to retrieve a fixed number of records from a table of the database using SQL.
SQL SELECT TOP clause is used to fetch the limited number of records from the table.
SELECT TOP clause supports MS SQL Server/MS Acces.
SELECT TOP [Number | Percent] columns
FROM tableName
WHERE condition;
LIMIT clause supports in MySQL.
SELECT columns
FROM tableName
WHERE condition
LIMIT number;
ROWNUM clause supports in Oracle
SELECT columns
FROM tableName
WHERE ROWNUM <= number;
Let us consider this table "Employee" for records.
Table Name: Employee
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
| 4 | Kapil | Noida | India | male | 25000 |
| 5 | Shalini | Jaipur | India | female | 18000 |
| 6 | Rakesh | Faridabad | India | male | 23000 |
| 7 | Akshay | Mumbai | India | male | 21000 |
| 8 | Sarah | New York | US | female | 76000 |
| 9 | Rocky | Noida | India | male | 28000 |
If you use SQL Server, then for fetch top 3 records, a query will be:
SELECT TOP 3 FROM Employee;
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
If you use MySQL Server, then for fetch top 3 records, a query will be:
SELECT *
FROM Employee
LIMIT 3;
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
If you use Oracle, then for fetch top 3 records, a query will be:
SELECT *
FROM Employee
WHERE ROWNUM <=3;
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
If you use SQL Server, then for fetch top 3 records WHERE Country="India", a query will be:
SELECT TOP 3
FROM Employee
WHERE Country='India';
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
If you use MySQL Server, then for fetch top 3 records WHERE Country="India", a query will be:
SELECT *
FROM Employee
WHERE Country='India'
LIMIT 3;
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |
If you use Oracle, then for fetch top 3 records WHERE Country="India", a query will be:
SELECT *
FROM Employee
WHERE Country='India' AND ROWNUM <=3;
| ID | EmpName | City | Country | Gender | Salary |
| 1 | Shankar | Delhi | India | male | 25000 |
| 2 | Sourabh | Delhi | India | male | 30000 |
| 3 | Ranvijay | Mumbai | India | male | 15000 |