开发者社区 问答 正文

SQL查询语句优化

select top 5 peopleName,peopleSalary, peopleSex
  from people where peopleSex ='男'order by peopleSalary desc
select  top 5 peopleName,peopleSalary, peopleSex
  from people where peopleSex='女' order by peopleSalary desc

查询工资最高的5位男员工 查询工资最高的5位女员工 如何合并成一条SQL语句啊?

展开
收起
游客thq53lsdx7b4m 2022-03-01 18:08:57 6517 分享 版权
4 条回答
写回答
取消 提交回答
  • SELECT * FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY peopleSex ORDER BY peopleSalary DESC) rowid, peopleName,peopleSalary, peopleSex FROM people ) TT WHERE TT.rowid <= 5;

    2022-06-29 15:59:46
    赞同 展开评论
  • Mgo

    两个sql之间 加个 union 就可以啦.

    2022-03-12 09:15:33
    赞同 展开评论
  • select top 5 * from ..... where .... union select top 5 * from ..... where ....

    2022-03-02 08:49:38
    赞同 展开评论
  • 1

    2022-03-01 19:54:26
    赞同 展开评论
问答分类:
SQL
问答地址: