SQL面试题练习 —— 微信运动步数在好友中的排名

目录

  • [1 题目](#1 题目)
  • [2 建表语句](#2 建表语句)
  • [3 题解](#3 题解)

题目来源:腾讯。

1 题目

有两个表,朋友关系表user_friend,用户步数表user_steps。朋友关系表包含两个字段,用户id,用户好友的id;用户步数表包含两个字段,用户id,用户的步数.用户在好友中的排名

复制代码
-- user_friend 数据
+----------+------------+
| user_id  | friend_id  |
+----------+------------+
| 1        | 2          |
| 1        | 3          |
| 2        | 1          |
| 2        | 3          |
| 2        | 4          |
| 2        | 5          |
| 3        | 1          |
| 3        | 4          |
| 3        | 5          |
| 4        | 2          |
| 4        | 3          |
| 4        | 5          |
| 5        | 2          |
| 5        | 3          |
| 5        | 4          |
+----------+------------+
--user_friend数据
+---------------------+-------------------+
| user_steps.user_id  | user_steps.steps  |
+---------------------+-------------------+
| 1                   | 100               |
| 2                   | 95                |
| 3                   | 90                |
| 4                   | 80                |
| 5                   | 10                |
+---------------------+-------------------+

2 建表语句

sql 复制代码
CREATE TABLE user_friend
(
    user_id   INT,
    friend_id INT
) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

-- 插入数据
INSERT INTO user_friend
VALUES (1, 2),
       (1, 3),
       (2, 1),
       (2, 3),
       (2, 4),
       (2, 5),
       (3, 1),
       (3, 4),
       (3, 5),
       (4, 2),
       (4, 3),
       (4, 5),
       (5, 2),
       (5, 3),
       (5, 4);

CREATE TABLE user_steps
(
    user_id INT,
    steps   INT
) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

INSERT INTO user_steps
VALUES (1, 100),
       (2, 95),
       (3, 90),
       (4, 80),
       (5, 10);

3 题解

(1)列出好友步数,并将自己步数添加到结果中

sql 复制代码
--好友步数
select t1.user_id, t1.friend_id, t2.steps
from user_friend t1
join user_steps t2
on t1.friend_id = t2.user_id
union all
-- 自己步数
select user_id, user_id as friend_id, steps
from user_steps

执行结果

复制代码
+--------------+----------------+------------+
| _u1.user_id  | _u1.friend_id  | _u1.steps  |
+--------------+----------------+------------+
| 1            | 2              | 95         |
| 1            | 3              | 90         |
| 2            | 1              | 100        |
| 2            | 3              | 90         |
| 2            | 4              | 80         |
| 2            | 5              | 10         |
| 3            | 1              | 100        |
| 3            | 4              | 80         |
| 3            | 5              | 10         |
| 4            | 2              | 95         |
| 4            | 3              | 90         |
| 4            | 5              | 10         |
| 5            | 2              | 95         |
| 5            | 3              | 90         |
| 5            | 4              | 80         |
| 1            | 1              | 100        |
| 2            | 2              | 95         |
| 3            | 3              | 90         |
| 4            | 4              | 80         |
| 5            | 5              | 10         |
+--------------+----------------+------------+

(2)按照用户分组,给每个用户的"好友"进行排名

sql 复制代码
select tt1.user_id,
       tt1.friend_id,
       tt1.steps,
       row_number() over (partition by tt1.user_id order by tt1.steps desc) as row_num
from (
         --好友步数
         select t1.user_id,
                t1.friend_id,
                t2.steps
         from user_friend t1
                  join user_steps t2
                       on t1.friend_id = t2.user_id
         union all
         -- 自己步数
         select user_id,
                user_id as friend_id,
                steps
         from user_steps) tt1

执行结果

复制代码
+--------------+----------------+------------+----------+
| tt1.user_id  | tt1.friend_id  | tt1.steps  | row_num  |
+--------------+----------------+------------+----------+
| 1            | 1              | 100        | 1        |
| 1            | 2              | 95         | 2        |
| 1            | 3              | 90         | 3        |
| 2            | 1              | 100        | 1        |
| 2            | 2              | 95         | 2        |
| 2            | 3              | 90         | 3        |
| 2            | 4              | 80         | 4        |
| 2            | 5              | 10         | 5        |
| 3            | 1              | 100        | 1        |
| 3            | 3              | 90         | 2        |
| 3            | 4              | 80         | 3        |
| 3            | 5              | 10         | 4        |
| 4            | 2              | 95         | 1        |
| 4            | 3              | 90         | 2        |
| 4            | 4              | 80         | 3        |
| 4            | 5              | 10         | 4        |
| 5            | 2              | 95         | 1        |
| 5            | 3              | 90         | 2        |
| 5            | 4              | 80         | 3        |
| 5            | 5              | 10         | 4        |
+--------------+----------------+------------+----------+

(3)求取最终结果

sql 复制代码
select user_id,
       row_num
from (select tt1.user_id,
             tt1.friend_id,
             tt1.steps,
             row_number() over (partition by tt1.user_id order by tt1.steps desc) as row_num
      from (
               --好友步数
               select t1.user_id,
                      t1.friend_id,
                      t2.steps
               from user_friend t1
                        join user_steps t2
                             on t1.friend_id = t2.user_id
               union all
               -- 自己步数
               select user_id,
                      user_id as friend_id,
                      steps
               from user_steps) tt1) tt2
where user_id = friend_id

执行结果

复制代码
+----------+----------+
| user_id  | row_num  |
+----------+----------+
| 1        | 1        |
| 2        | 2        |
| 3        | 2        |
| 4        | 3        |
| 5        | 4        |
+----------+----------+
相关推荐
快来卷java31 分钟前
MySQL篇(四)事务相关知识详解
java·数据库·mysql·链表
꧁坚持很酷꧂44 分钟前
Qt实现鼠标右键弹出弹窗退出
数据库·qt·计算机外设
振鹏Dong1 小时前
MySQL系统库汇总
数据库·mysql
zhslhm1 小时前
HeidiSQL:多数据库管理工具
数据库·数据库管理技巧·heidisql优化方案·开源数据库管理
垂金烟柳1 小时前
CentOS 7上配置SQL Server链接其他SQL Server服务器
服务器·数据库·sqlserver
一期一祈^2 小时前
使用MySQL时出现 Ignoring query to other database 错误
数据库·mysql
Mr.wangh2 小时前
Spring Boot 打印日志
java·数据库·spring boot
BenBen尔3 小时前
innodb如何实现mvcc的
数据库
孪生质数-3 小时前
2-Visual Studio 2022 NET开发Windows桌面软件并连接SQL Server数据库
数据库·windows·sqlserver·visual studio
T0uken4 小时前
【LLM】MCP(Python):实现 SSE 通信的 Server 和 Client
数据库·python·microsoft