2022-11-23: 分数排名。输出结果和表的sql如下。请写出输出结果的sql语句?

2022-11-23: 分数排名。输出结果和表的sql如下。请写出输出结果的sql语句?
+——-+——+
| score | rank |
+——-+——+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+——-+——+

DROP TABLE IF EXISTS scores;
CREATE TABLE scores (
  id int(11) NOT NULL,
  score decimal(10,2) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO scores VALUES ('1', '3.50');
INSERT INTO scores VALUES ('2', '3.65');
INSERT INTO scores VALUES ('3', '4.00');
INSERT INTO scores VALUES ('4', '3.85');
INSERT INTO scores VALUES ('5', '4.00');
INSERT INTO scores VALUES ('6', '3.65');

答案2022-11-23:

sql语句如下:













































select a.score as score,
(select count(distinct b.score) from scores b where b.score >= a.score) as rank
from scores a
order by a.score desc

在这里插入图片描述

本作品采用《CC 协议》,转载必须注明作者和本文链接
微信公众号:福大大架构师每日一题。最新面试题,涉及golang,rust,mysql,redis,云原生,算法,分布式,网络,操作系统。
讨论数量: 1

每天一问,为啥不用窗口函数呢?

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
470
粉丝
21
喜欢
37
收藏
22
排名:457
访问:1.9 万
私信
所有博文
社区赞助商