Use of TOP or ROWNUM or LIMIT function in SQL Query

Use of TOP or ROWNUM or LIMIT function in SQL Query

Select query with specific no of rows

Example:
In following example we want to select 4 rows from the table which using different RDBMS databases or software.
We are showing example of different syntax on each RDBMS databases software.
Note: you can also used order by with this to find highest salary in table example.

SQL Server

select top 4 column_name from table_name

Example highest salary of employee:
select top 4 employee_name,salary from employees order by salary desc;
;

Oracle

select columnname from table_name where rownum <= 4;

Example highest salary of employee:
Select * from (select employee_name,salary from employees order by salary desc) where rownum <= 4;

MySQL and PostgreSQL

select columnname from table_name limit 4;

Example highest salary of employee:
select employee_name,salary from employees order by salary desc limit 4;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.