有的答案不止一个,如果仔细看的话会发现,有的也写了几个,如果你有更好的,欢迎交流分享
1select Sname,Ssex,Class from Student;2select distinct Depart from Teacher;3select * from student;4select * from Score where Degree >= 60 and Degree <=80;select * from Score where Degree between 60 and 80; 5select * from Score where Degree = 85 or Degree = 86 or Degree = 88;select * from Score where Degree in (85,86,88);6select * from Student where class = '95031' or Ssex = N'女';7select * from Student order by class desc; 8select * from Score order by Cno,Degree desc;9select count(*) from Student where class='95031';select count(Sno) from Student where class='95031';10select Sno,Cno,Degree from Score where Degree=(select max(Degree) from Score);11select AVG(Degree) from Score where Cno='3-245';select AVG(Degree) from Score where Cno='3-105';select AVG(Degree) from Score where Cno='6-166';select AVG(Degree) from Score group by Cno;12select avg(Degree) from Score group by Cno having count(Cno)>=5 and Cno like '3%'; select AVG(Degree) from Score group by Cno having count(*)>=5 and Cno like '3%';select avg(Degree) from Score where Cno like '3%' and Cno in (select Cno from Score group by Cno having count(*)>=5);13select Sno from Score where Degree between 70 and 90; 14select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno;15select Sno,Cname,Degree from Score join Course on Score.Cno=Course.Cno;16select Sname,Cname,Degree from Student join Score on Student.Sno=Score.Sno join Course on Course.Cno=Score.Cno;17select AVG(Degree) from Score where Sno in (select Sno from Student where class='95033');select AVG(Degree) from Score,Student where Student.Sno=Score.Sno and Class='95033';18create table grade(low int,upp int,rankk char(1));insert into grade values(90,100,'A')insert into grade values(80,89,'B')insert into grade values(70,79,'C')insert into grade values(60,69,'D')insert into grade values(0,59,'E')SELECT * FROM GRADE;19select * from Student,Score where Student.Sno=Score.Sno and Cno='3-105' and Degree>76;select distinct a.Cno,a.Sno,a.Degree from Score a,Score b where a.Cno='3-105' and a.Degree>b.Degree and b.Sno='109' and b.Cno='3-105';select * from Score where Cno='3-105' and degree>(select degree from Score where Sno='109' and Cno='3-105');20select Sno from Score group by Sno having count(*)>1;select * from Score where Degree not in (select max(Degree) from Score group by Cno);select * from Score a where Sno in(select Sno from Score group by Sno having count(*)>1)and a.Degree not in(select max(Degree) from Score b where b.Cno=a.Cno); select * from Score where Sno in(select Sno from Score group by Sno having count(*)>1)and Degree not in(select max(Degree) from Score group by Sno);select * from Score where Degree not in (select max(Degree) from Score group by Sno having count(*)>1) and Sno in(select Sno from Score group by Sno having count(*)>1);--21--------------------------------------------------------------------------------------------select * from Score where Degree>(select Degree from Score where Sno='109' and Cno='3-105');22select Sno,Sname,Sbirthday from Student where Sbirthday='1977-09-01'; select Sno,Sname,Sbirthday from Student where Sbirthday=(select Sbirthday from Student where Sno='108');select Sno,Sname,Sbirthday from Student where year(Sbirthday)=(select Year(Sbirthday) from Student where Sno='108');--23select Degree from Score,Course,Teacher where Teacher.Tname=N'张旭' and Teacher.Tno=Course.Tno and Course.Cno=Score.Cno;select Degree from Score where Cno=(select Cno from Course where Tno=(select Tno from Teacher where Tname=N'张旭'));--24 为什么没有起作用??????--select Tname from Teacher where Tno in(select Tno from Course,Score group by Score.Cno having count(Score.Cno)>5);select Tname from Teacher where Tno in(select Tno from Course where Cno in(select Cno from Score group by Cno having count(Score.Cno)>5));25select * from Student where class='95033' or class='95031';select * from Student where class in('95033','95031');--26无效select Cno from Score where Degree in (select Degree from Score group by Cno having Score.Degree >85);27select distinct * from Score where Cno in(select Cno from Course where Tno in(select Tno From Teacher where Depart=N'计算机系'));select distinct Score.Sno,Score.Cno,Score.Degree from Score,Course,Teacher whereScore.Cno=Course.Cno and Course.Tno=Teacher.Tno and Teacher.Depart=N'计算机系';28--只查出了计算机系中的记录select Tname,Prof from Teacher where Depart=N'计算机系' and prof not in (select Prof from Teacher where Depart=N'电子工程系');select Tname,Prof from Teacher where Prof not in (select Prof from Teacher whereDepart=N'计算机系' and prof in(select Prof from Teacher where Depart=N'电子工程系'));29select Cno,Sno,Degree from Score where Cno='3-105' and Degree>any(select Degree from Score where Cno='3-245') order by Degree desc;--any 任何一个值30select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select max(Degree) from Score where Cno='3-245') order by Degree desc; select Cno,Sno,Degree from Score where Cno='3-105' and Degree>all(select Degree from Score where Cno='3-245') order by Degree desc; 31select Tname as name,Tsex as sex,Tbirthday as birthday from Teacherunion select Sname as name,Ssex as sex,Sbirthday as birthday from Student;--起别名时,当都需要起同一个别名时,不需要重复起,下面的SQL和上面结果相同,用union连起来select Tname as name,Tsex as sex,Tbirthday as birthday from Teacherunion select Sname,Ssex,Sbirthday from Student;32select Tname as name,Tsex as sex,Tbirthday as birthday from Teacher where Tsex=N'女' unionselect Sname,Ssex,Sbirthday from Student where Ssex=N'女';33select Sno,Cno,Degree from Score a where a.Degree<(select AVG(Degree) from Score b where b.Cno=a.Cno);--这个查询出来的结果多了一个81.0,不正确select * from Score where Degree<(select AVG(Degree) from Score);select AVG(degree) from Score;34select Tname,Depart from Teacher;select Tname,Depart from Teacher where Tno in(select Tno from Course where Teacher.Tno=Course.Tno);select Tname,Depart from Teacher where exists(select Tno from Course where Teacher.Tno=Course.Tno);--通常用exists里面select后面跟*select Tname,Depart from Teacher where Tno in(select Tno from Course where Cno in(select Cno from Score where Cno != '0'));select Tname,Depart from Teacher where Tno in(select Tno from Course where Cno in(select Cno from Score where Course.Cno=Score.Cno));35select Tname,Depart from Teacher where Tno not in(select Tno from COurse);select Tname,Depart from Teacher where Tno not in(select Tno from Course where Teacher.Tno=Course.Tno);select Tname,Depart from Teacher where Tno not in(select Tno from Course where Cno in(select Cno from Score where Cno != '0'));select Tname,Depart from Teacher where Tno not in(select Tno from Course where Cno in(select Cno from Score));36select class from Student where Ssex=N'男' group by class having count(Student.Ssex)>=2;select class from Student where Ssex=N'男' group by class having count(*)>=2;37select * from Student where Sname not like N'王%';38select Sname as 姓名,datepart(year,getdate())-year(Sbirthday) as 年龄 from Student;--获取系统当前年份select datepart(year,getdate());select getdate();39select max(Sbirthday) max,min(Sbirthday) min from Student;40select * from Student order by class desc,Sbirthday;41select Teacher.Tname,Teacher.Tsex,Course.Cname from Teacher,Course where Teacher.Tsex=N'男' and Teacher.Tno=Course.Tno;select distinct(Tname),Tsex, Cname from Teacher, Course where Tsex=N'男' and Course.Tno in(select Tno from Teacher where Tsex=N'男');42select * from Score where Degree=(select max(Degree) from Score);43select Sname from Student where Ssex=(select Ssex from Student where Sname=N'李军');44select Sname from Student where Ssex=(select Ssex from Student where Sname=N'李军') and class=(select class from Student where Sname=N'李军');45select * from Score where Cno in(select Cno from Course where Cname=N'计算机导论') and Sno in(select Sno from Student where Ssex=N'男');