sql练习题

题一:

 分析:

要想对比不同行之间的数据大小,利用where不能直接对其进行对比,where只能对比不同列之间的数据,因此对同一个表join,将不同行对比的数据转换到列上。

 

 select  stu.* , c1.score as '01课程分数' ,c2.score as '02课程分数'  from SC c1 join SC c2 on  c1.SId = c2.SId  and c1.CId = '01'

 and c2.CId = '02'   join Student stu on  c1.SId = stu.SId  where  c1.score > c2.score

题二:  

 当出现“每一” 的问题,就要涉及到group by分组

select stu.SId,stu.Sname,avg(sc.score) as avg_score from Student  stu join SC sc on stu.SId = sc.SId group by stu.SId having  avg_score  >= 60 ORDER by avg_score DESC; 

原文地址:https://www.cnblogs.com/fjwjw/p/11561564.html