I'm working on one SQL query.
Table name: employees.
I want to get the MAX and MIN sal with their employee names in SQL.
I know how to do with either MAX or MIN. But how can we do it both in one query?
I need a single row output like below:
e1.name AS MaxName, MAX(e1.sal) AS MaxSalary, e2.name AS MinName, MIN(e2.sal) AS MinSalary
In a single select:
SELECT MIN( salary ) AS MinSalary,
MIN( name ) KEEP ( DENSE_RANK FIRST ORDER BY salary ASC ) AS MinName,
MAX( Salary ) AS MaxSalary,
MAX( name ) KEEP ( DENSE_RANK LAST ORDER BY salary ASC ) AS MaxName
FROM Employees;
Two ways:
Using Analytic function:
SQL> SELECT MIN(ename) KEEP (DENSE_RANK FIRST ORDER BY sal) min_name,
2 MIN(sal) AS min_sal,
3 MAX(ename) KEEP (DENSE_RANK LAST ORDER BY sal) AS max_name,
4 MAX(sal) AS max_sal
5 FROM emp;
MIN_NAME MIN_SAL MAX_NAME MAX_SAL
---------- ---------- ---------- ----------
SMITH 800 KING 5000
Using an In-line view:
SQL> WITH DATA AS
2 ( SELECT MIN(sal) min_sal, MAX(sal) max_sal FROM emp
3 )
4 SELECT
5 (SELECT e.ename FROM DATA t, emp e WHERE e.sal = t.min_sal AND ROWNUM =1
6 ) min_name,
7 (SELECT t.min_sal FROM DATA t, emp e WHERE e.sal = t.min_sal AND ROWNUM =1
8 ) min_sal,
9 (SELECT e.ename FROM DATA t, emp e WHERE e.sal = t.max_sal AND ROWNUM =1
10 ) max_name,
11 (SELECT t.max_sal FROM DATA t, emp e WHERE e.sal = t.max_sal AND ROWNUM =1
12 ) max_sal
13 FROM dual;
MIN_NAME MIN_SAL MAX_NAME MAX_SAL
---------- ---------- ---------- ----------
SMITH 800 KING 5000
Assuming that you are using Oracle, try this. Here first we are getting rownumber in ascending and descending order and then doing a cross join.
with employee(id,name,sal) as
(select 1,'a',1000 from dual union all
select 3,'c',1500 from dual union all
select 2,'b',2000 from dual) --temp table to recreate the scenario
, enew as(
select e.*,row_number() over (order by sal) as salasc,row_number() over (order by sal desc) as saldesc from employee e
) --temp table to find the rownumber in ascending and descending order
--original query
select * from (select id as minsalid,name as minsalempname,sal as minsal from enew
where salasc=1)
cross join
(select id as maxsalemp,name as maxsalempname,sal as maxsal from enew
where saldesc=1)
The following solution works for MySQL, which was one of the tags you originally had when you posted your question.
You can perform a CROSS JOIN of the employees table against itself to find the max name/salary with a query which finds the min name/salary.
SELECT e1.name AS MaxName, MAX(e1.sal) AS MaxSalary,
e2.name AS MinName, MIN(e2.sal) AS MinSalary
FROM employees e1 CROSS JOIN employees e2
Click the link below for a running demo. I actually include the name/salary pairs, though you can remove the names if you don't want them there.
SQLFiddle
Try something like:
select max(sal), min(sal), employee_id
from employees
group by employee_id;
After that you can join it to get the name. Maybe you can group by name and id too.
Related
I need to show the names of the oldest employee and of the last one so i need to compare datetime of the hire_date, here's how i tried and its not working:
SELECT ename
from emp
where hiredate = 'MIN(hiredate)' AND hiredate = 'MAX(hiredate)';
what should i do?
For example:
SQL> with minimax as
2 (select min(hiredate) min_hiredate,
3 max(hiredate) max_hiredate
4 from emp
5 )
6 select e.ename, e.hiredate
7 from emp e join minimax m on e.hiredate in (m.min_hiredate, m.max_hiredate);
ENAME HIREDATE
---------- ----------
SMITH 17.12.1980
ADAMS 12.01.1983
SQL>
I would recommend window functions:
select *
from (
select e.*,
rank() over(order by hire_date) rn_asc,
rank() over(order by hire_date desc) rn_desc
from emp e
) e
where 1 in (rn_asc, rn_desc)
The subquery ranks employees by increasing and decreasing hire date, the the outer query selects the top and bottom records. This allows ties, if any.
The same logic can be implemented with a window min and a window max:
select *
from (
select e.*,
min(hire_date) over() min_hire_date,
max(hire_date) over() max_hire_date
from emp e
) e
where hire_date in (min_hire_date, max_hire_date)
In Oracle, you can do this with a "simple" aggregation query:
select max(ename) keep (dense_rank first order by hiredate asc) as oldest_employee,
max(ename) keep (dense_rank first order by hiredate desc) as newest_employee
from emp;
You can also use union all if you want this information on different rows:
(select name, hiredate
from emp
order by hiredate
fetch first 1 row only
) union all
(select name, hiredate
from emp
order by hiredate desc
fetch first 1 row only
);
I have the following ORACLE query where I attempt to find the department with the highest average salary. I would like to use in-line view (i.e. retain the b dataset) for this implementation, but struggle to get the right part at the WHERE and GROUP BY components. I know the below GROUP BY and WHERE (which is non-existant) is wrong. But how do i correct them?
select a.deptno from emp a,
(select max(avg_sal) max_avg_sal from (select
avg(sal) avg_sal from emp group by deptno) ) b
group by a.deptno, b.max_avg_sal
having avg(a.sal) = b.max_avg_sal
Expected Result
deptno
10
Emp Structure
deptno staff sal
10 A 1000
10 B 1500
11 C 1100
12 D 1000
12 E 900
12 F 1000
Is this what you want?
select e.*
from (select e.*, avg(e.salary) over (partition by e.deptno) as avg_salary
from emp e
) e
order by avg_salary desc
fetch first 1 row only;
fetch first is available in Oracle 12c+. You can do similar things with an additional subquery in earlier versions.
You can use subquery
select deptno from tablename
group by deptno
having avg(sal)= (select max(asal) from (select avg(sal) as asal from tablename group by deptdno)A)
The straight-forward way is:
select deptno
from emp
group by deptno
order by avg(salary) desc
fetch first row with ties;
FETCH FIRST is available as of Oracle 12c.
In Oracle 11g we could use this instead:
select deptno
from
(
select deptno, avg(salary) as avg_salary, max(avg(salary)) over () as max_avg_salary
from emp
group by deptno
)
where avg_salary = max_avg_salary;
But you want an inline view, another word for a derived table (a subquery in the from clause). That looks way more clumsy. One example without FETCH FIRST and without window functions:
with d as
(
select deptno, avg(salary) as avg_salary
from emp
group by deptno
)
, dmax as
(
select max(avg_salary) as max_avg_salary
from d
)
select d.*
from d
join dmax on dmax.max_avg_salary = d.avg_salary;
I find this very obfuscated and don't recommend it at all. You can do the same without WITH clauses of course. Then it is even less readable.
I don't know why you'd want to write it this way, but if you really want only inline views and no windowing clauses, you can write it this way:
select b.deptno
from (SELECT deptno, avg(sal) avgsal from emp group by deptno ) b
cross join (SELECT max(avgsal) maxavgsal FROM (SELECT avg(sal) avgsal FROM emp group by deptno )) c
where b.avgsal = c.maxavgsal;
This the same thing, if you don't like CROSS JOIN for some reason:
select b.deptno
from (SELECT deptno, avg(sal) avgsal from emp group by deptno ) b
inner join ( SELECT max(avgsal) maxavgsal FROM
( SELECT avg(sal) avgsal FROM emp group by deptno ) ) c
on b.avgsal = c.maxavgsal;
To find out the Nth max sal in oracle i'm using below query
SELECT DISTINCE sal
FROM emp a
WHERE (
SELECT COUNT(DISTINCE sal)
FROM emp b
WHERE a.sal<=b.sal)=&n;
But According to me by using the above query it will take more time to execute if table size is big.
i'm trying to use the below query
SELECT sal
FROM (
SELECT DISTINCE sal
FROM emp
ORDER BY sal DESC )
WHERE rownum=3;
but not getting output.. any suggetions please .. Please share any link on how to optimise queries and decrease the time for a query to execute.
try this
select *
from
(
select
sal
,dense_rank() over (order by sal desc) ranking
from table
)
where ranking = 4 -- Replace 4 with any value of N
SELECT sal FROM (
SELECT sal, row_number() OVER (order by sal desc) AS rn FROM emp
)
WHERE rn = 3
Yes, it will take longer to execute if the table is big. But for "N-th row" queries the only way is to look through all the data and sort it. It will be definitely much faster if you have an index on sal.
SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
This will show the 3rd max salary from table employee.
If you want to find out the 5th or 6th (whatever you want) value then just change the where condition like this where rownum<=5" or "where rownum<=6 and so on...
select min(sal) from(select distinct(sal) from emp where rownum<=3 order by sal desc);
SELECT Min(sal)
FROM (SELECT DISTINCT sal
FROM emp
WHERE sal IS NOT NULL
ORDER BY sal DESC)
WHERE rownum <= n;
These queries will also work:
Workaround 1)
SELECT ename, sal
FROM Emp e1 WHERE n-1 = (SELECT COUNT(DISTINCT sal)
FROM Emp e2 WHERE e2.sal > e1.sal)
Workaround 2) using row_num function.
SELECT *
FROM (
SELECT e.*, ROW_NUMBER() OVER (ORDER BY sal DESC) rn FROM Emp e
) WHERE rn = n;
Workaround 3 ) using rownum pseudocolumn
Select MAX(SAL)
from (
Select *
from (
Select *
from EMP
order by SAL Desc
) where rownum <= n
)
The following solution works from 12c onwards:
Select min(sal) from emp where
Sal in ( select distinct (sal) from emp order by sal desc fetch first n rows only);
Replace n as per your requirement
We could write as below mentioned also.
select min(sal) from (select sal from emp where rownum=<&n order by sal desc);
In my case this Query is successfully executed (Oracle).
select salary from
(select salary, (dense_rank()
over (order by salary desc)) R
from employees)
where R='10' group by salary;
you can replace value '10' by any value of 'n'.
SELECT sal
FROM (
SELECT empno,
deptno, sal,
dense_rank( ) over ( partition by deptno order by sal desc) NRANK
FROM emp
)
WHERE NRANK = 4
SELECT *
FROM (
SELECT empno,
deptno, sal,
dense_rank( ) over ( order by sal desc) NRANK
FROM emp
)
WHERE NRANK = 4
you can replace the 2 with your desired number
select * from ( select distinct (sal),ROW_NUMBER() OVER (order by sal desc) rn from emp ) where rn=2
Refer following query for getting nth highest salary. By this way you get nth highest salary. If you want get nth lowest salary only you need to replace DESC by ASC in the query.
Now you try this you will get for sure:
SELECT DISTINCT sal
FROM emp a
WHERE (
SELECT COUNT(DISTINCT sal)
FROM emp b
WHERE a.sal<=b.sal)=&n;
For your information, if you want the nth least sal:
SELECT DISTINCT sal
FROM emp a
WHERE (
SELECT COUNT(DISTINCT sal)
FROM emp b
WHERE a.sal>=b.sal)=&n;
select min(sal) from (select distinct sal from employee order by sal DESC) where rownum<=N;
place the number whatever the highest sal you want to retrieve.
Try out following:
SELECT *
FROM
(SELECT rownum AS rn,
a.*
FROM
(WITH DATA AS -- creating dummy data
( SELECT 'MOHAN' AS NAME, 200 AS SALARY FROM DUAL
UNION ALL
SELECT 'AKSHAY' AS NAME, 500 AS SALARY FROM DUAL
UNION ALL
SELECT 'HARI' AS NAME, 300 AS SALARY FROM DUAL
UNION ALL
SELECT 'RAM' AS NAME, 400 AS SALARY FROM DUAL
)
SELECT D.* FROM DATA D ORDER BY SALARY DESC
) A
)
WHERE rn = 3; -- specify N'th highest here (In this case fetching 3'rd highest)
Cheers!
select * FROM (
select EmployeeID, Salary
, dense_rank() over (order by Salary DESC) ranking
from Employee
)
WHERE ranking = N;
dense_rank() is used for the salary has to be same.So it give the proper output instead of using rank().
SELECT TOP (1) Salary FROM
(
SELECT DISTINCT TOP (10) Salary FROM Employee ORDER BY Salary DESC
) AS Emp ORDER BY Salary
This is for 10th max salary, you can replace 10 with n.
This will also work :
with data as
(
select sal,rwid from (
select salary as sal,rowid as rwid from salary order by salary desc
)
where rownum < 5
)
select * from salary a
where rowid = (select min(rwid) from data)
select min(sal) from (select distinct(sal) from emp order by sal desc) where rownum <=&n;
Inner query select distinct(sal) from emp order by sal desc will give the below output as given below.
SAL
5000
3000
2975
2850
2450
1600
1500
1300
1250
1100
950
800
without distinct in the above query select sal from emp order by sal desc
output as given below.
SAL
5000
3000
3000
2975
2850
2450
1600
1500
1300
1250
1250
1100
950
800
outer query will give the 'N'th max sal (E.g) I have tried here for 4th Max sal and out put as given below.
MIN(SAL)
2850
Select min(salary) from (
select distinct(salary) from empdetails order by salary desc
) where rownum <=&rn
Just enter nth number which you want.
Try this:
SELECT min(sal) FROM (
SELECT sal FROM emp ORDER BY sal desc) WHERE ROWNUM <= 3; -- Replace 3 with any value of N
You can optimize the query using Dense_rank() function.
for Example :
select distinct salary from
( select salary ,dense_rank() over (order by salary desc) ranking
from Employee
)
where ranking = 6
Note: ranking 6 is the number of nth order.
select * from (select rownum as rownumber,emp1.* from (select * from emp order by sal desc) emp1) where rownumber = 3;
Try this one :
Select sal
From (Select rownum as rank, empno,ename,sal
From (Select *
From emp order by sal desc)
)
where rank=2;
Just add the number as rank which will give you nth highest salary.
SELECT MIN(Salary) salary
FROM (
SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
)
WHERE ROWNUM <= n
ORDER BY salary ASC;
-- replace "n" from line number 7 with anything you want
select MIN(salary) from (select distinct salary from employees order by SALARY DESC) WHERE ROWNUM <= 3;
5th highest salary:
SELECT
*
FROM
emp a
WHERE
4 = (
SELECT
COUNT(DISTINCT b.sal)
FROM
emp b
WHERE
a.sal < b.sal
)
Replace 4 with any value of N.
There are three methods are there...
SELECT salary,first_name,rnk
FROM (SELECT salary,first_name,rank() over (order by salary desc nulls last) as rnk from emp) where rnk=3;
SELECT salary,first_name,rnk
FROM (SELECT salary,first_name,dense_rank() over (order by salary desc nulls last) as rnk from emp) where rnk=3;
select rnk,first_name,salary
from (select rownum as rnk ,first_name,salary
from (select first_name,salary
from emp order by salary desc nulls last)) where rnk=3
Suppose I have a table employee with id, user_name, salary. How can I select the record with the 2nd highest salary in Oracle?
I googled it, find this solution, is the following right?:
select sal from
(select rownum n,a.* from
( select distinct sal from emp order by sal desc) a)
where n = 2;
RANK and DENSE_RANK have already been suggested - depending on your requirements, you might also consider ROW_NUMBER():
select * from (
select e.*, row_number() over (order by sal desc) rn from emp e
)
where rn = 2;
The difference between RANK(), DENSE_RANK() and ROW_NUMBER() boils down to:
ROW_NUMBER() always generates a unique ranking; if the ORDER BY clause cannot distinguish between two rows, it will still give them different rankings (randomly)
RANK() and DENSE_RANK() will give the same ranking to rows that cannot be distinguished by the ORDER BY clause
DENSE_RANK() will always generate a contiguous sequence of ranks (1,2,3,...), whereas RANK() will leave gaps after two or more rows with the same rank (think "Olympic Games": if two athletes win the gold medal, there is no second place, only third)
So, if you only want one employee (even if there are several with the 2nd highest salary), I'd recommend ROW_NUMBER().
If you're using Oracle 8+, you can use the RANK() or DENSE_RANK() functions like so
SELECT *
FROM (
SELECT some_column,
rank() over (order by your_sort_column desc) as row_rank
) t
WHERE row_rank = 2;
This query works in SQL*PLUS to find out the 2nd Highest Salary -
SELECT * FROM EMP
WHERE SAL = (SELECT MAX(SAL) FROM EMP
WHERE SAL < (SELECT MAX(SAL) FROM EMP));
This is double sub-query.
I hope this helps you..
WITH records
AS
(
SELECT id, user_name, salary,
DENSE_RANK() OVER (PARTITION BY id ORDER BY salary DESC) rn
FROM tableName
)
SELECT id, user_name, salary
FROM records
WHERE rn = 2
DENSE_RANK()
You should use something like this:
SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 2 )
WHERE rnum >= 2;
select * from emp where sal=(select max(sal) from emp where sal<(select max(sal) from emp))
so in our emp table(default provided by oracle) here is the output
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698 BLAKE MANAGER 7839 01-MAY-81 3000 30
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
or just you want 2nd maximum salary to be displayed
select max(sal) from emp where sal<(select max(sal) from emp)
MAX(SAL)
3000
select * FROM (
select EmployeeID, Salary
, dense_rank() over (order by Salary DESC) ranking
from Employee
)
WHERE ranking = 2;
dense_rank() is used for the salary has to be same.So it give the proper output instead of using rank().
select Max(Salary) as SecondHighestSalary from Employee where Salary not in
(select max(Salary) from Employee)
I would suggest following two ways to implement this in Oracle.
Using Sub-query:
select distinct SALARY
from EMPLOYEE e1
where 1=(select count(DISTINCT e2.SALARY) from EMPLOYEE e2 where
e2.SALARY>e1.SALARY);
This is very simple query to get required output. However, this query is quite slow as each salary in inner query is compared with all distinct salaries.
Using DENSE_RANK():
select distinct SALARY
from
(
select e1.*, DENSE_RANK () OVER (order by SALARY desc) as RN
from EMPLOYEE e
) E
where E.RN=2;
This is very efficient query. It works well with DENSE_RANK() which assigns consecutive ranks unlike RANK() which assigns next rank depending on row number which is like olympic medaling.
Difference between RANK() and DENSE_RANK():
https://oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions
I believe this will accomplish the same result, without a subquery or a ranking function:
SELECT *
FROM emp
ORDER BY sal DESC
LIMIT 1
OFFSET 2
This query helps me every time for problems like this. Replace N with position..
select *
from(
select *
from (select * from TABLE_NAME order by SALARY_COLUMN desc)
where rownum <=N
)
where SALARY_COLUMN <= all(
select SALARY_COLUMN
from (select * from TABLE_NAME order by SALARY_COLUMN desc)
where rownum <=N
);
select * from emp where sal = (
select sal from
(select rownum n,a.sal from
( select distinct sal from emp order by sal desc) a)
where n = 2);
This is more optimum, it suits all scenarios...
select max(Salary) from EmployeeTest where Salary < ( select max(Salary) from EmployeeTest ) ;
this will work for all DBs.
You can use two max function. Let's say get data of userid=10 and its 2nd highest salary from SALARY_TBL.
select max(salary) from SALARY_TBL
where
userid=10
salary <> (select max(salary) from SALARY_TBL where userid=10)
Replace N with your Highest Number
SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
Explanation
The query above can be quite confusing if you have not seen anything like it before – the inner query is what’s called a correlated sub-query because the inner query (the subquery) uses a value from the outer query (in this case the Emp1 table) in it’s WHERE clause.
And Source
I have given the answer here
By the way I am flagging this Question as Duplicate.
Syntax it for Sql server
SELECT MAX(Salary) as 'Salary' from EmployeeDetails
where Salary NOT IN
(
SELECT TOP n-1 (SALARY) from EmployeeDetails ORDER BY Salary Desc
)
To get 2nd highest salary of employee then we need replace “n” with 2 our query like will be this
SELECT MAX(Salary) as 'Salary' from EmployeeDetails
where Salary NOT IN
(
SELECT TOP 1 (SALARY) from EmployeeDetails ORDER BY Salary Desc
)
3rd highest salary of employee
SELECT MAX(Salary) as 'Salary' from EmployeeDetails
where Salary NOT IN
(
SELECT TOP 2 (SALARY) from EmployeeDetails ORDER BY Salary Desc
)
SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM EMP WHERE SAL<(SELECT MAX(SAL) FROM EMP));
(OR)
SELECT ENAME ,SAL FROM EMP ORDER BY SAL DESC;
(OR)
SELECT * FROM(SELECT ENAME,SAL ,DENSE_RANK()
OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) R FROM EMP) WHERE R=2;
select salary from EmployeeDetails order by salary desc limit 1 offset (n-1).
If you want to find 2nd highest than replace n with that 2.
How to find top three highest salary in emp table in oracle?
SELECT *FROM
(
SELECT *FROM emp
ORDER BY Salary desc
)
WHERE rownum <= 3
ORDER BY Salary ;
You can try.
SELECT * FROM
(
SELECT EMPLOYEE, LAST_NAME, SALARY,
RANK() OVER (ORDER BY SALARY DESC) EMPRANK
FROM emp
)
WHERE emprank <= 3;
This will give correct output even if there are two employees with same maximun salary
Something like the following should do it.
SELECT Name, Salary
FROM
(
SELECT Name, Salary
FROM emp
ORDER BY Salary desc
)
WHERE rownum <= 3
ORDER BY Salary ;
SELECT a.ename, b.sal
FROM emp a, emp b
WHERE a.empno = b.empno
AND
3 > (SELECT count(*) FROM emp b
WHERE a.sal = b.sal);
Without using TOP, ROWID, rank etc. Works with oldest sql also
You could use DBMS_STAT_FUNCS.Summary:
SET SERVEROUTPUT ON;
DECLARE
s DBMS_STAT_FUNCS.SummaryType;
BEGIN
DBMS_STAT_FUNCS.SUMMARY('HR', 'EMPLOYEES', 'SALARY',3, s);
DBMS_OUTPUT.put_line('Top 3: ' || s.top_5_values(1) || '-'
|| s.top_5_values(2) || '-' || s.top_5_values(3));
END;
/
Output:
Top 3: 24000-17000-17000
SELECT *
FROM emp
ORDER BY Salary desc
FETCH FIRST 3 ROWS ONLY;
SELECT * FROM
(
SELECT EMPLOYEE, LAST_NAME, SALARY,
DENSE_RANK() OVER (ORDER BY SALARY DESC) EMPRANK
FROM emp
)
WHERE emprank <= 3;
Select ename, job, sal from emp
where sal >=(select max(sal) from emp
where sal < (select max(sal) from emp
where sal < (select max(sal) from emp)))
order by sal;
ENAME JOB SAL
---------- --------- ----------
KING PRESIDENT 5000
FORD ANALYST 3000
SCOTT ANALYST 3000
Limit The Query To Display Only The Top 3 Highest Paid Employees. : Query « Oracle PL / SQL
create table employee(
emp_no integer primary key
,lastname varchar2(20) not null
,salary number(3)
);
insert into employee(emp_no,lastname,salary)
values(1,'Tomy',2);
insert into employee(emp_no,lastname,salary)
values(2,'Jacky',3);
insert into employee(emp_no,lastname,salary)
values(3,'Joey',4);
insert into employee(emp_no,lastname,salary)
values(4,'Janey',5);
select lastname, salary
from (SELECT lastname, salary FROM employee ORDER BY salary DESC)
where rownum <= 3 ;
OUTPUT
LASTNAME SALARY
-------------------- ----------
Janey 5
Joey 4
Jacky 3
drop table employee;
SELECT salary,first_name||' '||last_name "Name of the employee"
FROM hr.employees
WHERE rownum <= 3
ORDER BY salary desc ;
select empno,salary from emp e
where 3 > ( Select count(salary) from emp
where e.salary < salary )
Another way :
select * from
(
select empno,salary,
Rank() over(order by salary desc) as rank from emp )
where Rank <= 3;
Another Way :
select * from
(
select empno,salary from emp
order by salary desc
)
where rownum <= 3;
SELECT Name, Salary
FROM
(
SELECT Name, Salary
FROM emp
ORDER BY Salary desc
)
WHERE rownum <= 3
ORDER BY Salary ;
select top(3) min(Name),TotalSalary,ROW_NUMBER() OVER (Order by TotalSalary desc) AS RowNumber
FROM tbl_EmployeeProfile group by TotalSalary
SELECT * FROM
(
SELECT ename, sal,
DENSE_RANK() OVER (ORDER BY SAL DESC) EMPRANK
FROM emp
)
emp1 WHERE emprank <=5
The easiest way to find any given highest salary
This will give you the 3rd largest salary:
select * from emp
order by sal desc
limit 2,1;
Here, (limit n,m) n represents the start position,
and the m represents how many outputs to display after the start position
If you change the values of n,m you will get the output accordingly.
Eg: (limit 3,1) will give you the 4th largest salary.
SELECT DISTINCT(salary) FROM emp order by salary asc limit 0 ,3
Above query gives three highest salary with DISTINCT.
SELECT * FROM Employees
WHERE rownum <= 3
ORDER BY Salary ;
solution for to find top 5 salary in sq l server
select top(1) name, salary from salary where salary in(select distinct top(3) salary from salary order by salary disc)
select top 3 * from emp order by sal desc