数据库作业27~45

--27、查询出“计算机系“教师所教课程的成绩表。
select * from Score where Cno in(select Cno from Course where Tno in(select  tno from Teacher where Depart='计算机系'))
--28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
select tname,prof from teacher where Prof not in(
 select Prof from Teacher where Prof in(select Prof from Teacher where Depart='计算机系')and Depart='电子工程系')
 and Depart in ('计算机系','电子工程系')
--29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select degree,Cno,sno from Score where degree >(select MIN( degree) from Score where Cno='3-245' ) and cno='3-105'order by degree desc
--第二种方法,any ,some任意一个
select degree,Cno,sno from Score where degree>any(select Degree from Score where Cno='3-245')and cno='3-105'order by degree desc
--30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select degree,Cno,sno from Score where degree >(select max( degree) from Score where Cno='3-245' ) and cno='3-105'
--第二种方法,all 所有的 相当于大于max
select degree,Cno,sno from Score where degree >all(select  degree from Score where Cno='3-245' ) and cno='3-105'

--31、 查询所有教师和同学的name、sex和birthday.
select Sname,Ssex,sbirthday from Student 
union --表的纵连接, 要列队列,数据类型按照先后顺序对应着数据类型,显示的名字按第一个表的显示
select Tname,Tsex,Tbirthday from Teacher

--32、查询所有“女”教师和“女”同学的name、sex和birthday.
select Sname,Ssex,sbirthday from Student where Ssex=''
union
select Tname,Tsex,Tbirthday from Teacher where Tsex=''
--33、 查询成绩比该课程平均成绩低的同学的成绩表。
select * from score a where degree <(select AVG(degree) from Score b where a.cno=b.cno )
select*from score
--34、 查询所有任课教师的Tname和Depart.
select tname ,depart from Teacher where Tno in (select distinct Tno from Course)
--35、查询所有未讲课的教师的Tname和Depart. 
select tname ,depart from Teacher where Tno not in (select distinct Tno from Course)
--36、查询至少有2名男生的班号。
select class from Student where Ssex='' group by Class having COUNT(*)>=2
--37、查询Student表中不姓“王”的同学记录。
select * from Student where sname not like '王%'
--38、查询Student表中每个学生的姓名和年龄。 
select sname,YEAR(GETDATE())-year(Sbirthday) as age from Student --year(getdate())取当前的时间并对当前时间取年份
--39、查询Student表中最大和最小的Sbirthday日期值。 
select max(day(sbirthday)) from student
select min(day(sbirthday)) from student
--40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from Student order by Class asc,YEAR(GETDATE())-YEAR(Sbirthday) asc
--41、查询“男”教师及其所上的课程。
select Tname,Cname from Course join Teacher on Course.Tno=Teacher.Tno where Tsex=''
--42、查询最高分同学的Sno、Cno和Degree列。
select sno, cno ,degree from score where degree =(select MAX(degree) from score)
--43、查询和“李军”同性别的所有同学的Sname.
 select sname from student where ssex in (select ssex from Student where sname='李军')
 --44、查询和“李军”同性别并同班的同学Sname.
select sname from student where ssex in (select ssex from Student where sname='李军')and class in (select class from Student where sname='李军')
--45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
select degree from Score where Sno in(select sno from student where sno in (select sno from Score where Cno in(select Cno from course  where Cname='计算机导论')) and Ssex='')
--截取字符串,以及类型转换
select LEFT('abcdefg',2) --从左边开始截取,截取两位
select right('abcdefg',2) --从右边开始截取,截取两位
select  LOWER('abcD') --转换为小写
select  upper('abcD') --转换为大写
select  len('abcD')  --获取字符串的长度int类型
select LTRIM('      dsfsd  ssdf   s                ') --去左空格
select RTRIM('      dsfsd  ssdf   s                ') --去右空格
select Substring('abccceecfddd',5,2) --索引从1开始,  截取字符串
select REPLACE('遂非文过哦','','爱上放大')  --替换字符串
select REPLICATE('啊沙发',10) --以指定的次数打印前面的内容
print STR(123.456,5,1)  --将123.456转换为5位的字符串,小数点也占一位,最后保留1位小数
select SPACE(100) --打印100个空格
print 'asfaf'+'打发' --拼接
print reverse ('abcde' ) --翻转
print patindex('%啊的沙发%','123啊的沙发123') --显示'啊的沙发'在哪个位置
print stuff('abcdefghijklmnopqrst',4,2,'zzzz') --在索引为4这个位置删除两个值然后插入ZZZZ
--类型转换函数
print cast('123'as int)+12  --类型转换
print cast(123 as varchar(20))+'123'
print convert(int,'123')+123
--数学函数
select ABS(-13) --取绝对值
select ceiling(1.23) --取上限,天花板
select FLOOR(1.9) --地板
select PI()--取π
select RAND(10) --取随机数,随机出一个种子出来 一定会随机出0~1之间的数
select RouND(1.234567,3) --四舍五入到第3位
print sqrt(2) --取平方根
print square(4) --取平方
select sname,'出生日期为'+cast(YEAR(sbirthday)as varchar(20))+''+cast(MONTH(Sbirthday)as varchar(20))+''+cast(DAY(Sbirthday)as varchar(20))+'' from student
原文地址:https://www.cnblogs.com/lk-kk/p/4456861.html