MySQL查詢語句的45道練習

原文:https://www.cnblogs.com/aqxss/p/6563625.html

一、設有一數(shù)據(jù)庫,包括四個表:學生表(Student)、課程表(Course)、成績表(Score)以及教師信息表(Teacher)。四個表的結(jié)構(gòu)分別如表1-1的表(一)~表(四)所示,數(shù)據(jù)如表1-2的表(一)~表(四)所示。用SQL語句創(chuàng)建四個表并完成相關(guān)題目。

1-1數(shù)據(jù)庫的表結(jié)構(gòu)

表(一)Student (學生表)

屬性名數(shù)據(jù)類型可否為空含 義

Snovarchar (20)否學號(主碼)

Snamevarchar (20)否學生姓名

Ssexvarchar (20)否學生性別

Sbirthdaydatetime可學生出生年月

Classvarchar (20)可學生所在班級

表(二)Course(課程表)

屬性名數(shù)據(jù)類型可否為空含 義

Cnovarchar (20)否課程號(主碼)

Cnamevarchar (20)否課程名稱

Tnovarchar (20)否教工編號(外碼)

表(三)Score(成績表)

屬性名數(shù)據(jù)類型可否為空含 義

Snovarchar (20)否學號(外碼)

Cnovarchar (20)否課程號(外碼)

DegreeDecimal(4,1)可成績

主碼:Sno+ Cno

表(四)Teacher(教師表)

屬性名數(shù)據(jù)類型可否為空含 義

Tnovarchar (20)否教工編號(主碼)

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計算機導論825

3-245操作系統(tǒng)804

6-166數(shù)字電路856

9-888高等數(shù)學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副教授計算機系

856張旭男1969-03-12講師電子工程系

825王萍女1972-05-05助教計算機系

831劉冰女1977-08-14助教電子工程系

#建學生信息表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

);

#添加學生信息

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','副教授','計算機系');

insert into teacher values('856','張旭','男','1969-03-12','講師','電子工程系');

insert into teacher values('825','王萍','女','1972-05-05','助教','計算機系');

insert into teacher values('831','劉冰','女','1977-08-14','助教','電子工程系');

#添加課程表

insert into course values('3-105','計算機導論','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ù)學','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、 查詢教師所有的單位即不重復的Depart列。

1selectdistinctDepartfromteacher

3、 查詢Student表的所有記錄。

1select*fromstudent

4、 查詢Score表中成績在60到80之間的所有記錄。

1select*fromScorewhereDegreebetween60and80

5、 查詢Score表中成績?yōu)?5,86或88的記錄。

1select*fromScorewhereDegreein(85,86,88)

6、 查詢Student表中“95031”班或性別為“女”的同學記錄。

1select*fromStudentwhereclass='95031'orSsex='女'

7、 以Class降序查詢Student表的所有記錄。

1select*fromstudentorderbyclassdesc

8、 以Cno升序、Degree降序查詢Score表的所有記錄。

1select*fromScoreorderbycnoasc,degreedesc

9、 查詢“95031”班的學生人數(shù)。

1selectcount(*)fromstudentwhereclass='95031'

10、???? 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)

1selectSno,CnofromScorewhereDegree=(selectmax(Degree)fromScore)

1selectSno,CnofromScoreorderbyDegreedesclimit 0,1

11、???? 查詢每門課的平均成績。

1selectCno,avg(degree)fromScoregroupbyCno

12、???? 查詢Score表中至少有5名學生選修的并以3開頭的課程的平均分數(shù)。

1selectavg(Degree)fromscorewhereCnolike'3%'andCnoin(selectCnofromscoregroupbyCnohavingcount(*)>=5) 用in不用= 是因為可能會有多個

1簡單寫法:selectavg(Degree)fromscorewhereCnolike'3%'andgroupbyCnohavingcount(*)>=5

13、???? 查詢分數(shù)大于70,小于90的Sno列。

1selectSnofromScorewheredegree>70anddegree<90

14、???? 查詢所有學生的Sname、Cno和Degree列。

1selectSname, Cno,DegreefromScore , studentwhereScore.Sno=student.Sno

15、???? 查詢所有學生的Sno、Cname和Degree列。

1selectSno,Cname,DegreefromScore , CoursewhereScore.Cno=Course.Cno

16、???? 查詢所有學生的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”班學生的平均分。

1selectavg(degree)as'class=95033'fromScorewhereSnoin(selectSnofromStudentwhereClass='95033')

18、 假設使用如下命令建立了一個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)查詢所有同學的Sno、Cno和rank列。

1selectSno,Cno,rankfromScore,gradewheredegreebetweenlowandupp

19、查詢選修“3-105”課程的成績高于“109”號同學成績的所有同學的記錄。

1109同學,選修是3-105課的

1select*fromscorewhereCno='3-105'anddegree>(selectmax(degree )fromScorewhereSno='109'andCno='3-105')

1109同學,沒有選修3-105課

1select*fromscorewhereCno='3-105'anddegree>(selectmax(degree )fromScorewhereSno='109')

anddegree<(selectmax(degree )fromScorewheresnoin(selectSnofromscoregroupbySnohavingcount(*)>1))

1選了多門課程并且是這個課程下不是最高分的

1select*fromscore awhereSnoin(selectSnofromscoregroupbySnohavingcount(*)>1)anddegree<(selectmax(degree )fromScore bwhereb.cno = a.cno)

21、查詢成績高于學號為“109”、課程號為“3-105”的成績的所有記錄。

1Select*fromscorewheredegree>(selectdegreefromScorewhereSno='109'andCno='3-105')

22、查詢和學號為108、101的同學同年出生的所有學生的Sno、Sname和Sbirthday列。

1selectsno,sname,sbirthdayfromstudentwhereyear(sbirthday) = (selectyear(sbirthday)fromstudentwheresno='108')

1selectsno,sname,sbirthdayfromstudentwhereyear(sbirthday) = (selectyear(sbirthday)fromstudentwheresno='101')

23、查詢“張旭“教師任課的學生成績。

1selectSno,degreefromscore,Coursewherescore.Cno=Course.CnoandCourse.Tno= (selectTnofromTeacherwhereTname='張旭')

1selectdegreefromscorewhereCnoin(selectcnofromcoursewhereTno= (selectTnofromTeacherwhereTname='張旭') )

24、查詢選修某課程的同學人數(shù)多于5人的教師姓名。

1selectTnamefromTeacher,? CoursewhereTeacher.Tno=Course.TnoandCourse.Cno =(selectCnofromScoregroupbyCnohavingcount(*)>5)

1selectTnamefromTeacherwheretno=(selectTnofromCoursewherecno=(selectCnofromScoregroupbyCnohavingcount(*)>5 ))

25、查詢95033班和95031班全體學生的記錄。

1select*fromstudentwhereclassin('95033','95031')

26、? 查詢存在有85分以上成績的課程Cno.

1selectCnofromscorewheredegree>85

27、查詢出“計算機系“教師所教課程的成績表。

1select*fromcoursewherecnoin(selectcnofromcoursewheretnoin(selecttnofromteacherwhereDepart='計算機系'))

28、查詢“計算 機系”與“電子工程系“不同職稱的教師的Tname和Prof。

1selectTname,ProffromTeacherwhereDepart ='計算機系'andProfnotin(selectProffromTeacherwhereDepart ='電子工程系')
union
selectTname,ProffromTeacherwhereDepart ='電子工程系'andProfnotin(selectProffromTeacherwhereDepart ='計算機系')

29、查詢選修編號為“3-105“課程且成績至少高于選修編號為“3-245”的同學的Cno、Sno和Degree,并按Degree從高到低次序排序。

any:代表括號中任意一個成績就可以

1selectCno,Sno,Degreefromscorewherecno='3-105'anddegree >any(selectdegreefromscorewherecno='3-245')orderbydegreedesc

30、查詢選修編號為“3-105”且成績高于選修編號為“3-245”課程的同學的Cno、Sno和Degree.

all:代表括號中的所有成績

1selectCno,Sno,Degreefromscorewherecno='3-105'anddegree >all(selectdegreefromscorewherecno='3-245')orderbydegreedesc

31、?查詢所有教師和同學的name、sex和birthday.

1selecttname,tsex,tbirthdayfromTeacherunionselectsname,ssex,sbirthdayfromStudent

32、查詢所有“女”教師和“女”同學的name、sex和birthday.

1selectTname,Tsex,TbirthdayfromTeacherwhereTsex='女'unionselectSname,Ssex,SbirthdayfromStudentwhereSsex='女'

33、?查詢成績比該課程平均成績低的同學的成績表。

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名男生的班號。

1selectclassfromstudentwheressex='男'groupbyclasshavingcount(*)>1

37、查詢Student表中不姓“王”的同學記錄。

1select*fromStudentwhereSnamenotlike'王%%'

38、查詢Student表中每個學生的姓名和年齡。

1selectSname,year(now())-year(sbirthday)fromStudent

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

1selectMax(Sbirthday ),Min(Sbirthday )fromStudent

40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。

1select*fromStudentorderbyclassdesc, Sbirthday

41、查詢“男”教師及其所上的課程。

1selectTname,Cnamefromcourse,teacherwherecourse.tno= teacher.tnoandteacher.Tsex='男'

42、查詢最高分同學的Sno、Cno和Degree列。

1selectSno,Cno,Degreefromscorewheredegree=(selectmax(degree)fromscore)

排序?qū)懛ǎ?/p>

1selectSno,Cno,Degreefromscoreorderbydegreedesclimit 0,1

43、查詢和“李軍”同性別的所有同學的Sname.

1selectSnamefromStudentwhereSsex = (selectSsexfromStudentwhereSname='李軍')

44、查詢和“李軍”同性別并同班的同學Sname.

1selectSnamefromStudentwhereSsex = (selectSsexfromStudentwhereSname='李軍')andclass=(selectclassfromstudentwhereSname='李軍')

45、查詢所有選修“計算機導論”課程的“男”同學的成績表。

1selectSno,Cno,degreefromscorewhereCno=(selectCnofromcoursewhereCname='計算機導論')andSnoin(selectSnofromstudentwhereSsex='男')

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容