SQL数据库查询语言练习.docx

上传人:b****6 文档编号:3394387 上传时间:2022-11-22 格式:DOCX 页数:7 大小:18.16KB
下载 相关 举报
SQL数据库查询语言练习.docx_第1页
第1页 / 共7页
SQL数据库查询语言练习.docx_第2页
第2页 / 共7页
SQL数据库查询语言练习.docx_第3页
第3页 / 共7页
SQL数据库查询语言练习.docx_第4页
第4页 / 共7页
SQL数据库查询语言练习.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

SQL数据库查询语言练习.docx

《SQL数据库查询语言练习.docx》由会员分享,可在线阅读,更多相关《SQL数据库查询语言练习.docx(7页珍藏版)》请在冰豆网上搜索。

SQL数据库查询语言练习.docx

SQL数据库查询语言练习

SQL数据库查询语言练习:

一、建立一个数据库表student,数据表computer,字段名name,number,sex,SQL2000,flash,net,其中SQL2000,flash,net设置为数值型数据

1、输出所有男生的成绩

 selectyuyanasSQL数据库,flashas网络动画,netas计算机网络;

fromcomputer;

wheresex

2、输出所有SQL成绩在90以上的女生的成绩

selectSQL2000asSQL数据库fromcomputer;

wherenotsexandSQL2000>=90

3、输出某一科目不合格所有的男生的成绩

selectyuyanasSQL数据库,flashas网络动画,netas计算机网络;

   fromcomputer;

wheresexandSQL2000<60orflash<60ornet<60

4、计算并显示每位同学各科的总分和平均分,并按总分从高到低排序

selectSQL2000+flash+netas总分,(SQL2000+flash+net/3)as平均分;

  fromcomputer;

orderbySQL2000+flash+netdesc

5、输出所有计算机网络成绩在70-79之间的同学

select*fromcomputer;

whereflashbetween70and79

6、输出所有姓“陈”和姓“李”的男生

select*fromcomputer;

wheresexandleft(name,1)in('李','陈')

 或者usestudent

select*fromcomputer

wheresex='男'(andnamelike'李__'ornamelike'陈__')

7、输出所有学号为偶数的同学成绩

selectnumas学号,SQL2000asSQL数据库,flashas网络动画,netas计算机网络;

fromcomputer;

wherenum%2=0

8、输出Flash成绩最好的5位同学

selecttop5*fromcomputerorderbyflashdesc

 

9、更新同学的成绩,把计算机网络成绩在55-59之间的同学该科的成绩调整为60分

updatecomputersetnet=60wherenetbetween55and59

10、删除平均分最低的3位同学

  selecttop3*,(SQL2000+flash+net)/3as平均分fromcomputer;

  orderby(SQL2000+flash+net)/3

  deletefromcomputerwherenumberin(033001,033003,033011)

11、统计成绩表中平均分为90以上(含90分)人数

selectcount(*)fromcomputerwhere(SQL2000+flash+net)/3>=90

二、

问题描述:

已知关系模式:

s(sno,sname)                      学生关系。

sno为学号,

sname为姓名

c(cno,cname,cteacher)  课程关系。

cno为课程号,

cname为课程名,

cteacher为任课教师

sc(sno,cno,scgrade)选课关系。

scgrade为成绩

要求实现如下5个处理:

  1.找出没有选修过“李明”老师讲授课程的所有学生姓名

  2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩

  3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名

  4.列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号

  5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩

 

1.找出没有选修过“李明”老师讲授课程的所有学生姓名

--实现代码:

selectsnamefroms;

wherenotexists;

   (select*fromsc,c;

whereo=oandc.cteacher='李明'andsc.sno=s.sno)

2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩

--实现代码:

selects.sno,s.sname,avg_scgrade=avg(sc.scgrade);

froms,sc,(;

    selectsnofromsc;

    wherescgrade<60;

    groupbysnohavingcount(distinctcno)>=2;

)awheres.sno=a.snoandsc.sno=a.sno;

groupbys.sno,s.sname

3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名

--实现代码:

selects.sno,s.sname;

froms,(;

    selectsc.sno;

    fromsc,c;

    whereo=o;

        andamein('1','2');

    groupbysno;

    havingcount(distinctcno)=2;

)scwheres.sno=sc.sno

 

4.列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号

--实现代码:

selects.sno,s.sname;

froms,scsc1,scsc2;

    whereo='1';

        andsc2.sno='2';

        ando=o;

        andsc1.scgrade>sc2.scgrade

5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩

--实现代码:

selectsc1.sno,[1号课成绩]=sc1.scgrade,[2号课成绩]=sc2.scgrade;

fromscsc1,scsc2;

whereo='1';

        ando='2';

        andsc1.sno=sc2.sno;

        andsc1.scgrade>sc2.scgrade

练习题目

  1、查询Student表中的所有记录的Sname、Ssex和Class列。

  2、查询教师所有的单位即不重复的Depart列。

  3、查询Student表的所有记录。

  4、查询Score表中成绩在60到80之间的所有记录。

  5、查询Score表中成绩为85,86或88的记录。

  6、查询Student表中“95031”班或性别为“女”的同学记录。

  7、以Class降序查询Student表的所有记录。

  8、以Cno升序、Degree降序查询Score表的所有记录。

  9、查询“95031”班的学生人数。

  10、查询Score表中的最高分的学生学号和课程号。

  11、查询‘3-105’号课程的平均分。

  12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

  13、查询最低分大于70,最高分小于90的Sno列。

  14、查询所有学生的Sname、Cno和Degree列。

  15、查询所有学生的Sno、Cname和Degree列。

  16、查询所有学生的Sname、Cname和Degree列。

  17、查询“95033”班所选课程的平均分。

  18、假设使用如下命令建立了一个grade表:

    createtablegrade(low  number(3,0),upp  number(3),rank  char

(1));

    insertintogradevalues(90,100,’A’);

    insertintogradevalues(80,89,’B’);

    insertintogradevalues(70,79,’C’);

    insertintogradevalues(60,69,’D’);

    insertintogradevalues(0,59,’E’);

    commit;

    现查询所有同学的Sno、Cno和rank列。

  19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

  20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。

  21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

  22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

  23、查询“张旭“教师任课的学生成绩。

  24、查询选修某课程的同学人数多于5人的教师姓名。

  25、查询95033班和95031班全体学生的记录。

  26、查询存在有85分以上成绩的课程Cno.

  27、查询出“计算机系“教师所教课程的成绩表。

  28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

  29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按  

    Degree从高到低次序排序。

  30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

  31、查询所有教师和同学的name、sex和birthday.

  32、查询所有“女”教师和“女”同学的name、sex和birthday.

  33、查询成绩比该课程平均成绩低的同学的成绩表。

  34、查询所有任课教师的Tname和Depart.

  35、查询所有未讲课的教师的Tname和Depart.

  36、查询至少有2名男生的班号。

  37、查询Student表中不姓“王”的同学记录。

  38、查询Student表中每个学生的姓名和年龄。

  39、查询Student表中最大和最小的Sbirthday日期值。

  40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

  41、查询“男”教师及其所上的课程。

  42、查询最高分同学的Sno、Cno和Degree列。

  43、查询和“李军”同性别的所有同学的Sname.

  44、查询和“李军”同性别并同班的同学Sname.

  45、查询所有选修“计算机导论”课程的“男”同学的成绩表

SQL语句练习题参考答案

  1、selectSname,Ssex,ClassfromStudent;

  2、selectdistinctdepartfromteacher;

  3、selectSnoas'学号',Snameas'姓名',Ssexas'性别',Sbirthdayas'出生日期',Classas'班号'fromstudent;

    或selectSnoas学号,Snameas姓名,Ssexas性别,Sbirthdayas出生日期,Classas班号fromstudent;

  4、select*fromscorewheredegreebetween60and80;或select*fromscorewheredegree>=60anddegree<=80;

  5、select*fromscorewheredegreein(85,86,88);

  6、select*fromstudentwhereclass='95031'orSsex='女';

  7、select*fromstudentorderbyclassdesc;

  8、select*fromscoreorderbycnoasc,degreedesc;或select*fromscoreorderbycno,degreedesc;

  9、selectcount(*)asCNTfromstudentwhereclass='95031';

  10、selectSnoas'学号',cnoas'课程号',degreeas'最高分'fromscorewheredegree=(selectmax(degree)from   

    score)

  11、selectavg(degree)as课程平均分fromscorewherecno='3-105';

  12、selectcno,avg(degree)fromscorewherecnolike'3%'groupbycnohaving  count(*)>5;

  13、selectSnofromscoregroupbySnohavingmin(degree)>70andmax(degree)<90;

  14、selectstudent.Sname,score.Cno,score.degreefromstudent,scorewherestudent.Sno=score.Sno;

  15、selectx.Sno,y.Cname,x.degreefromscorex,courseywherex.Cno=y.Cno;

  16、selectx.Sname,y.Cname,z.degreefromstudentx,coursey,scorezwherex.Sno=z.Snoandz.Cno=y.Cno;

  17、selecty.Cno,avg(y.degree)fromstudentx,scoreywherex.Sno=y.Snoandx.class='95033'groupbyo;

  18、selectSno,Cno,rankfromscore,gradewheredegreebetweenlowandupporderbyrank;

  19、selectx.Cno,x.Sno,x.degreefromscorex,scoreywhereo='3-105'andx.degree>y.degreeand   

    y.sno='109'ando='3-105';

  20、

    1,查询成绩非本科最高select*  fromscoreb  wheredegree<(selectmax(degree)fromscoreawhere    

     o=o);

    2,查询成绩非本科最高并且选2门以上的学生的成绩:

  21、selecto,x.Sno,x.degreefromscorex,scoreywherex.degree>y.degreeandy.sno='109'ando='3-105';

    selectcno,sno,degreefromscore  wheredegree>(selectdegreefromscorewheresno='109'andcno='3-105')

  22、selectsno,sname,sbirthdayfromstudentwhereto_char(sbirthday,'yyyy')=(selectto_char(sbirthday,'yyyy')   

    fromstudentwheresno='108');

  23、selectcno,sno,degreefromscorewherecno=(selectofromcoursex,teacherywherex.tno=y.tnoand

    y.tname='张旭');

  24、selecttnamefromteacherwheretnoin(selectx.tnofromcoursex,scoreywhereo=ogroupbyx.tno

    havingcount(x.tno)>5);

  25、select*fromstudentwhereclassin('95033','95031');

  26、selectdistinctcnofromscorewheredegreein(selectdegreefromscorewheredegree>85);

  27、select*fromscorewherecnoin(selectofromcoursex,teacherywherey.tno=x.tnoandy.depart='计算

    机系');

  28、selecttname,proffromteacherwheredepart='计算机系'andprofnotin(selectproffromteacherwhere

    depart='电子工程系');

  29、select*fromscorewherecno='3-105'anddegree>any(selectdegreefromscorewherecno='3-245')orderby

    degreedesc;

  30、select*fromscorewherecno='3-105'anddegree>all(selectdegreefromscorewherecno='3-245');

  31、selecttname,tsex,tbirthdayfromteacher

    unionselectsname,ssex,sbirthdayfromstudent;

  32、selecttname,tsex,tbirthdayfromteacherwheretsex='女'

    unionselectsname,ssex,sbirthdayfromstudentwheressex='女';

  33、select*fromscoreawheredegree<(selectavg(degree)

    fromscorebwhereo=o);

  34、selecttname,departfromteacherawhereexists

    (select*fromcoursebwherea.tno=b.tno);

  35、selecttname,departfromteacherawherenotexists

    (select*fromcoursebwherea.tno=b.tno);

  36、selectclassfromstudentwheressex='男'groupbyclasshavingcount(*)>=2;

  37、select*fromstudentwheresnamenotlike'王_';

  38、selectsnameas姓名,(to_char(sysdate,'yyyy')-to_char(sbirthday,'yyyy'))as年龄fromstudent

  39、selectsname,sbirthdayas最大fromstudentwhere  sbirthday=(selectmin(sbirthday)fromstudent)

    unionselectsname,sbirthdayas最小fromstudentwheresbirthday=(selectmax(sbirthday)fromstudent) 

  40、selectclass,sname,sbirthdayfromstudentorderbyclassdesc,sbirthday;

  41、selectx.tname,amefromteacherx,courseywherex.tno=y.tnoandx.tsex='男';

  42、select*fromscorewheredegree=(selectmax(degree)fromscore);

  43、selectsnamefromstudentwheressex=(selectssexfromstudentwheresname='李军');

  44、selectsnamefromstudentwheressex=(selectssexfromstudentwheresname='李军')andclass=(selectclass  

    fromstudentwheresname='李军');

  45、select*fromscorewheresnoin(selectsnofromstudentwheressex='男')andcno=(selectcnofromcourse

    wherecname='计算机导论');

本文档部分内容来源于网络,如有内容侵权请告知删除,感谢您的配合!

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1