Use of TOP or ROWNUM or LIMIT function in SQL Query with examples
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;