------------恢復(fù)內(nèi)容開(kāi)始------------
表一:student學(xué)生use)
1、create table student(
sno varchar(20) primary key not null comment'學(xué)號(hào)(主碼)',
sname varchar(20) not null comment'學(xué)生姓名',
ssex varchar(20) not null comment'學(xué)生性別',
sbirthday datetime comment'學(xué)生出生年月',
class varchar(20) comment'學(xué)生所在班級(jí)'
);
表(二)Course(課程表)
create table course(
cno varchar(20) primary key not null comment'課程號(hào)(主碼)',
cname varchar(20) not null comment'課程名稱',
tno varchar(20) not null comment'教工編號(hào)'
);
表(三)Score(成績(jī)表)
create table score(
id int primary key auto_increment comment'主鍵自增',
sno varchar(20) not null comment'學(xué)號(hào)',
cno varchar(20) not null comment'課程號(hào)',
degree Decimal(4,1) comment'成績(jī)'
);
表四 teacher(教師表)
create table teacher(
tno varchar(20) primary key not null comment'教工編號(hào)(主碼)',
tname varchar(20) not null comment'教工姓名',
tsex varchar(20) not null comment'教工性別',
tbirthday datetime comment'教工出生年月',
prof varchar(20) comment'職稱',
depart varchar(20) not null comment'教工所在部門(mén)'
學(xué)生表數(shù)據(jù)的插入:
insert into student values
('108','曾華','男','1977-09-01','95033'),
('105','匡明','男','1977-09-01','95031'),
('107','王麗','女','1977-09-01','95033'),
('101','李軍','男','1977-09-01','95033'),
('109','王芳','女','1977-09-01','95031'),
('103','陸君','男','1977-09-01','95031');
課程表數(shù)據(jù)的插入:
insert into course values
('3-105','計(jì)算機(jī)導(dǎo)論','825'),
('3-245','操作系統(tǒng)','804'),
('6-166','數(shù)字電路','856'),
('9-888','高等數(shù)學(xué)','831');
成績(jī)表數(shù)據(jù)的插入:
select cno from score group by cno; #找出這個(gè)表中所有的班級(jí)
insert into score(sno,cno,degree) values
('103','3-245','86'),
('105','3-245','75'),
('109','3-245','68'),
('103','3-105','92'),
('105','3-105','88'),
('109','3-105','76'),
('101','3-105','64'),
('107','3-105','91'),
('108','3-105','78'),
('101','6-166','85'),
('107','6-166','79'),
('108','6-166','81');
教師表數(shù)據(jù)的插入:
insert into teacher values
('804','李誠(chéng)','男','1958-12-02','副教授','計(jì)算機(jī)系'),
('856','張旭','男','1969-03-12','講師','電子工程系'),
('825','王萍','女','1972-05-05','助教','計(jì)算機(jī)系'),
('831','劉冰','女','1977-08-14','助教','電子工程系');
查詢:
1、 查詢Student表中的所有記錄的Sname、Ssex和Class列。
2、查詢教師所有的單位即不重復(fù)的Depart列。
select distinct depart from teacher;
3、 查詢Student表的所有記錄。
select * from student;
4、 查詢Score表中成績(jī)?cè)?0到80之間的所有記錄。
select * from score where degree>60 and degree<80;
5、 查詢Score表中成績(jī)?yōu)?5,86或88的記錄。
select * from score where degree in (85,86,88);
6、 查詢Student表中“95031”班或性別為“女”的同學(xué)記錄。
select * from student where class like '95031' or ssex like '女';
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”班的學(xué)生人數(shù)。
select count(*) from student group by class;
10、 查詢Score表中的最高分的學(xué)生學(xué)號(hào)和課程號(hào)。(子查詢或者排序)
select * from score order by degree desc limit 1;
下面這個(gè)使用的連接查詢
select max(degree) from score #先寫(xiě)出score的最高分
select * from score where degree = (select max(degree) from score);
select sno,cno from score where degree = (select max(degree) from score);
11、 查詢每門(mén)課的平均成績(jī)。
select cno,count(*),avg(degree) from score group by cno having count(*); 多條查詢
12、查詢Score表中至少有5名學(xué)生選修的并以3開(kāi)頭的課程的平均分?jǐn)?shù)。
select cno,avg(degree) from score where cno like '3-105' and cno like '3%';
select * from score group by degree;
select cno,count(*),avg(degree) from score where cno like '3%' group by cno having count(*) >= 5;
select cno,count(*),avg(degree) from score where cno like '3%' group by cno having count(*) >= 5;
13、查詢分?jǐn)?shù)大于70,小于90的Sno列。
select sno,degree from score where degree>70 and degree<90;
select group_concat(sno) from score where degree>70 and degree<90;
14、查詢所有學(xué)生的Sname、Cno和Degree列。(多表查詢)
student.sname,course.cno,score.degree
select student.sname,course.cno,score.degree from student,course,score;
select sname,cno,degree from student join score on student.sno = score.sno;
15、查詢所有學(xué)生的Sno、Cname和Degree列。
student.sno,course.cname,score.degree
select student.sno,course.cname,score.degree from student,course,score;
select sno,cname,degree from score join course on course.cno = score.cno;
16、查詢所有學(xué)生的Sname、Cname和Degree列。
student.sname,course.cname,score.degree;
select student.sname,course.cname,score.degree from student,course,score;
select student.sname,course.cname,score.degree from student,course,score where sname between '李軍' and '王麗';
select student.sname,cname,degree from student join score on student.sno = score.sno join course on course.cno = score.cno;
17、查詢“95033”班學(xué)生的平均分。
select sno from student where class = '95033';
select avg(degree) from score where sno in(select sno from student where class = '95033');
18、查詢選修“3-105”課程的成績(jī)高于“109”號(hào)同學(xué)成績(jī)的所有同學(xué)的記錄。
select degree from score where sno = '109' and cno = '3-105';
select sno,degree from score where degree > (select degree from score where sno = '109' and cno = '3-105');
19、查詢score中選學(xué)多門(mén)課程的同學(xué)中分?jǐn)?shù)為非最高分成績(jī)的記錄。
select cno from score where (cno = '3-245' and cno = '3-105') or (cno = '3-245' and cno = '6-166') or (cno = '3-245' and cno = '6-166'); 這是個(gè)錯(cuò)誤的
select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score b where b.Cno = a.Cno)
select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ))
20、查詢成績(jī)高于學(xué)號(hào)為“109”、課程號(hào)為“3-105”的成績(jī)的所有記錄。
select degree from score where sno = '109' and cno = '3-105';
select * from score where degree > (select degree from score where sno = '109' and cno = '3-105');
21、查詢和學(xué)號(hào)為108的同學(xué)同年出生的所有學(xué)生的Sno、Sname和Sbirthday列。
select sbirthday from student where sno = '108';
select sno,sname,sbirthday from student where sbirthday = (select sbirthday from student where sno = '108');
22、查詢“張旭“教師任課的學(xué)生成績(jī)(姓名)。
select tno from teacher where tname = '張旭'; #找出教師編號(hào)
select cno from course where tno = (select tno from teacher where tname = '張旭'); #找出課程編號(hào)
select sno,degree from score where cno = (select cno from course where tno = (select tno from teacher where tname = '張旭'));
select student.sno,degree,sname from score join student on score.sno = student.sno where student.sno in (select sno from score where cno = (select cno from course where tno = (select tno from teacher where tname = '張旭')));
23、查詢考計(jì)算機(jī)導(dǎo)論的學(xué)生成績(jī)
select cno from course where cname = '計(jì)算機(jī)導(dǎo)論'; #找到課程編號(hào)3-105
select sno,degree from score where cno = (select cno from course where cname = '計(jì)算機(jī)導(dǎo)論');
24、查詢李誠(chéng)老師教的課程名稱
select tno from teacher where tname = '李誠(chéng)'; ##找到教師編號(hào)
select cname from course where tno = (select tno from teacher where tname = '李誠(chéng)');
25、教高等數(shù)學(xué)的老師是哪個(gè)系的
select tno from course where cname = '高等數(shù)學(xué)';
select depart from teacher where tno = (select tno from course where cname = '高等數(shù)學(xué)');
26、查詢選修某課程的同學(xué)人數(shù)多于5人的教師姓名。
select cno,count(*) from score group by cno having count(*)>=5; #找出課程編號(hào)
select tno from course where cno = (select cno from score group by cno having count(*)>=5);
select tname from teacher where tno = (select tno from course where cno = (select cno from score group by cno having count(*)>=5));
27、查詢95033班和95031班全體學(xué)生的記錄。
select * from student group by class having count(*);
select * from student order by class desc;
28、查詢成績(jī)表中存在有85分以上成績(jī)的課程Cno.
select cno,degree from score where degree>85;
29、查詢出“計(jì)算機(jī)系“教師所教課程的成績(jī)表。
select tno,tname from teacher where depart = '計(jì)算機(jī)系' #查出教師編號(hào)
select cno from course where tno in (select tno from teacher where depart = '計(jì)算機(jī)系'); #查出課程編號(hào)
select sno,cno,degree from score where cno in (select cno from course where tno in (select tno from teacher where depart = '計(jì)算機(jī)系'));
30、查詢選修編號(hào)為“3-105”且成績(jī)高于選修編號(hào)為“3-245”課程的同學(xué)的 Cno、Sno和Degree.
select max(degree) from score where cno = '3-245'; #先把選修編號(hào)為3-245課程的同學(xué)的最高成績(jī)查詢出來(lái)
select cno,sno,degree from score where cno = '3-105' and degree > (select max(degree) from score where cno = '3-245');
31、查詢所有教師和同學(xué)的name、sex和birthday.
select tname as name,tsex as sex,tbirthday as birthday from teacher
union
select sname,ssex,sbirthday from student;
32、查詢所有“女”教師和“女”同學(xué)的name、sex和birthday.
select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex = '女'
union
select sname,ssex,sbirthday from student where ssex = '女';
33、查詢所有成績(jī)比3-105課程平均成績(jī)低的同學(xué)的成績(jī)表。
select avg(degree) from score where cno = '3-105';
select degree from score where degree < (select avg(degree) from score where cno = '3-105');
34、查詢所有任課教師的Tname和Depart.
select tname,depart from teacher;
35、查詢所有未講課的教師的Tname和Depart.
select tno from course group by tno; #找出有課的老師的編號(hào)
select tname,depart from teacher where not exists(select tno from course group by tno);
36、查詢至少有2名男生的班號(hào)。#####################################################3
select ssex,class from student where ssex = '男' group by class;
select class from student where exists ((select ssex,class from student where ssex = '男' group by class) * 2);
37、查詢Student表中不姓“王”的同學(xué)記錄。
select sname from student where sname like '王%'
select sname from student where sname not in (select sname from student where sname like '王%');
38、查詢Student表中每個(gè)學(xué)生的姓名和年齡。
select sname,
select floor(datediff(curdate(),@birthday)/365.2422)
39、查詢Student表中最大和最小的Sbirthday日期值。
select max(sbirthday) as '最大日期' , min(sbirthday) as '最小日期' from student;
update student set sbirthday = '1995-07-11' where sno = '108';
update student set sbirthday = '1820-05-01' where sno = '105';
40、以班號(hào)和年齡從大到小的順序查詢Student表中的全部記錄。
select * from student order by class desc,sbirthday desc;
41、查詢“男”教師及其所上的課程。
select tno from teacher where tsex = '男';
select cname from course where tno in (select tno from teacher where tsex = '男');
42、查詢最高分同學(xué)的Sno、Cno和Degree列。
select max(degree) from score
select score.sno,cno,degree from student
join
score
on student.sno = score.sno
where degree = (select max(degree) from score);
43、查詢和“李軍”同性別的所有同學(xué)的Sname.
select ssex from student where sname = '李軍';
select sname from student where ssex = (select ssex from student where sname = '李軍');
44、查詢和“李軍”同性別并同班的同學(xué)Sname.
select class from student where sname = '李軍';
select sname from student where ssex = (select ssex from student where sname = '李軍') and class = (select class from student where sname = '李軍');
45、查詢所有選修“計(jì)算機(jī)導(dǎo)論”課程的“男”同學(xué)的成績(jī)表。
select cno from course where cname = '計(jì)算機(jī)導(dǎo)論'; #根據(jù)課程表找到課程編號(hào)
select sno from score where cno = (select cno from course where cname = '計(jì)算機(jī)導(dǎo)論'); #根據(jù)課程編號(hào)找到成績(jī)表里面的學(xué)生編號(hào)
select sname from student where sno in (select sno from score where cno = (select cno from course where cname = '計(jì)算機(jī)導(dǎo)論')) and ssex = '男';
------------恢復(fù)內(nèi)容結(jié)束------------
來(lái)源:https://www.icode9.com/content-2-595651.html聯(lián)系客服