sql语句利用子查询语句

使用子查询,查询每位员工的编号、姓名、工资、所在部门平均工资,结果按照工资升序排序

是emp表吧
select empno,ename,sal,(select avg(b.sal) from emp b where a.deptno=b.deptno group by b.deptno) as avgsal from emp a;
子查询不加group by 也可以,因为给了它限制条件 直接默认group by 1,结果一样,可以思考一下:
select empno,ename,sal,(select avg(b.sal) from emp b where a.deptno=b.deptno ) as avgsal from emp a;
如果解决了你的问题,麻烦采纳一下哦!

提供一下相关的表结构。

select a.员工编号,姓名,b.实发工资,发放日期

from 员工表 a

outer apply (select top 1 实发工资,发放日期 from 工资表 where 员工编号=a.员工编号 order by 发放日期 desc) b

left join (select avg(实发工资) as 部门平均工资,y.部门 from 工资表 x left join 员工表 y on x.员工编号=y.员工编号 group by y.部门) c on c.部门=a.部门

order by 3