SQL255给出本表排名为奇数行的first_name

给出employees表中排名为奇数行的first_name

https://www.nowcoder.com/practice/e3cf1171f6cc426bac85fd4ffa786594

方法一:
select
e.first_name first
from employees e
join 
(select
first_name,
row_number()over(order by first_name) posn
from employees) e1
on e.first_name=e1.first_name
where e1.posn%2=1

本题思路如下:
1.用row_number()over()建立窗口函数用于排序,输出first_name、posn作为表e1;
2.第一条作为子查询,外面再套一层查询输出e.first_name first,用同一个表连接注意别名,连接键为first_name,通过where e1.posn%2=1筛选奇数行(需要涉及到对同一个表的重复使,且要找到奇数位置的行)。
总之有3个知识点:排序窗口函数、子查询、筛选奇数 (取余运算符%)
全部评论

相关推荐

notbeentak...:孩子,说实话,选择很重要,可能你换一个方向会好很多,但是现在时间不太够了,除非准备春招
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务