首页
>
试题广场
>
给出employees表中排名为奇数行的first_name
[编程题]给出employees表中排名为奇数行的first_name
-
热度指数:205667
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M
-
算法知识视频讲解
对于employees表中,输出first_name按照升序排序后,排名为奇数的first_name。
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`)
);
输入:
emp_no
|
birth_date
|
first_name
|
last_name
|
gender
|
hire_date
|
10001
|
1953-09-02
|
Georgi
|
Facello
|
M
|
1986-06-26
|
10002
|
1964-06-02
|
Bezalel
|
Simmel
|
F
|
1985-11-21
|
10005
|
1955-01-21
|
Kyoichi
|
Maliniak
|
M
|
1989-09-12
|
10006
|
1953-04-20
|
Anneke
|
Preusig
|
F
|
1989-06-02
|
输出:
如对以上示例数据的first_name排序后的序列为:Anneke、Bezalel、Georgi、Kyoichi。
则原序列中的Georgi排名为3,Anneke排名为1,所以
按原序列顺序输出Georgi、Anneke。
示例1
输入
drop table if exists `employees` ;
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');
INSERT INTO employees VALUES(10005,'1955-01-21','Kyoichi','Maliniak','M','1989-09-12');
INSERT INTO employees VALUES(10006,'1953-04-20','Anneke','Preusig','F','1989-06-02');
牛客题解官
发表于 2025-03-18 12:02:23
精华题解
这道题目要求我们给出employees表中排名为奇数行的first_name,我们要做的事情如下:
1. 确定总体问题
我们需要从员工表中提取名字,并按照名字的字母顺序排序后,输出排名为奇数的名字。
2. 分析关键问题
排序名字并分配排名:使用ROW_NUMBER窗口函数按照名字的字母顺序为每个名
展开全文
高质量搬砖人
发表于 2021-02-01 09:40:26
一开始是用窗口函数做的,但是怎么都做不对... SELECT a.first_name
FROM
( SELECT first_name, row_number()over(ORDER BY first_name)AS ran
展开全文
select e1.first_name from employees e1where(select count(*) from employees e2 wheree1.first_name>=e2.first_name)%2 = 1 其实这里主要的理解清楚 e1.first_
展开全文
题目描述:对于employees表中,输出first_name排名(按first_name升序排序)为奇数的first_name(因为Georgi按first_name排名为3,Anneke按first_name排名为1,所以会输出这2个,且输出时不需排序。)最开始的思路: select t1.fi
展开全文
起初我的问题和大家一样,也是用ROW_NUMBER function直接得出结果,结果出来时排序的,题目要求原来顺序一脸懵逼。想了想是不是可以把ROW_NUMBER放条件里 SELECT first_name
FROM employees
WHERE EXISTS (
SELECT firs
展开全文
牛萍萍
发表于 2021-09-27 13:07:03
select first_name from
(select first_name, count(emp_no) over(order by first_name) cnt from employees order by emp_no) t
where cnt % 2 =1 二刷的时候,突然自己就
展开全文
方法一:思路:①窗口函数row_number的作用是赋予唯一的连续位次。巧用窗口函数row_number对数据进行行排序,对first_name进行排序,将得到的位次命名为row_num。②用求余函数找出奇数行。代码:
select
a.first_name
from
(sele
展开全文
首先说一下自己的思路,根据题意,难点在于取出奇数行的数据,因为表中没有相关排列数据,所以我就想构建一列,让这列对应行数据的排行,故此,在下面解释的第二点中解释了如何构建。
摘自其他人的回答,我觉得这个回答很好
本题使用了三层 SELECT 查询,为了便于理解,采用缩进方式分层,且最外层对应e1,最
展开全文
sql script select e.first_name
from (select rank() over(order by first_name) as rank_fn,first_name
from employees) tmp, employees e
where rank_f
展开全文
select first_name
from
(
select emp_no
,first_name
,rank() over(order by first_name asc) as rk
from employees
) em
where em.rk%2=1
order by emp_no as
展开全文
不做造粪机器
发表于 2024-07-24 06:24:13
select a.first_name
from (
select emp_no,first_name, row_number() over(order by first_name) row_num_
from employees
) a
where a.row_num_%2=1
o
展开全文