Table Employee
Name Age Department Salary
Ramesh 20 Finance 50,000
Deep 25 Sales 30,000
Suresh 22 Finance 50,000
Ram 28 Finance 20,000
Pradeep 22 Sales 20,000
How to solve using sub-query?
Using window function,
select name,age,department,salary.
avg(salary) OVER(partition by department order by age) AS avg_salary from Employee
OUTPUT
Name Age Department Salary Avg_Salary
Ramesh 20 Finance 50,000 40, 000
Suresh 22 Finance 50,000 40, 000
Ram 28 Finance 20,000 40, 000
Pradeep 22 Sales 20,000 25, 000
Deep 25 Sales 30,000 25, 000
You could use:
select e.Name,
e.Age,
e.Department,
e.Salary,
tbl.avg_salary
from employees e
inner join ( select Department,
avg(Salary) as avg_salary
from employees
group by Department
) as tbl on e.Department=tbl.Department
order by e.Department,e.Age asc;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.