how to retrieve the top 3 salaries of the table using rownum
Answers were Sorted based on User's Feedback
SELECT * FROM(SELECT * FROM EMP ORDER BY SAL DESC) WHERE ROWNUM<=3;
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / d.elumalai
CREATE TABLE EMP (NAME VARCHAR2(20),SALARY NUMBER(10));
INSERT INTO EMP VALUES('AAA',10000);
INSERT INTO EMP VALUES('BBB',20000);
INSERT INTO EMP VALUES('CCC',30000);
INSERT INTO EMP VALUES('DDD',40000);
INSERT INTO EMP VALUES('EEE',50000);
INSERT INTO EMP VALUES('FFF',60000);
INSERT INTO EMP VALUES('GGG',70000);
INSERT INTO EMP VALUES('HHH',80000);
INSERT INTO EMP VALUES('III',90000);
COMMIT;
SELECT A.* FROM (SELECT name, salary FROM EMP ORDER BY salary DESC) A WHERE ROWNUM<=3;
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / muneer ahamed
select rownum as rank,sal from (select rownum,sal from emp
order by sal desc)
where rownum<4
| Is This Answer Correct ? | 4 Yes | 5 No |
Answer / phantom coding
select * from (select rownum as rnum,t.* from emp order by
sal desc) a where rnum < 4
the alias to rownum (i.e:rnum) should be used. If rownum is
used for the constraint then the query would return the
first three rows it hits and the results could be wrong.
| Is This Answer Correct ? | 1 Yes | 2 No |
Answer / ram
Select * from emp
where emp.salary in (select emp.salary from emp
where (select distict salary
from emp order by desc null last)
where rownum <=3);
| Is This Answer Correct ? | 1 Yes | 2 No |
Answer / nasir iqbal
SELECT * FROM EMP A WHERE 1=(SELECT COUNT (*) FROM EMP B
WHERE B.SAL>=A.SAL)
SELECT * FROM EMP
| Is This Answer Correct ? | 1 Yes | 2 No |
Answer / debanjan rudra
select sal from (select sal from emp order by val desc)
where rownum < 4;
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / ashwinee
select empnumber
from emp
where rownmum < 4 and salary in (select salary
from emp
order by salary desc)
| Is This Answer Correct ? | 0 Yes | 2 No |
Answer / manivasan.s.,
select salary from salary_table
where rownum <4
order by salary desc
| Is This Answer Correct ? | 2 Yes | 5 No |
Answer / sumalatha
select top 3 esal from employee order by esal desc
| Is This Answer Correct ? | 4 Yes | 18 No |
can use the following like overloading concept in a single package: procedure p1(a varchar), procedure p1(a varchar2), procedure p1(a char)
How do I view a procedure in sql?
How do I order columns in sql?
How to display the records between two range in Oracle SQL Plus?
What is pragma in sql?
which operator is used in query for pattern matching? : Sql dba
Can we perform dml in function?
Explain the commit statement.
What is a dirty read sql?
What are %type and %rowtype for?
Write a sql query to get the third highest salary of an employee from employee_table?
Does sql full backup truncate logs?
Oracle (3253)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)