要求写一段 SQL 语句,查询出每个学生参加每一门科目测试的次数,结果按 student_id 和 subject_name 排序。结果如下
代码:
select st.student_id,st.student_name,su.subject_name, count(su.subject_name) as attended_exams
from
students st cross join subjects su
left join Examinations as e
on e.student_id = st.student_id
group by st.student_id,su.subject_name
order by st.student_id , su.subject_name
学到新【知识点】cross join 返回两表的笛卡尔积,具体如下