SQL面试题集:识别互相关注的用户

news/2025/2/22 2:48:21

文章目录

      • 一、题目描述
      • 二、实现步骤
        • 步骤1:筛选双向关注用户对
        • 步骤2:去重处理
        • 最终SQL实现
      • 三、其他方法

题目来源:快手

一、题目描述

快手平台需优化好友推荐算法,需从用户关注行为中筛选出双向关注的用户对(即互相关注关系),用于分析高互动用户群体的行为特征及构建社交图谱。

样例数据
假设关注关系表 follow 包含以下数据:

from_userto_user
AB
BA
AC
CA
BC

二、实现步骤

步骤1:筛选双向关注用户对

作用:通过自连接,筛选出互相关注的用户对。
SQL逻辑

sql">select 
    t1.from_user as user_a, 
    t1.to_user as user_b
from 
    follow t1
join 
    follow t2
on 
    t1.from_user = t2.to_user 
    and t1.to_user = t2.from_user;

执行结果

user_auser_b
AB
BA
AC
CA
步骤2:去重处理

作用:确保每对用户只输出一行,避免重复。
SQL逻辑

sql">select 
    case when t1.from_user < t1.to_user then t1.from_user else t1.to_user end as user_a,
    case when t1.from_user < t1.to_user then t1.to_user else t1.from_user end as user_b
from 
    follow t1
join 
    follow t2
on 
    t1.from_user = t2.to_user 
    and t1.to_user = t2.from_user
where 
    t1.from_user < t1.to_user;

执行结果

user_auser_b
AB
AC
最终SQL实现
sql">select 
    case when t1.from_user < t1.to_user then t1.from_user else t1.to_user end as user_a,
    case when t1.from_user < t1.to_user then t1.to_user else t1.from_user end as user_b
from 
    follow t1
join 
    follow t2
on 
    t1.from_user = t2.to_user 
    and t1.to_user = t2.from_user
where 
    t1.from_user < t1.to_user;

三、其他方法

sql">select
    least(user_a, user_b) as user_id,
    greatest(user_a, user_b) as friend_id
from follow
group by least(user_a, user_b), greatest(user_a, user_b)
having count(*) >= 2;

http://www.niftyadmin.cn/n/5861539.html

相关文章

Rust配置笔记

1.Node.js下载配置 2.c环境配置 C我是用vs装的点击这个installer 点击修改 选择C环境就行,这个时候它就帮忙配置环境了 3.Rust下载配置 4.装napi-rs框架 npm install -g napi-rs/cliRust下载网站 下完之后直接打开 一开始下包会比较慢,多等等 下好之后跑项目前第一件事配置…

Flutter 记一次疑难杂症

问题描述 在运行.\gradlew build时&#xff0c;就会出现下面的错误&#xff0c;死活都解决不了。最牛逼的是&#xff0c;这个问题不解决&#xff0c;直接在 Android 模拟器中运行 Flutter 项目&#xff0c;电脑就直接给我干蓝屏了&#x1f92f;&#x1f92f;&#x1f92f; 直…

深入解析:Tableau在数据可视化中的高级应用

深入解析&#xff1a;Tableau在数据可视化中的高级应用 引言 在大数据时代&#xff0c;数据可视化已成为数据分析中不可或缺的一部分。作为一款广受欢迎的数据可视化工具&#xff0c;Tableau以其强大的功能和灵活性&#xff0c;赢得了众多数据分析师的青睐。然而&#xff0c;…

智慧医疗胃肠道息肉分割数据集labelme格式1000张1类别

数据集格式&#xff1a;labelme格式(不包含mask文件&#xff0c;仅仅包含jpg图片和对应的json文件) 图片数量(jpg文件个数)&#xff1a;1000 标注数量(json文件个数)&#xff1a;1000 标注类别数&#xff1a;1 标注类别名称:["polypus"] 每个类别标注的框数&…

41. 缺失的第一个正数(LeetCode 热题 100)

题目来源&#xff1a; 41. 缺失的第一个正数 - 力扣&#xff08;LeetCode&#xff09; 题目内容&#xff1a; 给你一个未排序的整数数组 nums &#xff0c;请你找出其中没有出现的最小的正整数。 请你实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案。 示例 1&…

2000字,极简版华为数字化转型方法论

​作为国内科技行业的领军者&#xff0c;华为的成功经验为众多企业提供了宝贵的借鉴。本文将围绕准备、规划和执行三个阶段展开&#xff0c;结合华为的实践案例&#xff0c;深入剖析其数字化转型的方法论&#xff0c;希望能为您的企业数字化转型提供有益的参考。 一、数字化转型…

机器学习,我们主要学习什么?

机器学习的发展历程 机器学习的发展历程&#xff0c;大致分为以下几个阶段&#xff1a; 1. 起源与早期探索&#xff08;20世纪40年代-60年代&#xff09; 1949年&#xff1a;Hebb提出了基于神经心理学的学习机制&#xff0c;开启了机器学习的先河1950年代&#xff1a;机器学习的…

Error [ERR_REQUIRE_ESM]: require() of ES Module

报错信息&#xff1a; 【报错】Message.js 导入方式不对&#xff0c;用的是 ES Moudle 的语法&#xff0c;提示使用 import 引入文件 项目开发没有用到 js-message 依赖&#xff0c;是 node-ipc 依赖中用到的 js-message 依赖&#xff0c; node-ipc 中限制 js-message 版本&a…