;

SQL TOP/LIMIT/ROWNUM


In this tutorial, we will learn how to retrieve a fixed number of records from a table of the database using SQL. 

SQL TOP, LIMIT or ROWNUM Clause

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.

Syntax of SELECT TOP Clause 

Syntax - SELECT TOP Clause
SELECT TOP [Number | Percent] columns
FROM tableName 
WHERE condition;

Syntax of LIMIT Clause

LIMIT clause supports in MySQL.

Syntax - LIMIT Clause
SELECT columns
FROM tableName
WHERE condition
LIMIT number;

Syntax of ROWNUM Clause

ROWNUM clause supports in Oracle

Syntax - ROWNUM Clause
SELECT columns
FROM tableName
WHERE ROWNUM <= number;

Example:

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

Example of SELECT TOP clause

If you use SQL Server, then for fetch top 3 records, a query will be: 

Example - SELECT TOP Clause
SELECT TOP 3 FROM Employee;
Output
ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
3 Ranvijay Mumbai India male 15000

Example of LIMIT clause

If you use MySQL Server, then for fetch top 3 records, a query will be:

Example - LIMIT Clause
SELECT * 
FROM Employee
LIMIT 3;
Output
ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
3 Ranvijay Mumbai India male 15000

Example of ROWNUM Clause

If you use Oracle, then for fetch top 3 records, a query will be:

Example - ROWNUM Clause
SELECT * 
FROM Employee
WHERE ROWNUM <=3;
Output
ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
3 Ranvijay Mumbai India male 15000

TOP, LIMIT or ROWNUM Clause using with WHERE clause in the Statement

If you use SQL Server, then for fetch top 3 records WHERE Country="India", a query will be:

Example - TOP Clause with WHERE Clause
SELECT TOP 3 
FROM Employee 
WHERE Country='India';
Output
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:

Example - LIMIT Clause with WHERE Clause
SELECT * 
FROM Employee
WHERE Country='India'
LIMIT 3;
Output
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:

Example - ROWNUM Clause with WHERE Clause
SELECT * 
FROM Employee
WHERE Country='India' AND ROWNUM <=3;
Output
ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
3 Ranvijay Mumbai India male 15000