继上一篇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'男');

 

转载于:https://my.oschina.net/u/3068158/blog/848727

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值