SQL语句练习及答案教学文案.docx
《SQL语句练习及答案教学文案.docx》由会员分享,可在线阅读,更多相关《SQL语句练习及答案教学文案.docx(15页珍藏版)》请在冰豆网上搜索。
![SQL语句练习及答案教学文案.docx](https://file1.bdocx.com/fileroot1/2022-10/28/7214634e-ffc2-45d6-b695-689baaf978fa/7214634e-ffc2-45d6-b695-689baaf978fa1.gif)
SQL语句练习及答案教学文案
sql语句练习题1
数据库有如下四个表格:
student(sno,sname,sage,ssex,sdpt)学生表
系表(dptno,dname)
course(cno,cname,gradet,tno)课程表
sc(sno,cno,score)成绩表
teacher(tno,tname)教师表
要求:
完成以下操作
1.查询姓"欧阳"且全名为三个汉字的学生的姓名。
select sname from student where sname like “欧阳__‟;
2.查询名字中第2个字为"阳"字的学生的姓名和学号。
select sname,sno from student where sname like '_阳%';
3.查询所有不姓刘的学生姓名。
select sname,sno,ssex
from student
where sname not like “刘%”;
4.查询db_design课程的课程号和学分。
select cno,ccreditfrom course
where cname like 'db_design'
5.查询以"db_"开头,且倒数第3个字符为i的课程的详细情况。
select * from course where cname like 'db%i_ _';
6.某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。
查询缺少成绩的学生的学号和相应的课程号。
select sno,cno from sc where grade is null;
7.查所有有成绩的学生学号和课程号。
select sno,cno from sc where grade is not null;
8.查询计算机系年龄在20岁以下的学生姓名。
select sname from student where sdept= 'cs' and sage<20;
9.查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。
select sno,grade from sc where cno= ' 3 ' order by grade desc;
10.查询学生总人数。
select count(*) from student;
11.查询选修了课程的学生人数。
select count(distinct sno) from sc;
12.计算1号课程的学生平均成绩。
select avg(grade) from sc where cno= ' 1 ';
13.查询选修1号课程的学生最高分数。
select max(grade) from sc where cno= ' 1 ';
14.查询学生200215012选修课程的总学分数。
select sum(grade) from sc,course
wheresno= ' 200215012 ' and o=o;
15.查询选修了3门以上课程的学生学号。
select sno from sc group by sno having count(*) >3;
16.查询每个学生及其选修课程的情况。
selectstudent.*,sc.*,course.*fromstudent,sc,course
wherestudent.sno=sc.snoando=o;
17.查询每个学生及其选修课程的情况包括没有选修课程的学生
18.查询选修2号课程且成绩在90分以上的所有学生的学号、姓名
selectstudent.sno,student.sname
fromstudent,sc
wherestudent.sno=sc.snoando=”2‟andsc.grade>90;
19.查询每个学生的学号、姓名、选修的课程名及成绩。
selectstudent.sno,sname,ssex,sage,sdept,cno,grade
fromstudentleftoutjoinscoon(student.sno=sc.sno);
20.查询与“刘晨”在同一个系学习的学生。
selectsno,sname,sdept
fromstudent
wheresdeptin
(selectsdeptfromstudentwheresname=”刘晨‟);
21.查询选修了课程名为“信息系统”的学生学号和姓名
selectsno,snamefromstudentwheresnoin
(selectsnofromscwherecnoin
(selectcnofromcoursewherecname=”信息系统‟));
22.找出每个学生超过他选修课程平均成绩的课程号。
selectsno,cnofromscxwheregrade>=
(selectavg(grade)fromscywherey.sno=x.sno);
23.将一个新学生记录(学号:
200215128;姓名:
陈冬;性别:
男;所在系:
is;年龄:
18岁)插入到student表中。
insertintostudentvalues('200215128','陈冬','男','is',18);
24.将学生200215121的年龄改为22岁。
updatestudentsetsage=22wheresno='200215121';
25.将所有学生的年龄增加1岁。
updatestudentsetsage=sage+1;
26.将计算机科学系全体学生的成绩置零。
updatescsetgrade=0whereexits
(selete*fromstudentwherestudent.sno=sc.snoandsdept=”计算机科学系”);
27.删除学号为20021528的学生记录
deletefromstudentwheresno=”200215128';
28.删除所有的学生选课记录。
deletefromsc;
29.删除2号课程的所有选课记录。
deletefromscwherecno='2';
30.删除计算机科学系所有学生的选课记录。
deletefromscwheresnoin
(seletesnofromstudentwheresdept=”计算机科学系”);
31.建立信息系学生的视图。
createviewis_studentas
selectsno,sname,sagefromstudentwheresdept='is';
sql语句练习题2
设教学数据库education,有三个关系:
学生关系s(sno,sname,age,sex,sdept);
学习关系sc(sno,cno,grade);
课程关系c(cno,cname,cdept,tname)
查询问题:
1:
查所有年龄在20岁以下的学生姓名及年龄。
selectsname,sage
froms
wheresage<20;(notage>=20);
2:
查考试成绩有不及格的学生的学号
selectdistinctsno
fromsc
wheregrade<60;
3:
查所年龄在20至23岁之间的学生姓名、系别及年龄。
selectsname,sdept,sage
froms
wheresagebetween20and23;
4:
查计算机系、数学系、信息系的学生姓名、性别。
selectsname,ssexfromswheresdeptin(‘cs’,’is’,’math’);
5:
查既不是计算机系、数学系、又不是信息系的学生姓名、性别
selectsname,ssexfromswheresdeptnotin(‘cs’,’is’,’math’);
6:
查所有姓“刘”的学生的姓名、学号和性别。
selectsname,sno,ssexfromswheresnamelike‘刘%’;
7:
查姓“上官”且全名为3个汉字的学生姓名。
selectsnamefromswheresnamelike‘上官__’;
8:
查所有不姓“张”的学生的姓名。
selectsname,sno,ssexfromswheresnamenotlike‘张%’;
9:
查db_design课程的课程号。
selectcnofromcwherecnamelike‘db_design’;
10:
查缺考的学生的学号和课程号。
selectsno,cnofromscwheregradeisnull;
11:
查年龄为空值的学生的学号和姓名。
selectsno,snamefromswheresageisnull;
12:
查计算机系20岁以下的学生的学号和姓名。
selectsno,sname
froms
wheresdept=’cs’andsage<20;
13:
查计算机系、数学系、信息系的学生姓名、性别。
selectsname,ssex
froms
wheresdept=’cs’orsdept=’is’orsdept=’math’;
14:
查询选修了c3课程的学生的学号和成绩,其结果按分数的
降序排列。
selectsno,grade
fromsc
wherecno=’c3’
orderbygradedesc;
15:
查询全体学生的情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排列。
select*
froms
orderbysdep,sagedesc;
16:
查询学生总人数。
selectcount(*)froms;
17:
查询选修了课程的学生人数。
selectcount(distinctsno)fromsc
18:
计算选修了c1课程的学生平均成绩。
selectavg(grade)
fromsc
wherecno=’c1’;
19:
查询学习c3课程的学生最高分数。
selectmax(grade)
fromsc
wherecno=’c3’;
20:
查询各个课程号与相应的选课人数。
selectcno,count(sno)
fromsc
groupbycno;
21:
查询计算机系选修了3门以上课程的学生的学号。
selectsno
fromsc
wheresdept=’cs’
groupbysno
havingcount(*)>3;
22:
求基本表s中男同学的每一年龄组(超过50人)有多少人?
要求查询结果按人数升序排列,人数相同按年龄降序排列。
selectsage,count(sno)
froms
wheressex='m'
groupbysage
havingcount(*)>50
orderby2,sagedesc;
23:
查询每个学生及其选修课程的情况。
selects.sno,sname,sage,ssex,sdept,cno,grade
froms,sc
wheres.sno=sc.sno;
24:
查询选修了c2课程且成绩在9