# Write your MySQL query statement below# ①求出所有朋友关系的总表WITH T as(SELECT user1_id ,user2_id
FROM friendship
UNIONSELECT user2_id user1_id ,user1_id user2_id
FROM friendship
)SELECT t1.user1_id,t2.user2_id,count(*) common_friend
FROM T t1
INNERJOIN T t2
ON t1.user2_id=t2.user1_id
INNERJOIN T t3
ON t1.user1_id=t3.user1_id
WHERE t2.user2_id=t3.user2_id AND t1.user1_id<t2.user2_id
GROUPBY t1.user1_id,t2.user2_id
HAVINGcount(*)>=3
1.3 运行截图
2 查询具有最多共同关注者的所有两两结对组
2.1 题目内容
2.1.1 基本题目信息
2.1.2 示例输入输出
2.2 示例sql语句
# 一样的关注着可能不只一个with T as(SELECT r1.user_id user1_id,r2.user_id user2_id,IFNULL(count(*),0) n
FROM Relations r1
INNERJOIN Relations r2
ON r1.user_id<r2.user_id AND r1.follower_id=r2.follower_id
GROUPBY r1.user_id,r2.user_id
)SELECT user1_id,user2_id
FROM T
WHERE n=(SELECTMAX(n)FROM T
)
2.3 运行截图
3 找出每所学校的最低分数要求
3.1 题目内容
3.1.1 基本题目信息1
3.1.2 基本题目信息2
3.1.3 示例输入输出
a 示例输入
b 示例输出
3.2 示例sql语句
# 只要容量大于学生招生数,且是最低分数要求就可以招生# 先找出符合条件的,再去进行后续的筛选SELECT s.school_id,IFNULL(MIN(e.score),-1) score
FROM Schools s
LEFTJOIN Exam e
ON s.capacity>=e.student_count
GROUPBY s.school_id