#建學生信息表studentcreate table studenthtml
(spa
sno varchar(20) not null primary key,操作系統
sname varchar(20) not null,3d
ssex varchar(20) not null,htm
sbirthday datetime,blog
class varchar(20)排序
);ci
#創建教師表create table teacherget
(數學
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
);
#創建課程表coursecreate 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','操做系統','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('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列。
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=85 or degree=86 or degree=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);
11、 查詢每門課的平均成績。
select cno,avg(degree) from score group by cno;
詳解--查詢每門課的平均成績--分組和聚合結合的時候,先分組,而後對每一組分別進行聚合Select AVG(Degree) from Score group by Cno;--先按Cno分組,在對Cno聚合
12、 查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
解法1:select * from score where cno in(select cno from score group by cno having count(*)>=5) and cno like '3%';
解法2:select cno,avg(degree) from score where cno like'3%'group by cno having count(*) >=5;
詳解--select Cno,AVG(Degree) from Score where Cno like '3%' group by Cno having COUNT(*)>=5--篩選分組完了再對每一組進行AVG聚合,分完組以後至關於只剩下分完組的列
13、 查詢分數大於70,小於90的Sno列。
select sno from score group by sno having min(degree)>70 and max(degree)<90
14、 查詢全部學生的Sname、Cno和Degree列。
select student.sname,score.cno,score.degree from student,score where student.sno=score.sno;
詳解--錶鏈接,必定有主外鍵關係:都好隔開兩個表造成笛卡爾積,再進行where篩選,經過表主外鍵關係篩選,where只是個篩選條件
15、查詢全部學生的Sno、Cname和Degree列。
select score.sno,score.degree,course.cname from score,course where score.cno=course.cno;
16、 查詢全部學生的Sname、Cname和Degree列。
解法1:select student.sname,score.degree,course.cname from student,score,course where score.cno=course.cno and score.sno=student.sno;
解法2:select Sname,Cname,Degree from student join Score on Student.Sno=Score.Sno join Course on Score.Cno=Course.Cno
17、 查詢「95033」班學生的平均分。
解法1:select avg(degree) from student,score where student.sno=score.sno and class='95033';
解法2: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列。
解法1:select sno,cno,rank from score,grade where degree between low and upp order by rank;
解法2:select sno,cno,degree,[rank] from Score join grade on degree between low and upp order by [rank]--on後面加篩選條件解法3:select Sno,Cno,(select [rank] from grade where Score.Degree between low and upp) as LV from Score order by Degree desc--子查詢獲得的結果必須惟一
19查詢選修「3-105」課程的成績高於「109」號同窗成績的全部同窗的記錄。
select * from score where cno='3-105' and degree>(select degree from score where sno=109 and cno='3-105');
20、查詢score中選學多門課程的同窗中分數爲非最高分紅績的記錄。
理解1:選學多門課程的同窗中,分數小於全部學生中最高分:
1.select * from score where sno in(2)and degree<(3)
2.select sno from score group by sno having count(*)>1
3.select max(degree) from score where sno in(4)
4.select sno from score group by sno having count(*)>1
結合起來:select * from score where sno in(select sno from score group by sno having count(*)>1) and degree<(select max(degree) from score where sno in(select sno from score group by sno having count(*)>1));
理解2:選多門課程中,分數小於每門課程最高分的學生信息
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 sno='109'and Cno='3-105')
22、查詢和學號爲108的同窗同年出生的全部學生的Sno、Sname和Sbirthday列
select sno,sname,sbirthday from student where year(sbirthday)=(select year(sbirthday) from student where sno='108');
23、查詢「張旭「教師任課的學生成績。
1.select * from score where cno in ()2.select cno from course where tno=()3.select tno from teacher where tname='張旭'
結合起來:
select * from score where cno in (select cno from course where tno=(select tno from teacher where tname='張旭'))
解法1:select teacher.tname,course.cno,score.degree from teacher,course,score where teacher.tno=course.tno and course.cno=score.cno and tname='張旭';
解法2:select degree from Score join Course on Score.Cno=Course.Cno join Teacher on Course.Tno=Teacher.Tno where Tname = '張旭'
24、查詢選修某課程的同窗人數多於3人的教師姓名。
1.select tname from teacher where tno=()2.select tno from course where cno=()3.select cno from score group by cno having count(*)>3
聯合起來:
select tname from teacher where tno=(select tno from course where cno=(select cno from score group by cno having count(*)>3))
解法1;select teacher.tname from teacher,score,course where teacher.tno=course.tno and course.cno=score.cno group by tname having count(*)>3 ;
解法2:select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having COUNT(Cno)>=3))
2五、查詢95033班和95031班全體學生的記錄。
解法1:select * from student where class='95033' or class='95031';
解法2:select*from student inner join Score on Student.Sno=Score.Sno where Class in(‘95033’,‘95031’)
解法3:select *from Student,Score where Class in(95033,95031) and Student.Sno=Score.Sno
select * from(score left join course on score.cno=course.cno)left join student on score.sno=student.sno where class="95033" or class="95031";
26、 查詢存在有85分以上成績的課程Cno.
select score.cno from score,course where score.cno=course.cno and degree>85;
select cno from score group by cno having max(degree)>85;
27、查詢出「計算機系「教師所教課程的成績表
解法1:select degree from teacher,score,course where teacher.tno=course.tno and course.cno=score.cno and teacher.depart='計算機系';
解法2:select *from Score where Cno in( select Cno from Course where Tno in (select Tno from Teacher where Depart='計算機系'))解法3:select Sno,Score.Cno,Degree from Score join Course on Score.Cno=Course.Cno join Teacher on Course.Tno=Teacher.Tno where Depart='計算機系'
2八、查詢「計算 機系」與「電子工程系「不一樣職稱的教師的Tname和Prof。
解法1:select tname,prof from teacher where depart='計算機系' and prof not in (select prof from teacher where depart='電子工程系');
解法2:select tname,prof from teacher where prof not in
(select prof from teacher where depart='電子工程系' and prof in (select prof from teacher where depart='計算機系'))and depart in ('計算機系','電子工程系')--查詢兩個系中教師相同職稱名稱,不在這裏面的就是除去兩個系都有的剩下的,見上面select prof from teacher where depart='電子工程系' and prof in (select prof from teacher where depart='計算機系')
29、查詢選修編號爲「3-105「課程且成績至少高於選修編號爲「3-245」的同窗的Cno、Sno和Degree,並按Degree從高到低次序排序。(any的用法)
解法1;select * from score where cno='3-105' and degree > (select min(degree) from score where cno='3-245')order by degree desc;
解法2:select * from score where cno='3-105' and degree > any(select degree from score where cno='3-245')order by degree desc;
30、查詢選修編號爲「3-105」且成績高於選修編號爲「3-245」課程的同窗的Cno、Sno和Degree.(all的用法)
select * from score where cno='3-105' and degree > (select max(degree) from score where cno='3-245');
select * from score where cno='3-105' and degree > all(select degree from score where cno='3-245');
31、 查詢全部教師和同窗的name、sex和birthday.
select tname,tsex,tbirthday from teacherunionselect sname,ssex,sbirthday from student
32、查詢全部「女」教師和「女」同窗的name、sex和birthday.
select tname,tsex,tbirthday from teacher where tsex='女'union select sname,ssex,sbirthday from student where ssex='女'
33、 查詢成績比該課程平均成績低的同窗的成績表。
select * from score as aa where degree<(select avg(degree) from score as bb where aa.cno=bb.cno);
解法2:詳解--相關子查詢--同一門學科的平均分,,每門學科低於自身平均分的select * from score as a where a.degree<
(select AVG(degree) from score as b where a.cno = b.cno group by cno)--至關於foreach列出每一門課的平均分select * from score as a where a.degree<
(select AVG(degree) from score as b group by cno having a.cno = b.cno)
34、 查詢全部任課教師的Tname和Depart
解法1:select tname,depart from teacher where exists (select * from course where teacher.tno=course.tno);
解法2:select Tname,Depart from Teacher where Tno in(select distinct Tno from Course)
35 、 查詢全部未講課的教師的Tname和Depart.
解法1:select tname,depart from teacher where not exists (select * from course where teacher.tno=course.tno);
36、查詢至少有2名男生的班號。
select class from student where ssex='男'group by class having count(*)>=2;
37、查詢Student表中不姓「王」的同窗記錄。
select * from student where sname not like'王%';
38、查詢Student表中每一個學生的姓名和年齡。
select sname,year(now())-year(sbirthday) from student;
39、查詢Student表中最大和最小的Sbirthday日期值。
select max( date(student.sbirthday)) ,min( date(student.sbirthday)) from student;
40、以班號和年齡從大到小的順序查詢Student表中的所有記錄。
select * from student order by class desc,date(sbirthday) asc;
41、查詢「男」教師及其所上的課程。
解法1:select teacher.tname,teacher.tsex,course.cname from teacher,course where teacher.tno=course.tno and tsex='男';
解法2:select teacher.tno,tname,tsex,cname,cno from teacher join course on teacher.tno=course.tno where tsex='男'
42、查詢最高分同窗的Sno、Cno和Degree列。
select sno,cno,degree from score where degree=(select max(degree) from score);
43、查詢和「李軍」同性別的全部同窗的Sname.
select sname from student where ssex=(select ssex from student where sname='李軍');
44、查詢和「李軍」同性別並同班的同窗Sname.
select sname from student where ssex=(select ssex from student where sname='李軍') and class=(select class from student where sname='李軍');
45、查詢全部選修「計算機導論」課程的「男」同窗的成績表。
select student.sname,score.degree from student,score,course where student.sno=score.sno and course.cno=score.cno and course.cname='計算機導論' and student.ssex='男';
select * from score where sno in(select sno from student where ssex='男') and cno=(select cno from course where cname='計算機導論')