原文:https://www.cnblogs.com/aqxss/p/6563625.html
一、設(shè)有一數(shù)據(jù)庫剃毒,包括四個(gè)表:學(xué)生表(Student)病袄、課程表(Course)搂赋、成績表(Score)以及教師信息表(Teacher)。四個(gè)表的結(jié)構(gòu)分別如表1-1的表(一)~表(四)所示益缠,數(shù)據(jù)如表1-2的表(一)~表(四)所示脑奠。用SQL語句創(chuàng)建四個(gè)表并完成相關(guān)題目。
表1-1數(shù)據(jù)庫的表結(jié)構(gòu)
表(一)Student (學(xué)生表)
屬性名數(shù)據(jù)類型可否為空含 義
Snovarchar (20)否學(xué)號(hào)(主碼)
Snamevarchar (20)否學(xué)生姓名
Ssexvarchar (20)否學(xué)生性別
Sbirthdaydatetime可學(xué)生出生年月
Classvarchar (20)可學(xué)生所在班級(jí)
表(二)Course(課程表)
屬性名數(shù)據(jù)類型可否為空含 義
Cnovarchar (20)否課程號(hào)(主碼)
Cnamevarchar (20)否課程名稱
Tnovarchar (20)否教工編號(hào)(外碼)
表(三)Score(成績表)
屬性名數(shù)據(jù)類型可否為空含 義
Snovarchar (20)否學(xué)號(hào)(外碼)
Cnovarchar (20)否課程號(hào)(外碼)
DegreeDecimal(4,1)可成績
主碼:Sno+ Cno
表(四)Teacher(教師表)
屬性名數(shù)據(jù)類型可否為空含 義
Tnovarchar (20)否教工編號(hào)(主碼)
Tnamevarchar (20)否教工姓名
Tsexvarchar (20)否教工性別
Tbirthdaydatetime可教工出生年月
Profvarchar (20)可職稱
Departvarchar (20)否教工所在部門
表1-2數(shù)據(jù)庫中的數(shù)據(jù)
表(一)Student
SnoSnameSsexSbirthdayclass
108曾華男1977-09-0195033
105匡明男1975-10-0295031
107王麗女1976-01-2395033
101李軍男1976-02-2095033
109王芳女1975-02-1095031
103陸君男1974-06-0395031
表(二)Course
CnoCnameTno
3-105計(jì)算機(jī)導(dǎo)論825
3-245操作系統(tǒng)804
6-166數(shù)字電路856
9-888高等數(shù)學(xué)831
表(三)Score
SnoCnoDegree
1033-24586
1053-24575
1093-24568
1033-10592
1053-10588
1093-10576
1013-10564
1073-10591
1083-10578
1016-16685
1076-16679
1086-16681
表(四)Teacher
TnoTnameTsexTbirthdayProfDepart
804李誠男1958-12-02副教授計(jì)算機(jī)系
856張旭男1969-03-12講師電子工程系
825王萍女1972-05-05助教計(jì)算機(jī)系
831劉冰女1977-08-14助教電子工程系
#建學(xué)生信息表student
create table student
(
sno varchar(20) not null primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday datetime,
class varchar(20)
);
#建立教師表
create 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
);
#建立課程表course
create table course
(
cno varchar(20) not null primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)
);
#建立成績表
create table score
(
sno varchar(20) not null primary key,
foreign key(sno) references student(sno),
cno varchar(20) not null,
foreign key(cno) references course(cno),
degree decimal
);
#添加學(xué)生信息
insert into student values('108','曾華','男','1977-09-01','95033');
insert into student values('105','匡明','男','1975-10-02','95031');
insert into student values('107','王麗','女','1976-01-23','95033');
insert into student values('101','李軍','男','1976-02-20','95033');
insert into student values('109','王芳','女','1975-02-10','95031');
insert into student values('103','陸君','男','1974-06-03','95031');
#添加教師表
insert into teacher values('804','李誠','男','1958-12-02','副教授','計(jì)算機(jī)系');
insert into teacher values('856','張旭','男','1969-03-12','講師','電子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','計(jì)算機(jī)系');
insert into teacher values('831','劉冰','女','1977-08-14','助教','電子工程系');
#添加課程表
insert into course values('3-105','計(jì)算機(jī)導(dǎo)論','825');
insert into course values('3-245','操作系統(tǒng)','804');
insert into course values('6-166','數(shù)字電路','856');
insert into course values('9-888','高等數(shù)學(xué)','831');
#添加成績表
insert into score values('103','3-245','86');
insert into score values('105','3-245','75');
insert into score values('109','3-245','68');
insert into score values('103','3-105','92');
insert into score values('105','3-105','88');
insert into score values('109','3-105','76');
insert into score values('103','3-105','64');
insert into score values('105','3-105','91');
insert into score values('109','3-105','78');
insert into score values('103','6-166','85');
insert into score values('105','6-166','79');
insert into score values('109','6-166','81');
1幅慌、 查詢Student表中的所有記錄的Sname宋欺、Ssex和Class列。
1selectSname,Ssex,Classfromstudent
2胰伍、 查詢教師所有的單位即不重復(fù)的Depart列齿诞。
1selectdistinctDepartfromteacher
3、 查詢Student表的所有記錄骂租。
1select*fromstudent
4祷杈、 查詢Score表中成績?cè)?0到80之間的所有記錄。
1select*fromScorewhereDegreebetween60and80
5渗饮、 查詢Score表中成績?yōu)?5但汞,86或88的記錄。
1select*fromScorewhereDegreein(85,86,88)
6互站、 查詢Student表中“95031”班或性別為“女”的同學(xué)記錄私蕾。
1select*fromStudentwhereclass='95031'orSsex='女'
7、 以Class降序查詢Student表的所有記錄云茸。
1select*fromstudentorderbyclassdesc
8是目、 以Cno升序、Degree降序查詢Score表的所有記錄标捺。
1select*fromScoreorderbycnoasc,degreedesc
9懊纳、 查詢“95031”班的學(xué)生人數(shù)。
1selectcount(*)fromstudentwhereclass='95031'
10亡容、???? 查詢Score表中的最高分的學(xué)生學(xué)號(hào)和課程號(hào)嗤疯。(子查詢或者排序)
1selectSno,CnofromScorewhereDegree=(selectmax(Degree)fromScore)
1selectSno,CnofromScoreorderbyDegreedesclimit 0,1
11、???? 查詢每門課的平均成績闺兢。
1selectCno,avg(degree)fromScoregroupbyCno
12茂缚、???? 查詢Score表中至少有5名學(xué)生選修的并以3開頭的課程的平均分?jǐn)?shù)。
1selectavg(Degree)fromscorewhereCnolike'3%'andCnoin(selectCnofromscoregroupbyCnohavingcount(*)>=5) 用in不用= 是因?yàn)榭赡軙?huì)有多個(gè)
1簡單寫法:selectavg(Degree)fromscorewhereCnolike'3%'andgroupbyCnohavingcount(*)>=5
13、???? 查詢分?jǐn)?shù)大于70覆积,小于90的Sno列郭怪。
1selectSnofromScorewheredegree>70anddegree<90
14、???? 查詢所有學(xué)生的Sname悔耘、Cno和Degree列。
1selectSname, Cno,DegreefromScore , studentwhereScore.Sno=student.Sno
15我擂、???? 查詢所有學(xué)生的Sno衬以、Cname和Degree列缓艳。
1selectSno,Cname,DegreefromScore , CoursewhereScore.Cno=Course.Cno
16、???? 查詢所有學(xué)生的Sname看峻、Cname和Degree列阶淘。
1selectSname,Cname,Degreefromstudent,course,scorewherestudent.Sno=score.Snoandcourse.Cno=score.Cno
1join..on寫法:selectSname,Cname,Degreefromstudentjoinscoreonstudent.Sno=score.Snojoincourseoncourse.Cno=score.Cno
17、???? 查詢“95033”班學(xué)生的平均分互妓。
1selectavg(degree)as'class=95033'fromScorewhereSnoin(selectSnofromStudentwhereClass='95033')
18溪窒、 假設(shè)使用如下命令建立了一個(gè)grade表:
create table grade(low? int(3),upp? int(3),rank? char(1))
insert into grade values(90,100,’A’)
insert into grade values(80,89,’B’)
insert into grade values(70,79,’C’)
insert into grade values(60,69,’D’)
insert into grade values(0,59,’E’)
現(xiàn)查詢所有同學(xué)的Sno、Cno和rank列车猬。
1selectSno,Cno,rankfromScore,gradewheredegreebetweenlowandupp
19霉猛、查詢選修“3-105”課程的成績高于“109”號(hào)同學(xué)成績的所有同學(xué)的記錄。
1109同學(xué)珠闰,選修是3-105課的
1select*fromscorewhereCno='3-105'anddegree>(selectmax(degree )fromScorewhereSno='109'andCno='3-105')
1109同學(xué)惜浅,沒有選修3-105課
1select*fromscorewhereCno='3-105'anddegree>(selectmax(degree )fromScorewhereSno='109')
anddegree<(selectmax(degree )fromScorewheresnoin(selectSnofromscoregroupbySnohavingcount(*)>1))
1選了多門課程并且是這個(gè)課程下不是最高分的
1select*fromscore awhereSnoin(selectSnofromscoregroupbySnohavingcount(*)>1)anddegree<(selectmax(degree )fromScore bwhereb.cno = a.cno)
21、查詢成績高于學(xué)號(hào)為“109”伏嗜、課程號(hào)為“3-105”的成績的所有記錄坛悉。
1Select*fromscorewheredegree>(selectdegreefromScorewhereSno='109'andCno='3-105')
22、查詢和學(xué)號(hào)為108承绸、101的同學(xué)同年出生的所有學(xué)生的Sno裸影、Sname和Sbirthday列。
1selectsno,sname,sbirthdayfromstudentwhereyear(sbirthday) = (selectyear(sbirthday)fromstudentwheresno='108')
1selectsno,sname,sbirthdayfromstudentwhereyear(sbirthday) = (selectyear(sbirthday)fromstudentwheresno='101')
23军熏、查詢“張旭“教師任課的學(xué)生成績轩猩。
1selectSno,degreefromscore,Coursewherescore.Cno=Course.CnoandCourse.Tno= (selectTnofromTeacherwhereTname='張旭')
1selectdegreefromscorewhereCnoin(selectcnofromcoursewhereTno= (selectTnofromTeacherwhereTname='張旭') )
24、查詢選修某課程的同學(xué)人數(shù)多于5人的教師姓名荡澎。
1selectTnamefromTeacher,? CoursewhereTeacher.Tno=Course.TnoandCourse.Cno =(selectCnofromScoregroupbyCnohavingcount(*)>5)
1selectTnamefromTeacherwheretno=(selectTnofromCoursewherecno=(selectCnofromScoregroupbyCnohavingcount(*)>5 ))
25均践、查詢95033班和95031班全體學(xué)生的記錄。
1select*fromstudentwhereclassin('95033','95031')
26摩幔、? 查詢存在有85分以上成績的課程Cno.
1selectCnofromscorewheredegree>85
27彤委、查詢出“計(jì)算機(jī)系“教師所教課程的成績表。
1select*fromcoursewherecnoin(selectcnofromcoursewheretnoin(selecttnofromteacherwhereDepart='計(jì)算機(jī)系'))
28或衡、查詢“計(jì)算 機(jī)系”與“電子工程系“不同職稱的教師的Tname和Prof焦影。
1selectTname,ProffromTeacherwhereDepart ='計(jì)算機(jī)系'andProfnotin(selectProffromTeacherwhereDepart ='電子工程系')
union
selectTname,ProffromTeacherwhereDepart ='電子工程系'andProfnotin(selectProffromTeacherwhereDepart ='計(jì)算機(jī)系')
29、查詢選修編號(hào)為“3-105“課程且成績至少高于選修編號(hào)為“3-245”的同學(xué)的Cno封断、Sno和Degree,并按Degree從高到低次序排序斯辰。
any:代表括號(hào)中任意一個(gè)成績就可以
1selectCno,Sno,Degreefromscorewherecno='3-105'anddegree >any(selectdegreefromscorewherecno='3-245')orderbydegreedesc
30、查詢選修編號(hào)為“3-105”且成績高于選修編號(hào)為“3-245”課程的同學(xué)的Cno坡疼、Sno和Degree.
all:代表括號(hào)中的所有成績
1selectCno,Sno,Degreefromscorewherecno='3-105'anddegree >all(selectdegreefromscorewherecno='3-245')orderbydegreedesc
31椒涯、?查詢所有教師和同學(xué)的name、sex和birthday.
1selecttname,tsex,tbirthdayfromTeacherunionselectsname,ssex,sbirthdayfromStudent
32回梧、查詢所有“女”教師和“女”同學(xué)的name废岂、sex和birthday.
1selectTname,Tsex,TbirthdayfromTeacherwhereTsex='女'unionselectSname,Ssex,SbirthdayfromStudentwhereSsex='女'
33、?查詢成績比該課程平均成績低的同學(xué)的成績表狱意。
1select*fromscore awheredegree < (selectavg(degree)fromscore bwhereb.cno=a.cno)
34、 查詢所有任課教師的Tname和Depart.
1selectTname,DepartfromTeacherwheretnoin(selecttnofromcourse )
35?藏姐、 查詢所有未講課的教師的Tname和Depart.
1selectTname,DepartfromTeacherwhereTnonotin(selectTnofromCoursewherecnoin(selectcnofromscore? ))
36捌臊、查詢至少有2名男生的班號(hào)曙寡。
1selectclassfromstudentwheressex='男'groupbyclasshavingcount(*)>1
37、查詢Student表中不姓“王”的同學(xué)記錄镀琉。
1select*fromStudentwhereSnamenotlike'王%%'
38凡壤、查詢Student表中每個(gè)學(xué)生的姓名和年齡俗扇。
1selectSname,year(now())-year(sbirthday)fromStudent
39串稀、查詢Student表中最大和最小的Sbirthday日期值橄教。
1selectMax(Sbirthday ),Min(Sbirthday )fromStudent
40、以班號(hào)和年齡從大到小的順序查詢Student表中的全部記錄。
1select*fromStudentorderbyclassdesc, Sbirthday
41、查詢“男”教師及其所上的課程。
1selectTname,Cnamefromcourse,teacherwherecourse.tno= teacher.tnoandteacher.Tsex='男'
42匈仗、查詢最高分同學(xué)的Sno火架、Cno和Degree列骡男。
1selectSno,Cno,Degreefromscorewheredegree=(selectmax(degree)fromscore)
排序?qū)懛ǎ?/p>
1selectSno,Cno,Degreefromscoreorderbydegreedesclimit 0,1
43已亥、查詢和“李軍”同性別的所有同學(xué)的Sname.
1selectSnamefromStudentwhereSsex = (selectSsexfromStudentwhereSname='李軍')
44俱笛、查詢和“李軍”同性別并同班的同學(xué)Sname.
1selectSnamefromStudentwhereSsex = (selectSsexfromStudentwhereSname='李軍')andclass=(selectclassfromstudentwhereSname='李軍')
45捆姜、查詢所有選修“計(jì)算機(jī)導(dǎo)論”課程的“男”同學(xué)的成績表泥技。
1selectSno,Cno,degreefromscorewhereCno=(selectCnofromcoursewhereCname='計(jì)算機(jī)導(dǎo)論')andSnoin(selectSnofromstudentwhereSsex='男')