开发者社区> 问答> 正文

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 6497 0
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
问答地址:
问答排行榜
最热
最新

相关电子书

更多
SQL Server在电子商务中的应用与实践 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载