# 无论考没考试都要该科目这栏且无连接字段,就可以考虑笛卡尔积了SELECT s.student_id,s.student_name,s.subject_name,IFNULL(e.num,0) attended_exams
FROM(SELECT*FROM Students
CROSSJOIN Subjects
)s
LEFTJOIN(SELECT student_id,subject_name,count(*) num
FROM Examinations
GROUPBY student_id,subject_name
)e
ON s.student_id=e.student_id AND s.subject_name=e.subject_name
ORDERBY s.student_id asc,s.subject_name asc;
1.3 运行截图
2 可以放心投资的国家
2.1 题目内容
2.1.1 基本题目信息1
2.1.2 基本题目信息2
2.1.3 示例输入输出
a 示例输入
b 示例输出
2.2 示例sql语句
# 先求出每个国家的平均通话时长,在求出全球通话时长,然后把两者进行比较即可#substring函数从1开始,#全球通话时长是无论是接听者还是打电话的人都需要算进去SELECT c1.name country
FROM Country c1
INNERJOIN Person p
ON c1.country_code=SUBSTRING(p.phone_number,1,3)LEFTJOIN(SELECT caller_id cid,duration
FROM Calls
UNIONAllSELECT callee_id cid,duration
FROM Calls
)c2
ON p.id=c2.cid
GROUPBY c1.name
HAVINGavg(c2.duration)>(SELECTavg(duration)FROM Calls);
2.3 运行截图
3 部门工资最高的员工
3.1 题目内容
3.1.1 基本题目信息
3.1.2 示例输入输出
3.2 示例sql语句
SELECT e1.department,e1.employee,e1.salary
FROM(SELECT e.name Employee,d.name Department,e.Salary,rank()over(partitionby e.departmentId ORDERBY e.salary desc) rk
FROM Employee e
INNERJOIN Department d
ON e.departmentId=d.id
)e1
WHERE e1.rk=1;