基础题

1、统计及格与不及格的人数

select 

count(case when score>=60 then 1 end)及格,

count(case when score<60 then 1 end)不及格

from table

2、统计各班(或者各门课)及格与不及格人数

select cid,

sum(case when score>=60 then 1 else 0 end)as 及格,

sum(case when score<60 then 1 else 0 end)as 不及格

from table

group by  cid

3、求学生每门课程平均成绩并降序

select  id,name,AVG(score)avgscore

from table

group by id

order by avgscore desc

4、计算每个人单科最高成绩

select   t1.sid,t1.name,t1.subject,t1.score from stuscore t1,

(select sid,max(score) as maxscore from stuscore group by sid) t2,

where t1.sid=t2.sid and t1.score=t2.maxsore

 5、连接

https://blog.csdn.net/ccsuxwz/article/details/70157911

外连接查询: 分为左外连接,右外连接,

左外连接:根据左表的记录,在被连接的右表中找出符合条件的记录与之匹配,找不到匹配的,用null填充      

SELECT Student.Sno,Cno
FROM Student
LEFT JOIN Study
ON Student.Sno=Study.Sno
WHERE Grade IS NULL

右连接:根据右表的记录,在被连接的左表中找出符合条件的记录与之匹配,找不到匹配的,用null填充 

SELECT Student.Sno AS 学号,SName AS 姓名, Grade AS 成绩
FROM Study
RIGHT JOIN Student
ON Study.Sno=Student.Sno

6、子查询

SELECT Sno,SName
FROM Student
WHERE Sno IN
(
  SELECT Sno
  FROM Study
  WHERE Cno IN
  (
    SELECT Cno
    FROM Course
    WHERE CName='高等数学'
  )

猜你喜欢

转载自www.cnblogs.com/lizijiangmm/p/9690905.html