sql练习

use School
--1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select Sname,Ssex,class from Student
--2、 查询教师所有的单位即不重复的Depart列。
select distinct Depart from Teacher
--3、 查询Student表的所有记录。
select * from Student
--4、 查询Score表中成绩在60到80之间的所有记录。
select * from Score where Degree between 60 and 80
--5、 查询Score表中成绩为85,86或88的记录。
select * from Score where Degree in (85,86,88)
--6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from Student where Class = '95031' or Ssex = '女'
--7、 以Class降序查询Student表的所有记录。
select * from Student order by Class desc
--8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from Score order by Cno asc,Degree desc
--9、 查询“95031”班的学生人数。
select COUNT(*) from Student where Class = '95031'
--10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select Sno,Cno from Score where Degree = 
(select MAX(Degree) from Score)

select top 1 Sno,Cno from Score order by DEGREE desc
--11、 查询每门课的平均成绩。
select Cno,AVG(DEGREE) from Score group by Cno
--12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select AVG(Degree) from Score where Cno in
(select cno from Score group by Cno having COUNT(Cno)>=5)
and 
Cno like '3%'
--13、查询分数大于70,小于90的Sno列。

--14、查询所有学生的Sname、Cno和Degree列。
select t1.Sname,t2.Cno,t2.Degree from Student t1 full join Score t2 on t1.Sno = t2.Sno
--15、查询所有学生的Sno、Cname和Degree列。
select t1.Sno,t2.Cname,t1.DEGREE from Score t1 full join Course t2 on t1.Cno=t2.Cno
--16、查询所有学生的Sname、Cname和Degree列。
select t1.Sname,t3.Cname,t2.Degree 
from Student t1 
join Score t2 on t1.Sno=t2.Sno 
join Course t3 on t2.Cno = t3.Cno
原文地址:https://www.cnblogs.com/qwer123666/p/7059829.html