有的答案不止一个,若是仔细看的话会发现,有的也写了几个,若是你有更好的,欢迎交流分享sql
1 select Sname,Ssex,Class from Student; 2 select distinct Depart from Teacher; 3 select * from student; 4 select * from Score where Degree >= 60 and Degree <=80; select * from Score where Degree between 60 and 80; 5 select * from Score where Degree = 85 or Degree = 86 or Degree = 88; select * from Score where Degree in (85,86,88); 6 select * from Student where class = '95031' or Ssex = N'女'; 7 select * from Student order by class desc; 8 select * from Score order by Cno,Degree desc; 9 select count(*) from Student where class='95031'; select count(Sno) from Student where class='95031'; 10 select Sno,Cno,Degree from Score where Degree=(select max(Degree) from Score); 11 select 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; 12 select 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); 13 select Sno from Score where Degree between 70 and 90; 14 select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno; 15 select Sno,Cname,Degree from Score join Course on Score.Cno=Course.Cno; 16 select Sname,Cname,Degree from Student join Score on Student.Sno=Score.Sno join Course on Course.Cno=Score.Cno; 17 select 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'; 18 create 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; 19 select * 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'); 20 select 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'); 22 select 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'); --23 select 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)); 25 select * 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); 27 select 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 where Score.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 where Depart=N'计算机系' and prof in(select Prof from Teacher where Depart=N'电子工程系')); 29 select 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 任何一个值 30 select 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; 31 select Tname as name,Tsex as sex,Tbirthday as birthday from Teacher union 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 Teacher union select Sname,Ssex,Sbirthday from Student; 32 select Tname as name,Tsex as sex,Tbirthday as birthday from Teacher where Tsex=N'女' union select Sname,Ssex,Sbirthday from Student where Ssex=N'女'; 33 select 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; 34 select 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)); 35 select 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)); 36 select 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; 37 select * from Student where Sname not like N'王%'; 38 select Sname as 姓名,datepart(year,getdate())-year(Sbirthday) as 年龄 from Student; --获取系统当前年份 select datepart(year,getdate()); select getdate(); 39 select max(Sbirthday) max,min(Sbirthday) min from Student; 40 select * from Student order by class desc,Sbirthday; 41 select 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'男'); 42 select * from Score where Degree=(select max(Degree) from Score); 43 select Sname from Student where Ssex=(select Ssex from Student where Sname=N'李军'); 44 select Sname from Student where Ssex=(select Ssex from Student where Sname=N'李军') and class=(select class from Student where Sname=N'李军'); 45 select * from Score where Cno in(select Cno from Course where Cname=N'计算机导论') and Sno in( select Sno from Student where Ssex=N'男');