查询练习1
1.查询出前三名学生存在哪些兴趣爱好。
select distinct Likes from student
where likes in(select top(3) Likes from Student);
2.查询学生的学号,分数信息,要求查询的列名展示成"学号",“分数”
--将列名的英文变为中文,首先应该想到起别名。
select StuCode as '学号',Score '分数' from Student
3.查询班级信息,将状态换一个显示方式,状态为0的记录展示为"正常",为1的记录展示为“已删除”?
--case when的两个作用: 1、转换显示。2、分等级
select Id,ClassName,case Status
when '0' then '正常'
when '1' then '已删除'
else '未知' end as '状态'
from Classes;
4.查询学生中姓名含有“天”字的,并且爱好是篮球的学生?
--like模糊查询,"%" 是个通配符表示匹配所有字符
select * from Student where RealName like'%天%' and Likes='篮球'
5.查询学生学号是“2019001”,“2019003”的学生信息?
--OR组合两个条件,并在任何一个条件为 TRUE 时取值为 TRUE。
--in 后边跟一对小括号,括号内填写 常量值,值之间用逗号分割
select * from Student where StuCode='2019001'or StuCode='2019003';
select * from Student where StuCode in('2019001','2019003');
6.查询年龄最大的前三名学生信息?
select top(3) * from Student order by birthday asc;
7.使用一条Sql语句往班级表中一次插入三条数据?
insert into Classes(Id,ClassName,Status)
values(5,'五班',1),(6,'六班',0),(7,'七班',0);
8.使用一条Sql语句快速生成一张备份表,并将学生信息中分数高于60分的学生插入其中?
--查询结果集创建新表:语法:SELECT * INTO 新表名称 FROM 源表名
select * into StudentB from Student where Score>60;
9.使用一条Sql语句将学生“2019005”的分数设置为90?
update Student set Score=90 where StuCode='2019005';
10.使用一条Sql语句快速删除班级表中的数据?
truncate table Classes;
--delete:删除整张表的数据或者某条数据。速度慢
--truncate:只能删除整张表的数据。 快速