繼上一篇SQL練習題,給出答案

有的答案不止一個,若是仔細看的話會發現,有的也寫了幾個,若是你有更好的,歡迎交流分享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'男');
相關文章
相關標籤/搜索