天天看點

MySQL查詢語句練習題(三)

設有一資料庫,包括四個表:學生表(Student)、課程表(Course)、成績表(Score)以及教師資訊表(Teacher)。表結構及資料如下,請完成題目。

表(一)Student (學生表) :

MySQL查詢語句練習題(三)
 表(二)Course(課程表):
MySQL查詢語句練習題(三)
 表(三)Score(成績表):
MySQL查詢語句練習題(三)
 表(四)Teacher(教師表):
MySQL查詢語句練習題(三)

 表1-2資料庫中的資料:

表(一)Student:

MySQL查詢語句練習題(三)
 表(二)Course:
MySQL查詢語句練習題(三)
 表(三)Score:
MySQL查詢語句練習題(三)
 表(四)Teacher:
MySQL查詢語句練習題(三)

#建學生資訊表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,
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','作業系統','804');
insert into course values('6-166','數字電路','856');
insert into course values('9-888','高等數學','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('101','3-105','64');

insert into score values('107','3-105','91');

insert into score values('108','3-105','78');

insert into score values('101','6-166','85');

insert into score values('107','6-166','79');

insert into score values('108','6-166','81');

1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。

#單表查詢
select Sname,Ssex,Class from Student;      

2、 查詢教師所有的機關即不重複的Depart列。

#單表查詢
select distinct Depart from teacher;      

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

#單表查詢
select * from student      

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

#單表查詢
select * from Score where Degree between 60 and 80;      

5、 查詢Score表中成績為85,86或88的記錄。

#單表查詢
select * from Score where Degree in (85,86,88);      

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

#單表查詢
select * from Student where Class= '95031' or Ssex='女';      

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

#單表查詢
select * from Student order by Class desc;      

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

#單表查詢
select * from Score order by Cno asc,Degree desc;      

9、 查詢“95031”班的學生人數。

#單表查詢
select count(*) from Student where Class = '95031';      

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

#單表查詢
select Sno,Cno from Score where Degree = (
select max(Degree) from Score);
select Sno,Cno from Score order by Degree desc limit 0,1;      

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

#單表查詢
select Cno,avg(Degree) from Score group by Cno;      

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

#單表查詢
select Cno,avg(Degree) from Score 
where Cno like '3%'
group by Cno 
having count(Sno) >=5;

select avg(Degree) from score where Cno like '3%' and Cno in (select Cno from score group by Cno having count(*)>=5);      

13、查詢分數大于70,小于90的Sno列。

#單表查詢
select Sno from Score where Degree >70 and Degree < 90;      

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

#多表查詢
#可以用where,内連接配接,子查詢
select Sname,Cno,Degree from Student,Score where Student.Sno=Score.Sno;

select Sname,Cno,Degree from Student inner join Score
on Student.Sno=Score.Sno;      

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

#多表查詢
#同上
select Sno,Cname,Degree from Score,Course where Score.Cno=Course.Cno;      

16、查詢所有學生的Sname、Cname和Degree列。

#多表查詢
#同上
select Sname,Cname,Degree from student,course,score where student.Sno=score.Sno and course.Cno=score.Cno;

select Sname,Cname,Degree from Student inner join Score on Student.Sno=Score.Sno inner join 
Course on Score.Cno=Course.Cno;      

17、查詢“95033”班學生的平均分。

#多表查詢
#用where,内連接配接,子查詢
select avg(Degree) from Score,Student where Student.Sno=Score.Sno and Class='95033';

select avg(Degree) from Score inner join Student on Student.Sno=Score.Sno where Class='95033';

select avg(Degree) from Score where Sno in (select Sno from Student
where Class='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’)      

現查詢所有同學的Sno、Cno和rank列。

#多表查詢
select Sno,Cno,rank from Score,grade where Degree between low and upp;      

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

#單表查詢
#109同學,選修是3-105課的
select * from score where Cno='3-105' and degree>(select max(degree ) from Score where Sno='109' and Cno='3-105' );
#109同學,沒有選修3-105課
select * from Score where Cno = '3-105' and Degree > (
select max(Degree) from Score where Sno ='109')and degree<( select max(degree ) from Score where sno in (select Sno from score group by Sno having count(*)>1));
#選了多門課程并且是這個課程下不是最高分的
select * from score a where Sno in (select Sno from score group by Sno having count(*)>1) and degree<( select max(degree ) from Score b where b.cno = a.cno);      

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

#單表查詢
select * from Score where Degree >(
select Degree from Score where Cno = '109' and Course = '3-105');      

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

#單表查詢
select Sno,Sname,Sbirthday from Student where year(Sbirthday) in (
select year(Sbirthday) from Student where Sno='108' or '101');      

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

#多表查詢
#用where,内連接配接,子查詢
select Sno,Degree from Score where Cno in (
select Cno from Course where Tno in(
select Tno from Teacher where Tname = '張旭'));

select Sno,Degree from Score,Course,Teacher where
Score.Cno=Course.Cno and Course.Tno=Teacher.Tno and Tname = '張旭' ;

select Sno,Degree from Score inner join Course on Score.Cno=Course.Cno inner join Teacher on Teacher.Tno=Course.Tno where Tname ='張旭' ;      

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

#多表查詢
#用where,内連接配接,子查詢
select Tname from Teacher where Tno in (
select Tno from Course where Cno in (
select Cno from Score group by Cno having count(Sno)>5));

select Tname from Teacher, Course where Teacher.Tno=Course.Tno and Course.Cno =(select Cno from Score group by Cno having count(*)>5);      

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

#多表查詢
#用where,内連接配接,子查詢
select * from Student,Score,Course where Student.Sno=Score.Sno and Score.Cno=Course.Cno and class = '95033' or '95031';      

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

select Cno from Score where Degree >85;      

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

#多表查詢
#用where,内連接配接,子查詢
select * from Score where Cno in (
select Cno from Course where Tno in (
select Tno from Teacher where Depart = '計算機系'));
#先找交集再not in
select Score.Sno,Score.Cno,Score.Degree from Score,Course,Teacher where Score.Cno=Course.Cno and Course.Tno=Teacher.Tno and Depart = '計算機系';

select Score.Sno,Score.Cno,Score.Degree from Score inner join Course on Score.Cno=Course.Cno inner join Teacher on Teacher.Tno =Course.Tno where Depart ='計算機系';      

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

select Tname,Prof from Teacher where Prof not in (
select Prof from Teacher where Prof in (select Prof from Teacher where Depart = '計算機系') and Depart = '電子工程系');

select Tname,Prof from Teacher where Prof not in (
select Prof from Teacher where Depart = '計算機系' or Depart ='電子工程系' group by Prof having count(*)>1);

select Tname,Prof from Teacher where Depart ='計算機系' and Prof not in( select Prof from Teacher where Depart ='電子工程系')
union 
select Tname,Prof from Teacher where Depart ='電子工程系' and Prof not in( select Prof from Teacher where Depart ='計算機系');      

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

select Cno,Sno,Degree from Score where Cno = '3-105' and Degree >
any(select Degree from Score where Cno = '3-245') order by Degree desc;

select Cno,Sno,Degree from Score where Cno = '3-105' and Degree >
(select min(Degree) from Score where Cno = '3-245') order by Degree desc;
#大于任何一個或者大于最小的      

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

select Cno,Sno,Degree from Score where Cno = '3-105' and Degree > all(select Degree from Score where Cno = '3-245');      

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

select Sname as '學生',Ssex,Sbirthday from Student
union
select Tname as '老師',Tsex,Tbirthday from Teacher;      

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

select Sname,Ssex,Sbirthday from Student where Ssex = '女'
union
select Tname,Tsex,Tbirthday from Teacher where Tsex = '女';      

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

select * from score a where degree < (
select avg(degree) from score b where b.cno=a.cno);
1
2
這是一種特殊形式的父子表連接配接(自連接配接)SQL選擇查詢寫法。對于這種特殊的寫法,資料庫引擎會以特殊的方式檢索父查詢表裡的資料。如果搞不清楚這種特殊的檢索方式,我們很難從該SQL語句的表面邏輯理出個中道理。
現在我們來分拆該SQL語句裡的父查詢和子查詢
1)語句中的父查詢
select * from score a where degree<”子查詢獲得的一個資料值“
2)語句中的子查詢
select avg(degree) from score b where a.cno=b.cno
請注意這個子查詢的from子句裡隻有一張表 b ,但是where子句裡卻出現了第二張表 a ,
如果單獨運作這個子查詢,因為子查詢沒有列出表a,系統會要求輸入a.cno或者直接報錯,反正無法順利執行,但是表a可以在父查詢裡的from子句中找到,面對這種情況資料庫引擎會采取逐條取主查詢記錄與子查詢實施比對以确定是否檢出該條記錄,最後彙總各次檢索的結果輸出整個記錄集。
這個特殊的SQL語句檢索過程大緻如下:
取出首條記錄的a.cno用作過濾,子查詢裡以avg函數得到該課程的平均分,主查詢以分數比對平均分,滿足條件保留否則抛棄(degree小于平均分的留下);
跟着判斷父查詢表下一條記錄,處理過程相同,最後合并各次判斷結果進而的到最終結果。
這種特殊的寫法可以規避輸出包含非分組字段,而分組不得輸出非分組字段的沖突。      

34、 查詢所有任課教師的Tname和Depart。

select Tname,Depart from Teacher where Tno in (
select Tno from Course);      

35 、查詢所有未講課的教師的Tname和Depart。

select Tname,Depart from Teacher where Tno not in (
select Tno from Course where Cno in (
select Cno from Score ));      

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

select Class from Student where Ssex = '男'
group by Class 
having count(Ssex) >1;      

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

select * from Student where Sname not like '王%%';      

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

select Sname,year(now())-year(Sbirthday) from Student;      

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

select max(Sbirthday),min(Sbirthday) from Student;      

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

select * from Student order by Class desc,Sbirthday;      

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

#需要兩個表的資訊用where或者連接配接查詢
select Cno,Cname,Tname from Course,Teacher where Course.Tno=Teacher.Tno and Tsex='男';

select Cno,Cname,Tname from Course inner join Teacher on Course.Tno=Teacher.Tno where Tsex='男';      

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

select Sno,Cno,Degree from Score order by Degree desc limit 0,1;

select Sno,Cno,Degree from Score where Degree = (
select max(Degree) from Score);      
select Sname from Student where Ssex = (
select Ssex from Student where Sname = '李軍');      
select Sname from Student where Ssex = (
select Ssex from Student where Sname = '李軍')
and Class = (
select Class from Student where Sname = '李軍');      
select * from Score where Cno = (
select Cno from Course where Cname = '計算機導論') and
Sno in (select Sno from Student where Ssex = '男');      
IT
上一篇: MySQL事務

繼續閱讀