SQL Query to get the next sal in same line - sql

Employee Salary Department
A 1000 IT
B 2000 IT
C 3000 IT
D 4000 HR
E 2000 HR
F 1500 IT
G 7000 HR
Write a query to get results like below -
Employee Salary Next highest salary(in same department)
Department
A 1000 1500 IT
B 1500 2000 IT
.
.
E 2000 4000 HR
D 4000 None HR

select employee, salary
, lead(salary) over(partition by department order by salary) as next_salary
, department
from table

Related

With Join And Window Function Getting Result

A Table Dept
Id Salary Dept
1 1000 A
2 2000 B
3 5000 A
4 2500 C
5 3000 D
So the Output
Id Salary Dept
1 6000 A
Need To Get the Sum of Salary and Which department as the max salary using Join or Window function
You seem to want:
select d.dept, sum(d.salary)
from dept d
group by d.dept
order by max(d.salary) desc
fetch first 1 row only
If you are using MSSQL you can try this
select top 1 d.Dept, Sum(d.Salary) as Salary from Dept d group by d.Dept order by Sum(d.Salary) desc

what does count(*) do exactly? [duplicate]

This question already has answers here:
What is the difference between count(0), count(1).. and count(*) in mySQL/SQL?
(9 answers)
Closed 3 years ago.
I use oracle 11g. I want maximum and minimum salary, and count of employees for each department, so I do this :
select d.department_id, d.department_name, max(salary), min(salary), count(*)
from employees e ,
departments d
where e.department_id = d.department_id
group by d.department_id, d.department_name;
and it works :
DEPARTMENT_ID DEPARTMENT_NAME MAX(SALARY) MIN(SALARY) COUNT(*)
------------- ------------------------------ ----------- ----------- ----------
100 Finance 12008 6900 6
50 Shipping 8200 2100 45
70 Public Relations 10000 10000 1
30 Purchasing 11000 2500 6
90 Executive 24000 17000 3
10 Administration 4400 4400 1
110 Accounting 12008 8300 2
40 Human Resources 6500 6500 1
20 Marketing 13000 6000 2
60 IT 9000 4200 5
80 Sales 14000 6100 34
11 rows selected.
So what does count(*) mean? Count departments or what ?
count(*) returns the rows that match the where statement.
In your case its where e.department_id=d.department_id.
Because you applied a group by your count(*) will return the count of rows for each group (d.department_id).
This means you will get the count of employees (e.department_id) for each department (d.department_id).

ORA-00923: FROM keyword not found where expected (Retrieve department wise highest 2 salaries)

I was trying to retrieve the highest two salaries by each department. But I am getting error! Please help, I will be appreciated, Thanks.
CREATE TABLE emp_h ( Dept varchar(100), Emp varchar(100), Sal numeric(18,2) )
Values are inserted in this table below.
SELECT * FROM emp_h
DEPT EMP SAL
Comp A 6000
Comp B 7000
Comp C 10000
Comp D 9000
Elec P 2000
Elec Q 10000
Elec R 11000
Eng AA 15000
Eng BB 2000
Eng BB 7000
Eng BB 3000
I want the output (Department wise top 2 highest salary)
Dept Emp Sal
----------------------------
Comp C 10000
Comp D 9000
Elec R 11000
Elec Q 10000
Eng AA 15000
Eng BB 7000
Now I ran this query to retrieve the highest 2 salary each department wise:
SELECT * FROM(SELECT RANK() OVER (PARTITION BY DEPT ORDER BY SAL DESC) AS 'Rank', * FROM emp_h) AS A WHERE RANK <= 2
Result:
ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"
*Cause:
*Action:
Error at Line: 25 Column: 61
Oracle likes you to qualify * if there are any other columns on the row. So try this:
SELECT e.*
FROM (SELECT RANK() OVER (PARTITION BY DEPT ORDER BY SAL DESC) AS rnk, e.*
FROM emp_h e
) e
WHERE rnk <= 2;

SQL Query to fetch the name who are getting more than the average salary of employees working in IT and OPERATION

Name Salary Department
A 9568 IT
B 7854 IT
C 8564 IT
D 4875 IT
E 5895 HR
F 8858 HR
G 9858 HR
H 11852 FINANCE
I 7582 OPERATION
J 5892 OPERATION
K 12850 FINANCE
L 5265 STORE
M 3582 STORE
N 8520 FINANCE
Write a SQL query to fetch the names whose salary is greater than average salary of people working in department IT and OPERATION
SET #value = AVERAGE(Salary) FROM YOURTABLE WHERE department in ('OPERATION', 'IT')
SELECT * FROM YOURTABLE WHERE Salary > #value;
I think that should work. Replace YOURTABLE with your table's name.

Department names(with employee name) with more than 2 employee and salary greater than 90% of respective department average salary

I am trying to generate a SQL query to find the Department names(with employee name) with more than 2 employee whose salary greater than 90% of respective department average salary. My SQL Code is working fine , it has no Syntax errors but the output is giving me additional data. The table is as follos
JONES ACCOUNTING 3000
STEEL ACCOUNTING 2500
WILSON RESEARCH 3000
WOLFE RESEARCH 2500
LEE RESEARCH 2400
LANCASTER SALES 2000
JACKSON SALES 2500
FISHER SALES 3000
ADAMS IT 2000
MILLER IT 1000
SCOTT IT 2500
SMITH IT 2900
KING EXECUTIVE 5000
JOST EXECUTIVE 4500
CLARK EXECUTIVE 4000
My code is as follows.
Select department_name , employee_name
from department d , employee e
where e.department_id = d.department_id
and (SELECT COUNT(*)
FROM Employee E
WHERE E.department_ID = D.department_ID) > 2
and salary >
0.9*(SELECT ROUND(AVG(salary),2)
FROM employee e_inner
WHERE e_inner.department_id = e.department_id);
I notice that my code returns the value of department with more than 2 employees and salary > 90% of department's average salary. whereas I am looking for departments with more than 2 employees whose salary is more than 90% of department avg salary
I think this should do it:
select *
from (
select department_name,
employee_name,
sum(case when salary > avg_dept_sal * 0.9 then 1 else 0 end) over (partition by department_id) as greater_count
from (
select d.department_name,
e.department_id,
e.employee_name,
e.salary,
count(*) over (partition by e.department_id) as dept_count,
avg(salary) over (partition by e.department_id) as avg_dept_sal
from employee e
join department d on e.department_id = d.department_id
) t1
) t2
where greater_count >= 2
This will return all employees of those departments. If you only want to see those employees whose salary is actually greater than the 90% you need to add another condition to the outer where clause to only select those.
The below stated query will give result as desired without using any analytics. Simple & Easy just like English.
**
*
with T as (select empname, deptno, salary from employee e1 where
salary > (select avg(salary)*0.9 from employee e2 where
e2.deptno=e1.deptno group by deptno having count(empname)>2)) select
T.empname, deptname, salary from T, department where
T.deptno=department.deptno;
*
**
I executed this query successfully on Oracle database.