首页 > 代码库 > SqlServer基础语句练习(一)
SqlServer基础语句练习(一)
学了不少东西,感觉自己的sql语句还是很不好,从基础学起吧。
来一段sql脚本:
create database tongjigouse tongjigocreate table student(Sno varchar(20) not null primary key ,--学号Sname varchar(20) not null,--学生姓名Ssex varchar(20) not null, --学生性别Sbirthday datetime,--学生出生年月Class varchar(20)--学生所在班级)gocreate table teacher--老师(Tno varchar(20) not null primary key ,--教工编号(主码)Tname varchar(20) not null,--教工姓名Tsex varchar(20) not null, --教工性别Tbirthday datetime,--教工出生年月Prof varchar(20),--职称Depart varchar(20) not null--教工所在部门)gocreate table Course--课程(Cno varchar(20) not null primary key ,--课程号Cname varchar(20) not null,--课程名称Tno varchar(20) not null references teacher(Tno), --教工编号(外码))gocreate table Score--分数(Sno varchar(20) not null references student(Sno), --学号(外码)Cno varchar(20) not null references Course(Cno), --课程号(外码)primary key(Sno,Cno),Degree Decimal(4,1),--成绩)
开始写练习sql语句:
-- 1.查询Student表中的所有记录的Sname、Ssex和Class列。SELECT Sname,Ssex,Class FROM student--2.查询教师所有的单位即不重复的Depart列。SELECT DISTINCT depart FROM teacher--3.查询Score表中成绩在60到80之间的所有记录。SELECT * FROM Score WHERE degree BETWEEN 60 AND 80--4.查询Score表中成绩为85,86或88的记录。select * from Score where degree in(85,86,88)--5.查询Student表中“95031”班或性别为“女”的同学记录。select * from student where [class] =‘95031‘ and Ssex=‘女‘--6.以Class降序查询Student表的所有记录。select * from student order by class desc--7.以Cno升序、Degree降序查询Score表的所有记录。select * from Score order by cno,degree desc--8.查询“95031”班的学生人数。select COUNT(*) from student where Class=‘95031‘--9. 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)select Sno,Cno from Score where degree=(select MAX(degree) from score)--10.查询每门课的平均成绩。select cno,AVG(degree) from Score group by Cno--11.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。select AVG(degree) from Score where cno like‘3%‘ group by Cno having COUNT(*)>=5select Avg(degree) from Score group by Cno having COUNT(*)>=5 and Cno like‘3%‘--12.查询分数大于70,小于90的Sno列。select sno from Score where degree>70 and DEGREE<90--13.查询所有学生的Sname、Cno和Degree列。select sname,cno,degree from score join student on score.sno = student.sno--14.查询所有学生的Sno、Cname和Degree列。select sno,cname,degree from score join course on score.cno=course.cno--15.查询所有学生的Sname、Cname和Degree列。select sname,cname,degree from score join student on student.sno=score.sno join course on score.cno = course.cnoselect sname,cname,degree from score,student,course where student.sno=score.sno and score.cno = course.cno
SqlServer基础语句练习(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。