SQL面试题练习 —— 共同使用ip用户检测问题

目录

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

题目来源:字节跳动。

1 题目

现有用户登录日志表,记录了每个用户登录的IP地址,请查询共同使用过3个及以上IP的用户对;

样例数据

+----------+-----------------+----------------------+
| user_id  |       ip        |      time_stamp      |
+----------+-----------------+----------------------+
| 2        | 223.104.41.101  | 2023-08-24 07:00:00  |
| 4        | 223.104.41.122  | 2023-08-24 10:00:00  |
| 5        | 223.104.41.126  | 2023-08-24 11:00:00  |
| 4        | 223.104.41.126  | 2023-08-24 13:00:00  |
| 1        | 223.104.41.101  | 2023-08-24 16:00:00  |
| 3        | 223.104.41.101  | 2023-08-24 16:02:00  |
| 2        | 223.104.41.104  | 2023-08-24 16:30:00  |
| 1        | 223.104.41.121  | 2023-08-24 17:00:00  |
| 2        | 223.104.41.122  | 2023-08-24 17:05:00  |
| 3        | 223.104.41.103  | 2023-08-24 18:11:00  |
| 2        | 223.104.41.103  | 2023-08-24 19:00:00  |
| 1        | 223.104.41.104  | 2023-08-24 19:00:00  |
| 3        | 223.104.41.122  | 2023-08-24 19:07:00  |
| 1        | 223.104.41.122  | 2023-08-24 21:00:00  |
+----------+-----------------+----------------------+

2 建表语句

sql 复制代码
--建表语句
CREATE TABLE t_login_log (
user_id bigint COMMENT '用户ID',
ip string COMMENT '用户登录ip地址',
time_stamp string COMMENT '登录时间'
) COMMENT '用户登录记录表'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 
;
-- 插入数据
insert into t_login_log(user_id,ip,time_stamp)
values
(1,'223.104.41.101','2023-08-24 16:00:00'),
(1,'223.104.41.121','2023-08-24 17:00:00'),
(1,'223.104.41.104','2023-08-24 19:00:00'),
(1,'223.104.41.122','2023-08-24 21:00:00'),
(1,'223.104.41.122','2023-08-24 22:00:00'),
(2,'223.104.41.101','2023-08-24 07:00:00'),
(2,'223.104.41.103','2023-08-24 19:00:00'),
(2,'223.104.41.104','2023-08-24 16:30:00'),
(2,'223.104.41.122','2023-08-24 17:05:00'),
(3,'223.104.41.103','2023-08-24 18:11:00'),
(3,'223.104.41.122','2023-08-24 19:07:00'),
(3,'223.104.41.101','2023-08-24 16:02:00'),
(4,'223.104.41.126','2023-08-24 13:00:00'),
(5,'223.104.41.126','2023-08-24 11:00:00'),
(4,'223.104.41.122','2023-08-24 10:00:00');

3 题解

(1)将所有用户登录记录按照用户ID和登录IP去重

sql 复制代码
select user_id,
       ip
from t_login_log
group by user_id, ip

执行结果

+----------+-----------------+
| user_id  |       ip        |
+----------+-----------------+
| 1        | 223.104.41.101  |
| 1        | 223.104.41.104  |
| 1        | 223.104.41.121  |
| 1        | 223.104.41.122  |
| 2        | 223.104.41.101  |
| 2        | 223.104.41.103  |
| 2        | 223.104.41.104  |
| 2        | 223.104.41.122  |
| 3        | 223.104.41.101  |
| 3        | 223.104.41.103  |
| 3        | 223.104.41.122  |
| 4        | 223.104.41.122  |
| 4        | 223.104.41.126  |
| 5        | 223.104.41.126  |
+----------+-----------------+

(2)通过IP地址进行自关联,去重,剔除相同用户。

sql 复制代码
with tmp as
         (select user_id,
                 ip
          from t_login_log
          group by user_id, ip)
select t1.user_id,
       t2.user_id,
       t1.ip
from tmp as t1
         join
     tmp as t2
     on t1.ip = t2.ip
where t1.user_id < t2.user_id

执行结果

+-------------+-------------+-----------------+
| t1.user_id  | t2.user_id  |      t1.ip      |
+-------------+-------------+-----------------+
| 1           | 2           | 223.104.41.101  |
| 1           | 3           | 223.104.41.101  |
| 2           | 3           | 223.104.41.101  |
| 2           | 3           | 223.104.41.103  |
| 1           | 2           | 223.104.41.104  |
| 1           | 2           | 223.104.41.122  |
| 1           | 3           | 223.104.41.122  |
| 1           | 4           | 223.104.41.122  |
| 2           | 3           | 223.104.41.122  |
| 2           | 4           | 223.104.41.122  |
| 3           | 4           | 223.104.41.122  |
| 4           | 5           | 223.104.41.126  |
+-------------+-------------+-----------------+

(3)根据用户组计算使用共同IP的个数

sql 复制代码
with tmp as
         (select user_id,
                 ip
          from t_login_log
          group by user_id, ip)
select t1.user_id,
       t2.user_id,
       count(t1.ip)
from tmp as t1
         join
     tmp as t2
     on t1.ip = t2.ip
where t1.user_id < t2.user_id
group by t1.user_id,
         t2.user_id

执行结果

+-------------+-------------+------+
| t1.user_id  | t2.user_id  | _c2  |
+-------------+-------------+------+
| 1           | 2           | 3    |
| 1           | 3           | 2    |
| 1           | 4           | 1    |
| 2           | 3           | 3    |
| 2           | 4           | 1    |
| 3           | 4           | 1    |
| 4           | 5           | 1    |
+-------------+-------------+------+

(4)查询共同使用过3个以上IP的用户对

sql 复制代码
with tmp as
         (select user_id,
                 ip
          from t_login_log
          group by user_id, ip)
select t1.user_id,
       t2.user_id
from tmp as t1
         join
     tmp as t2
     on t1.ip = t2.ip
where t1.user_id < t2.user_id
group by t1.user_id,
         t2.user_id
having count(t1.ip) >= 3

执行结果

+-------------+-------------+
| t1.user_id  | t2.user_id  |
+-------------+-------------+
| 1           | 2           |
| 2           | 3           |
+-------------+-------------+
相关推荐
Hacker_LaoYi几秒前
【渗透技术总结】SQL手工注入总结
数据库·sql
岁月变迁呀2 分钟前
Redis梳理
数据库·redis·缓存
独行soc3 分钟前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍06-基于子查询的SQL注入(Subquery-Based SQL Injection)
数据库·sql·安全·web安全·漏洞挖掘·hw
你的微笑,乱了夏天37 分钟前
linux centos 7 安装 mongodb7
数据库·mongodb
工业甲酰苯胺1 小时前
分布式系统架构:服务容错
数据库·架构
独行soc2 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍08-基于时间延迟的SQL注入(Time-Based SQL Injection)
数据库·sql·安全·渗透测试·漏洞挖掘
White_Mountain2 小时前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu
Code apprenticeship2 小时前
怎么利用Redis实现延时队列?
数据库·redis·缓存
百度智能云技术站2 小时前
广告投放系统成本降低 70%+,基于 Redis 容量型数据库 PegaDB 的方案设计和业务实践
数据库·redis·oracle
装不满的克莱因瓶2 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb