Count id_employee in another column - sql

Could someone help me how to count the number of times an employee
"id" is next to the another employee in the "executive" column?
In the first picture I am sending the table and the results I want. In the second picture the result I have
SELECT id_employee, lastname, COUNT(id_employee)
FROM employees
WHERE id_employee IN (SELECT executive FROM employees)
GROUP BY lastname, id_employee;
1
2

You need a self-join:
select iemp.d_employee, emp.last_name, subordinates
from employee as emp
join
( -- count per executive
select executive, count(*) as subordinates
from employee
group by executive
) as emp_count
on emp.id_employee = emp_count.executive
When you switch to an Outer Join you can include employees which are not executives:
select id_employee, last_name,
coalesce(subordinates, 0) -- to get zero instead of NULL
from employee as emp
left join
( -- count per executive
select executive, count(*) as subordinates
from employee
group by executive
) as emp_count
on emp.id_employee = emp_count.executive

You can try below query, simple and sweet.
SELECT * INTO #EMPLOYEE FROM
(SELECT 1001 as ID_EMP,'JAMES' as LASTNAME,NULL as EXECUTIVE
UNION
SELECT 1002 as ID_EMP,'JENSEN' as LASTNAME,1001 as EXECUTIVE
UNION
SELECT 1003 as ID_EMP,'RUDY' as LASTNAME,1001 as EXECUTIVE
UNION
SELECT 1004 as ID_EMP,'CZARNY' as LASTNAME,1002 as EXECUTIVE
UNION
SELECT 1005 as ID_EMP,'RAMBUS' as LASTNAME,1002 as EXECUTIVE
UNION
SELECT 1006 as ID_EMP,'ADAM' as LASTNAME,1003 as EXECUTIVE
)A
SELECT ID_EMP,A.COUNT FROM (SELECT EXECUTIVE,COUNT(*) AS COUNT FROM #EMPLOYEE GROUP BY EXECUTIVE)A LEFT JOIN #EMPLOYEE E
ON E.ID_EMP = A.EXECUTIVE

Related

how to retrieve the employee who got maximum bonus?

I have to display the employee detail who has got the maximum bonus(one with employee details and another table with bonus details). Here I have created a 'performance bonus' column to sum up the multiple bonuses. How to retrieve the employee from that column?
select e.Employee_id,
e.First_name,
e.Department,
e.Salary,
coalesce((select sum(b.Bonus_Amount)
as Bonus-- Let's sum up all Employee's the bonuses
from Employee_Bonus_Table b
where b.Employee_ref_id = e.Employee_Id), 0) [Performance_bonus]
from Employee_Table e
If I understood the task correctly, then the bonuses can be repeated, is that right?
then we must first sum up all the bonuses by employee, then sort from largest to smallest and get the first one from the list
--- for examples:
with Employee_Bonus_Table as(
select Bonus_Amount = 1 ,Employee_ref_id = 1
union select Bonus_Amount = 1000 ,Employee_ref_id = 2
union select Bonus_Amount = 2000 ,Employee_ref_id = 2
),Employee_Table as (
select Employee_id=1
,First_name='First_name'
,Department= 'Department'
,Salary = 1000
UNION select Employee_id=2
,First_name='First_name2'
,Department= 'Department2'
,Salary = 2000
)
--reslut query:
select top 1
e.Employee_id,
e.First_name,
e.Department,
e.Salary,
b.sumBonus_Amount
from Employee_Table e
join (select sumBonus_Amount = sum(Bonus_Amount), Employee_ref_id
from Employee_Bonus_Table
group by Employee_ref_id
) b on b.Employee_ref_id = e.Employee_Id
order by b.sumBonus_Amount desc

SQL: Trying to find total of one column and who has the highest value in that column

Link of tables:
https://docs.google.com/document/d/1df4zUTI6e5Rw8mJxZKkOWLBuRYzjAOPkkf8zsC_2mRo/edit?usp=sharing
I'm trying to write a query that displays the total (sum) of all the salaries in the salary column and the name of the individual who has the highest salary.
I used:
select name as highest_paid,sum(salary) as total_salary
from uscis,employer
where uscis.alienno=employer.alienno and salary=(select max(salary) from employer) group by name
I did get the result of name of the highest salary paid but i did not get the sum of the salary columns. I instead got 280,000 which is the highest salary.
My apologies in advance if I worded this question poorly.
So you do not want just the one persons salary? You want to sum the whole salary column from the Employer table? Assuming a 1:1 relationship between USCIS table and Employer, try this.
SELECT name as highest_paid, sum(salary) OVER() as everybodys_total_salary
FROM uscis
INNER JOIN employer
on uscis.alienno=employer.alienno
ORDER BY salary DESC
FETCH FIRST ROW ONLY
Description of oracle sum as an analytic function found here https://oracle-base.com/articles/misc/sum-analytic-function
Want to get the sum(salary) and max(salary) together?
with total as (
select sum(salary) as sum_salary from employer
), max as (
select a.name, b.salary from uscis a, employer b where a.alienno = b.alienno and b.salary = (select max(salary) from employer)
)
select * from max, total;
look at the dbfiddle
Use SUM as an analytic function and the ORDER BY the salary in DESCending order and get only the first row:
SELECT name as highest_paid,
SUM(salary) OVER () as total_salary
FROM uscis
INNER JOIN employer
ON ( uscis.alienno=employer.alienno )
ORDER BY salary DESC
FETCH FIRST ROW WITH TIES;
-- or FETCH FIRST ROW ONLY if you only ever want one row.
So, for your test data:
CREATE TABLE uscis ( AlienNo, Name, Nationality, City ) AS
SELECT 'A1023', 'Jeff', 'India', 'Atlanta' FROM DUAL UNION ALL
SELECT 'A1024', 'David', 'China', 'NY' FROM DUAL UNION ALL
SELECT 'A1025', 'Mark', 'UK', 'Charlotte' FROM DUAL UNION ALL
SELECT 'A2050', 'Shown', 'Germany', 'Astoria' FROM DUAL;
CREATE TABLE employer ( AlienNo, SSN, Dept, Salary ) AS
SELECT 'A1023', '111-22-4567', 'EE', 280000 FROM DUAL UNION ALL
SELECT 'A1024', '333-32-8767', 'CS', 180000 FROM DUAL UNION ALL
SELECT 'A1025', '444-45-3454', 'CE', 140000 FROM DUAL UNION ALL
SELECT 'A2050', '234-34-2234', 'ME', 180.000 FROM DUAL;
This outputs:
HIGHEST_PAID | TOTAL_SALARY
:----------- | -----------:
Jeff | 600180
db<>fiddle here

Select all the people that have the same salary

I have problems finding a solution to an SQL query. This is probably a very obvious beginner question but I can't seem to get the results that I'm after. I have a table that looks something like the following:
|Name |Station|Salary|
|Bob |1 |2000 |
|Steve|2 |1750 |
|Mark |3 |2050 |
|Lisa |4 |2200 |
|Hans |5 |2000 |
I would like to select the names of the people in this table that have the same salary. The result should of course be Bob and Hans.
If you join the table against itself on Salary but where the names are separate then this should give you any matching salaried people:
SELECT s1.Name, s1.Station, s1.Salary
FROM Staff s1
INNER JOIN Staff s2 ON s1.Salary = s2.Salary AND s1.Name <> s2.Name
Here's a SQLFiddle to show it in action
SELECT Name
FROM table1
WHERE Salary IN (
SELECT Salary
FROM table1
GROUP BY Salary
HAVING COUNT(*) > 1
)
If you determine the salary bands which have more than one employee, you can then join back to it as a derived table:
SELECT e.Name, e.Station
FROM Employee e
INNER JOIN
(
SELECT Salary
FROM Employee
GROUP BY Salary
HAVING COUNT(*) > 1
) grp ON e.Salary = grp.Salary;
SqlFiddle here
Most databases support window functions. The simple method to do this is to count the number of people that have a given salary. Then choose those people. Here is an easy method:
select t.*
from (select t.*, count(*) over (partition by salary) as salarycnt
from table t
) t
where salarycnt > 1
order by salary;
TRY THIS:
SELECT E1.Name, E1.Salary
FROM Employee E1, Employee E2
WHERE E1.Salary = E2.Salary
AND E1.Name <> E2.Name
http://sqlfiddle.com/#!2/1e34b
Use below Query:
with datatab as
(
select 'Bob' Name, 1 Station, 2000 Salary from dual union
select 'Steve' Name, 2 Station, 1750 Salary from dual union
select 'Mark' Name, 3 Station, 2050 Salary from dual union
select 'Lisa' Name, 4 Station, 2200 Salary from dual union
select 'Hans' Name, 5 Station, 2000 Salary from dual union
select 'Test' Name, 6 Station, 1750 Salary from dual
)
SELECT NAME, sTATION, SALARY FROM DATATAB
WHERE SALARY IN
(
SELECT Salary
FROM datatab
GROUP BY Salary
HAVING COUNT(1) > 1
);
As someone suggested in Edits, The Query would be:
SELECT NAME, sTATION, SALARY FROM TABLE_NAME
WHERE SALARY IN
(
SELECT Salary
FROM TABLE_NAME
GROUP BY Salary
HAVING COUNT(1) > 1
)
Select a.name, a.salary from
(
Select count(salary) as cnt, salary from staff group by salary having count(salary) > 1
)as X
Inner join staff a on a.Salary=x.salary
order by a.salary:
Use aliases when you have to put one and the same table two or more times:
select t1.Name,
t2.Name,
t1.Salary
from MyTable t1,
MyTable t2
where t1.Station > t2.Station and -- if Station is id
-- t1.Name > t2.Name -- if Name is in fact an id
t1.Salary = t2.Salary
Try this:
select COUNT(*) as NumberOfPerson,salary from tblEmployee group by salary having COUNT(*)>1
For MySQL:
Select * from worker
where salary in (select salary from worker
group by salary
having count(1)>1);
lets say table name is: employee
select distinct e.name, e.salary from employee e, employee e1 where
e.salary = e1.salary and e.name != e1.name;
First Solution :
Select e1.name from employee e1
inner join employee e2 on e1.salary=e2.salary AND e1.name <> e2.name
Second easy solution:
Select name from employee
where salary In (Select salary from employee
group by salary
having count(*)>1)
SUPPOSE WE HAVE TABLE NAMELY AS EMP3 WHICH ONTEIN COLUMNS AS FIRST_NAME,LAST_NAME,SALRY,DEPARTMENT_ID,COMMISSION ETC.
NOW WE HAVE TO SELECT RECORDS OF EMPLOYEES WHO HAVE THE SAME SALARY.
LET'S GET OUTPUT WITH THE HELP OF SELF JOIN.
HERE WE ARE JOINING THE SAME TABLE WITH ITSELFMTO GET RECORDS WHO HAVE THE SAME SALARY.
SELECT E1.FIRST_NAME AS FN,E1.SALARY FROM EMPLOYEES E1 INNER JOIN EMPLOYEES E2 ON E1.SALARY=E2.SALARY;
Treating same table as a separate two table.
SELECT DISTINCT T.username, T.salary
FROM account T, account T
WHERE T.salary = T.salary
AND T.username <> T.username
ORDER BY salary;
Do you want the quantity of people with the same salary.
SELECT count(*) FROM table GROUP BY salary

ALL keyword in Oracle SQL

I want the empno who is working on at least all the same projects on which empno 101 is working.
I tried following query but failed:
SELECT EMPNO
FROM EMPLOYEE
WHERE PROJECTNO= ALL(SELECT PROJECTNO
FROM EMPLOYEE
WHERE EMPNO=101);
Empno 101 IS working on comp134 and comp90
and empno 103 is also working on both these projects
but I get answer as no rows selected for following table.
projectno empno
--------- ------
comp134 101
comp90 101
comp90 103
comp14 104
comp213 103
comp134 103
comp14 108
comp90 104
For Exact Match:
SELECT EMPNO
FROM EMPLOYEE E1
WHERE EXISTS
(
SELECT 'x' FROM EMPLOYEE E2
WHERE E2.EMPNO=101 AND E1.PROJECTNO = E2.PROJECTNO
)
MINUS
SELECT EMPNO
FROM EMPLOYEE E1
WHERE NOT EXISTS
(SELECT 'x' FROM EMPLOYEE E2
WHERE E2.EMPNO=101 AND E1.PROJECTNO = E2.PROJECTNO)
For Atleast All of it Also For Exact Match
SELECT EMPNO
FROM EMPLOYEE e
JOIN (SELECT PROJECTNO,count(1) OVER () AS ct
FROM EMPLOYEE
WHERE EMPNO=101) my_list
ON (e.PROJECTNO = my_list.PROJECTNO AND e.EMPNO <> 101)
GROUP BY EMPNO
HAVING count(*) = MAX(my_list.ct)
This sounds like a kind of Relational Division:
with cte as
( select PROJECTNO
from EMPLOYEE
WHERE EMPNO=101
)
SELECT EMPNO
FROM EMPLOYEE as e
join cte
on e.PROJECTNO = cte.PROJECTNO
group by EMPNO
HAVING count(*)
= (select count(*) from cte)
try this
select `projectno` from
( select `projectno`, count(`empno`) as counts from EMPLOYEE
group by `empno`
order by counts desc
limit 1)t ;
or even simpler like that
select `projectno`
from EMPLOYEE
group by `empno`
order by count(`empno`) desc
limit 1
DEMO HERE

Highest Salary in each department

I have a table EmpDetails:
DeptID EmpName Salary
Engg Sam 1000
Engg Smith 2000
HR Denis 1500
HR Danny 3000
IT David 2000
IT John 3000
I need to make a query that find the highest salary for each department.
SELECT DeptID, MAX(Salary) FROM EmpDetails GROUP BY DeptID
The above query is the accepted answer but it will not work for the following scenario. Let's say we have to find the employees with the highest salary in each department for the below table.
DeptID
EmpName
Salary
Engg
Sam
1000
Engg
Smith
2000
Engg
Tom
2000
HR
Denis
1500
HR
Danny
3000
IT
David
2000
IT
John
3000
Notice that Smith and Tom belong to the Engg department and both have the same salary, which is the highest in the Engg department. Hence the query "SELECT DeptID, MAX(Salary) FROM EmpDetails GROUP BY DeptID" will not work since MAX() returns a single value. The below query will work.
SELECT DeptID, EmpName, Salary FROM EmpDetails
WHERE (DeptID,Salary) IN (SELECT DeptID, MAX(Salary) FROM EmpDetails GROUP BY DeptID)
Output will be
DeptID
EmpName
Salary
Engg
Smith
2000
Engg
Tom
2000
HR
Danny
3000
IT
John
3000
Assuming SQL Server 2005+
WITH cteRowNum AS (
SELECT DeptID, EmpName, Salary,
DENSE_RANK() OVER(PARTITION BY DeptID ORDER BY Salary DESC) AS RowNum
FROM EmpDetails
)
SELECT DeptID, EmpName, Salary
FROM cteRowNum
WHERE RowNum = 1;
If you want to show other parameters too along with DeptId and Salary like EmpName, EmpId
SELECT
EmpID
, Name,
, Salary
, DeptId
FROM Employee
where
(DeptId,Salary)
in
(select DeptId, max(salary) from Employee group by DeptId)
SELECT empName,empDept,EmpSalary
FROM Employee
WHERE empSalary IN
(SELECT max(empSalary) AS salary
From Employee
GROUP BY EmpDept)
Select empname,empid,Sal,DeptName from
(Select e.empname,e.empid,Max(S.Salary) Sal,D.DeptName, ROW_NUMBER() Over(partition by D.DeptName order by s.salary desc) Rownum
from emp e inner join Sal S
on e.empid=s.empid
inner join Dept d on e.Deptid=d.Deptid
group by e.empname,e.empid,D.DeptName,s.Salary
) x where Rownum = 1
This will work if the department, salary and employee name are in the same table.
select ed.emp_name, ed.salary, ed.dept from
(select max(salary) maxSal, dept from emp_dept group by dept) maxsaldept
inner join emp_dept ed
on ed.dept = maxsaldept.dept and ed.salary = maxsaldept.maxSal
Is there any better solution than this?
ermn, something like:
select
d.DeptID,
max(e.Salary)
from
department d
inner join employees e on d.DeptID = e.DeptID
group by
d.DeptID
WITH cteRowNum AS (
SELECT DeptID, EmpName, Salary,
ROW_NUMBER() OVER(PARTITION BY DeptID ORDER BY Salary DESC) AS RowNum
FROM EmpDetails
)
SELECT DeptID, EmpName, Salary,Rownum
FROM cteRowNum
WHERE RowNum in(1,2);
SELECT Employee_ID
, First_name
, last_name
, department_id
, Salary
FROM (SELECT Employee_ID
, First_name
, last_name
, department_id
, Salary
, MAX(salary) OVER (PARTITION BY department_id) dept_max_sal
FROM EMPLOYEES) AS Emp
WHERE salary = dept_max_sal;
Use following command;
SELECT A.*
FROM #EmpDetails A
INNER JOIN ( SELECT DeptID ,
MAX(salary) AS salary
FROM #EmpDetails
GROUP BY DeptID
) B ON A.DeptID = B.DeptID
AND A.salary = B.salary
ORDER BY A.DeptID
SELECT DeptID, MAX(Salary)
FROM EmpDetails
GROUP BY DeptID
This query will work fine, but the moment if you want to fetch some others details related to the employee having the highest salary will contradict.
You can use :
SELECT DepatID, a , b, c
FROM EmpDetails
WHERE Salary IN (
SELECT max(Salary)
FROM EmpDetails
GROUP BY DeptID
);
if you will use the previous query it will only reflects the records of the min val except the salary as you have used the max function.
SELECT
DeptID,
Salary
FROM
EmpDetails
GROUP BY
DeptID
ORDER BY
Salary desc
***
> /*highest salary by each dept*/
***
select d.Dept_Name,max(e.salary)
from emp_details as e join Dept_Details as d
on e.d_id=d.Dept_Id
group by d.Dept_Name
select distinct e.d_id,d.Dept_Name
from emp_details as e join Dept_Details as d
on e.d_id=d.Dept_Id
select e.salary,d.Dept_Name,d.Dept_Id
from emp_details as e join Dept_Details as d
on e.d_id=d.Dept_Id
/////simplest query for max salary dept_wise////
Use the below quesry:
select employee_name,salary,department_id from emp where salary in(select max(salary) from emp group by department_id);
select empno
from EMP e
where salary=(select max(sal)
from EMP w
where groupby w.deptno having e.deptno=w.deptno)
I hope it will work...
Use correlated subquery:
SELECT DeptID, EmpName, Salary
FROM EmpDetails a
WHERE Salary = (SELECT MAX(Salary)
FROM EmpDetails b
WHERE a.DeptID = b.DeptID)
This is the best possible solution for ORACLE:
Select * from (select customerid, city, freight,
row_number() over (partition by customerid order by freight desc) Row_Number from
(select orders.orderId, customers.CUSTOMERID, customers.city, orders.FREIGHT from orders inner join customers on orders.customerid = customers.customerid where customers.country='Germany' order by customers.customerid, orders.freight desc)
order by customerid, freight desc) where Row_Number<=2;
Notice here I have used partition by clause for marking row number, this is majorly because we need to partition the records grouping them according to customer id. I have used two inner queries here. The inner most query is to give a view which is sorted according to customer ID and decreasing order of cost. Now from that we need to obtain always top two records so firstly we need to name them and then we need to filter them according to rownum. Second level query is to mark rownum according to customer ID. And final query will filter the result according to rownum. For every partition.
select deptid, empname, salary from
(Select deptid, empname,salary,
rank() Over(Partition by deptid order by salary desc)as rank from
EmpDetails) emp
where emp.rank = 1
First ranks each employee by salary in descending order having highest
rank 1 and then selects only deptid, empname, salary. You can do this for
all Nth member of the group.
SELECT empname
FROM empdetails
WHERE salary IN(SELECT deptid max(salary) AS salary
FROM empdetails
group by deptid)
select a.*
from EmpDetails a
inner join
(
select DeptID,max(Salary) as Salary
from EmpDetails group by DeptID
)b
on a.DeptID = b.DeptID and a.Salary = b.Salary
Here is a way to get maximum values and names on any version of SQL.
Test Data:
CREATE TABLE EmpDetails(DeptID VARCHAR(10), EmpName VARCHAR(10), Salary DECIMAL(8,2))
INSERT INTO EmpDetails VALUES('Engg','Sam',1000)
INSERT INTO EmpDetails VALUES('Engg','Smith',2000)
INSERT INTO EmpDetails VALUES('HR','Denis',1500)
INSERT INTO EmpDetails VALUES('HR','Danny',3000)
INSERT INTO EmpDetails VALUES('IT','David',2000)
INSERT INTO EmpDetails VALUES('IT','John',3000)
Example:
SELECT ed.DeptID
,ed.EmpName
,ed.Salary
FROM (SELECT DeptID, MAX(Salary) MaxSal
FROM EmpDetails
GROUP BY DeptID)AS empmaxsal
INNER JOIN EmpDetails ed
ON empmaxsal.DeptID = ed.DeptID
AND empmaxsal.MaxSal = ed.Salary
Not the most elegant, but it works.
SELECT D.DeptID, E.EmpName, E.Salary
FROM Employee E
INNER JOIN Department D ON D.DeptId = E.DeptId
WHERE E.Salary IN (SELECT MAX(Salary) FROM Employee);
select * from (
select a.* from EmpDetails a
right join (select DeptID,max(salary) as Salary from EmpDetails group by DeptID) b
on b.DeptID=a.DeptID and b.salary=a.salary ) as c group by c.DeptID;
The below query will display employee name with their respective department name in which that particular employee name is having highest salary.
with T as
(select empname, employee.deptno, salary
from employee
where salary in (select max(salary)
from employee
group by deptno))
select empname, deptname, salary
from T, department
where T.deptno=department.deptno;
I executed the above query successfully on Oracle database.
If you just want to get the highest salary from that table, by department:
SELECT MAX(Salary) FROM TableName GROUP BY DeptID
IF you want Department and highest salary, use
SELECT DeptID, MAX(Salary) FROM EmpDetails GROUP BY DeptID
if you want more columns in employee and department, use
select Department.Name , emp.Name, emp.Salary from Employee emp
inner join (select DeptID, max(salary) [salary] from employee group by DeptID) b
on emp.DeptID = b.DeptID and b.salary = emp.Salary
inner join Department on emp.DeptID = Department.id
order by Department.Name
if use salary in (select max(salary...)) like this, one person have same salary in another department then it will fail.
The below listed query will list highest salary in each department.
select deptname, max(salary) from department, employee where
department.deptno=employee.deptno group by deptname;
I executed this query successfully on Oracle database.
with ctesal as (
select DepartmentId , Name , Salary, ROW_Number() OVER (partition by DepartmentId
order by Salary desc) as RowNum
from dbo.Employee
)
select DepartmentId , Name , Salary , RowNum from ctesal where RowNum =2;
This is applicable to SQL server.
ROW_Number is a inbuilt function in SQL server .It gives count starting from 1 based on partition by and order by clause. At the end, We can write where condition based on our requirements.
I have like 2 approaches using one with Rank and the other with ROW_NUMBER
This is my sample data
Age Name Gender Salary
----------- -------------------------------------------------- ---------- -----------
1 Mark Male 5000
2 John Male 4500
3 Pavan Male 5000
4 Pam Female 5500
5 Sara Female 4000
6 Aradhya Female 3500
7 Tom Male 5500
8 Mary Female 5000
9 Ben Male 6500
10 Jodi Female 7000
11 Tom Male 5500
12 Ron Male 5000
13 Ramani Female 7000
So here is my first query to find max salary and the person with that max salary for each Gender
with CTE as(
select RANK() over(partition by Gender Order by Salary desc) as [Rank],* from employees)
select * from CTE where [Rank]=1
Rank Age Name Gender Salary
-------------------- ----------- -------------------------------------------------- ---------- -----------
1 10 Jodi Female 7000
1 13 Ramani Female 7000
1 9 Ben Male 6500
So in this case, we can see there is a tie between these 2 female employees "Jodi" and "Ramani". In that case, As a tie-breaker I want to make use of Age as a deciding factor and person with more age is supposed to be displayed
with CTE as(
select RANK() over(partition by Gender Order by Salary desc,age desc) as [Rank],* from employees)
select * from CTE where [Rank]=1
Rank Age Name Gender Salary
-------------------- ----------- -------------------------------------------------- ---------- -----------
1 13 Ramani Female 7000
1 9 Ben Male 6500
Usually, in this case for finding the highest salary, it doesn't make much difference even if
Rank, Dense_Rank, or Row_Number() are used. But they have some impact in other cases.
Thank you #JoeStefanelli for his answer (https://stackoverflow.com/a/8477083/4691279). He provided SQL Server 2005+ version and I used the same to create the Oracle version:
WITH cteRowNum(dep_id, emp_id, Salary, RowNums) AS (
SELECT dep_id, emp_id, Salary,
DENSE_RANK() OVER(PARTITION BY dep_id ORDER BY Salary DESC) AS RowNums
FROM employee
)
SELECT cteRowNum.dep_id, cteRowNum.emp_id, cteRowNum.Salary
FROM cteRowNum
WHERE cteRowNum.RowNums = 1;
You can test this using livesql.oracle.com, below are my DDLs and DMLs you can use:
create table employee (
emp_id varchar2(50) NOT NULL,
dep_id varchar2(50) NOT NULL,
salary number not null
);
create table department (
dep_id varchar2(50) NOT NULL,
dep_name varchar2(50) NOT NULL
);
insert into employee values (100, 5000, 1000000);
insert into employee values (200, 5000, 2000000);
insert into employee values (300, 5000, 3000000);
insert into employee values (400, 6000, 1500000);
insert into employee values (500, 6000, 1500000);
insert into employee values (600, 7000, 1000000);
insert into employee values (700, 7000, 1000000);
insert into employee values (800, 7000, 2000000);
insert into department values (5000, 'dep 1');
insert into department values (6000, 'dep 2');
insert into department values (7000, 'dep 3');
And below is the success screenshot of the query: