备注:创建下面的每个题目对应的表,插入案例数据,而后执行须要的
SQL
,将结果复制到
word
文件中
MYSQL经典面试题,后面有建表过程和SQL执行语句html
1、现有数据库
casemanage
中表结构以下图
TABLENAME:afinfo
Id
name
age
birth
sex
memo
1
徐洪国
37
1979-03-23
男
高中
2
王芳芳
26
1988-02-06
女
本科
3
徐晓盛
24
1990-04-02
男
硕士
4
陈晓
30
1984-09-12
女
博士
5
郑凯
27
1987-12-30
男
大专
6
。。。。。。
。。。。。。
。。。。。。
。。。。。。。
。。。。。。
1)
请编写
sql
语句对年龄进行升序排列
select * fromafinfoorder bybirth;
2)
请编写
sql
语句查询对“徐”姓开头的人员名单
select * fromafinfowherenamelike '徐%';
3)
请编写
sql
语句修改“陈晓”的年龄为“
45
”
updateafinfosetage=45 andbirth=birth-YEAR(45) wherename="陈晓";
4)
请编写
sql
删除王芳芳这表数据记录。
delete fromafinfowherename="王芳芳";
2、现有如下几个表
学生信息表(
student
)
姓名name
学号code
张三
001
李四
002
马五
003
甲六
004
考试信息表
(exam)
学号code
学科subject
成绩score
001
数学
80
002
数学
75
001
语文
90
002
语文
80
001
英语
90
002
英语
85
003
英语
80
004
英语
70
1)
查询出全部学生信息,
SQL
怎么编写?
select * from stu;
2)
新学生小明,学号为
005
,须要将信息写入学生信息表,
SQL
语句怎么编写?
insert into stu values ("
小明
",005);
3)
李四语文成绩被登记错误,成绩实际为
85
分,更新到考试信息表中,
SQL
语句怎么编写?
update exam set score=85 where id=(select id from stu where name="
李四
") and subject="
语文
";
4)
查询出各科成绩的平均成绩,显示字段为:学科、平均分,
SQL
怎么编写?
select subject,avg(score) from exam group by subject;
5)查询出全部学生各科成绩,显示字段为:姓名、学号、学科、成绩,并以学号与学科排序,没有成绩的学生也须要列出,SQL怎么编写?
select s.name,s.id,e.subject,e.score from stu s left join exam e on s.id=e.id order by id,subject;
6)
查询出单科成绩最高的,显示字段为:姓名、学号、学科、成绩,
SQL
怎么编写?
select s.name,s.id,e.subject,e.score from stu s join exam e on s.id=e.id where (e.subject,e.score) in (select subject,max(score) from exam group by subject);
7)
列出每位学生的各科成绩,要求输出格式:姓名、学号、语文成绩、数学成绩、英语成绩,
SQL
怎么编写?
3、根据要求写出
SQL
语句。
Student
(
s_no,sname,sage,sex
)学生表
Course
(
c_no,cname,t_no
)课程表
Sc(s_no,c_no,score)
成绩表
Teacher
(
t_no,tname
)教师表
1
、查询“
001
”课程比“
002
”课程成绩高的全部学生的学号。
select a.s_no from (select s_no,score from Sc where c_no='1') a,(select s_no,score from Sc where c_no='2') b where a.score>b.score and a.s_no=b.s_no;
2
、查询平均成绩大于
60
分的同窗的学号和平均成绩。
select s_no,avg(score) from Sc group by s_no having avg(score)>60;
3
、查询全部同窗的学号、姓名、选课数、总成绩。
select Student.s_no,Student.sname,count(Sc.c_no),sum(score) from Student left outer join Sc on Student.s_no=Sc.s_no group by Student.s_no, Student.sname;
4
、查询姓李的老师的个数。
select count(distinct(tname)) from Teacher where tname like '
李
';
5
、查询没学过“叶平”老师课的同窗的学号、姓名
select Student.s_no,Student.sname from Student where s_no not in(select distinct (Sc.s_no) from Sc,Course,Teacher where Sc.s_no=Course.c_no and Teacher.t_no=Course.t_no and Teacher.tname='
叶平
');
6
、查询学过“
001
”而且也学过编号“
002
”课程的同窗的学号、姓名。
select Student.s_no,Student.sname from Student,Sc where Student.s_no=Sc.s_no and Sc.c_no='002' and exists(select * from Sc as Sc1 where Sc.s_no=Sc1.s_no and Sc1.s_no='002');
7
、查询全部课程成绩小于
60
分的同窗的学号、姓名。
select s_no,sname from Student where s_no not in (select S.s_no from Student AS S,Sc where S.s_no=Sc.s_no and score>60);
8
、查询没有学全全部课的同窗的学号、姓名。
select Student.s_no,Student.sname from Student,Sc where Student.s_no=Sc.s_no group by Student.s_no,Student.sname having count(c_no)
10
、查询至少学过学号为“
001
”同窗全部一门课的其余同窗学号和姓名。
select distinct s_no,sname from Student,Sc where Student.s_no=Sc.s_no and Sc.c_no in (select c_no from Sc where s_no='1001');
11
、把“
sc
”表中“叶平”老师教的课的成绩都更改成此课程的平均成绩。
update Sc set score=(select avg(Sc_2.score) from Sc Sc_2 where SC_2.c_no=Sc.c_no ) from Course,Teacher where Course.c_no=Sc.c_no and Course.t_no=Teacher.t_no and Teacher.tname='
叶平
');
12
、查询和“
1002
”号同窗学习的课程彻底相同的其余同窗学号和姓名。
select s_no from Sc where c_no in (select c_no from Sc where s_no='1002') group by s_no having count(*)=(select count(*) from Sc where s_no='1002');
13
、删除学习“叶平”老师课的
sc
表记录。
delete Sc from course,Teacher where Course.c_no=SC.c_no and Course.t_no=Teacher.t_no and tname='
叶平
';
14
、向
sc
表中插入一些记录,这些记录要求符合一下条件:没有上过编号“
003
”课程的同窗学号
insert into Sc select s_no from Student where s_no not in (Select s_no from Sc where c_no='003');
15
、查询各科成绩最高和最低的分:以以下形式显示:课程
ID
,最高分,最低分。
SELECT L.c_no As c_no,L.score AS max_score,R.score AS mix_score FROM Sc L ,Sc AS R
WHERE L.c_no = R.c_no and
L.score = (SELECT MAX(IL.score)
FROM Sc AS IL,Student AS IM
WHERE L.c_no = IL.c_no and IM.s_no=IL.s_no
GROUP BY IL.c_no)
AND
R.Score = (SELECT MIN(IR.score)
FROM Sc AS IR
WHERE R.c_no = IR.c_no
GROUP BY IR.c_no
) order by L.c_no;
16
、查询不一样老师所教不一样课程平均分从高到低显示。
select c_no,avg(score) avg_score from Sc group by c_no order by avg_score desc ;
17
、统计各科成绩,各分数段人数:课程
ID
,课程名称,【
100-85
】,【
85-70
】,【
70-60
】,【
<60
】
select Course.c_no,cname,
count(case when score>85 and score<=100 then score end) '[85-100]',
count(case when score>70 and score<=85 then score end) '[70-85]',
count(case when score>=60 and score<=70 then score end) '[60-70]',
count(case when score<60 then score end) '[<60]'
from Course,Sc
where Course.c_no=Sc.c_no
group by Course.c_no,c_name;
18
、查询每门课程被选修的学生数
select c_no,count(*) from Sc group by c_no;
19
、查询出只选修了一门课程的所有学生的学号和姓名
select Student.s_no,Student.sname,count(c_no) from Student join Sc on Student.s_no=Sc.s_no group by Student.s_no, Student.sname having count(c_no)=1;
20
、查询男生、女生人数
select count(*) from Student group by sex;
21
、查询姓“张”的学生名单
select * from Student where sname like '
张
%';
22
、查询同名同性学生名单,并统计同名人数。
select sname ,count(*) from Student group by sname having count(*)>1;
23
、查询
1994
年出生的学生名单(注:
student
表中
sage
列的类型是
datatime
)
select * from Student where year(curdate())-age='1994';
24
、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列。
select c_no ,avg(score)from Sc group by c_no order by avg(score) asc,c_no desc;
25
、查询平均成绩都大于
85
的全部学生的学号,姓名和平均成绩
select Student.s_no,Student.sname,avg(score) from Student,Sc where Student.s_no=Sc.s_no group by Student.s_no, Student.sname having avg(score)>85;
26
、查询课程名称为“数据库”且分数低于
60
的学生姓名和分数
select Student.sname,Sc.score from Student,Sc where Student.s_no=Sc.s_no and Sc.score<60 and Sc.c_no=(select c_no from Course where cname='
数据库
');
27
、查询全部学生的选课状况
select Student.s_no,Student.sname,Sc.s_no,Course.cname from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no;
28
、查询不及格的课程,并按课程号从大到小排序。
select Student.sname,Sc.c_no,Course.cname,Sc.score from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no and Sc.score<60 order by c_no;
29
、查询课程编号为
003
且课程成绩在
80
分以上的学生的学号和姓名。
select Student.s_no,Student.sname from Student,Sc,Course where Sc.score>80 and Course.c_no='003';
30
、求选修了课程的学生人数。
select count(*) from (select count(*) from Sc group by s_no) b;
31
、查询选修了“冯老师”所授课程的学生中,成绩最高的学生姓名及其成绩。
select Student.sname,Sc.score from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no order by score desc limit 1;
32
、查询各个课程及相应的选修人数。
select Course.c_no,Course.cname,count(s_no) from Course join Sc on Course.c_no=Sc.c_no group by Course.c_no, Course.cname;
33
、查询每门课程最好的前两名。
select a.s_no,a.c_no,a.score from Sc a where (select count(distinct score) from Sc b where b.c_no=a.c_no and b.score>=a.score)<=2 order by a.c_no,a.score desc ;
34
、查询每门课程的学生选修人数(超过
10
人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列。
select Sc.c_no,count(*) from Sc group by c_no having count(*)>10 order by count(*) desc,c_no;
35
、检索至少选修两门课程的学生学号。
select s_no from Sc group by s_no having count(*)>2;
36
、查询所有学生都选修的课程的课程号和课程名。
select Course.c_no,Course.cname from Course join Sc on Course.c_no=Sc.c_no join (select c_no,count(s_no) from Sc group by c_no having count(s_no)=(select count(*) from Student) )as a on Course.c_no=a.c_no;
37
、查询两门以上不及格课程的同窗的学号及其平均成绩。
select s_no,avg(score) from Sc where s_no in (select s_no from Sc where score<60 group by s_no having count(*)>2) group by s_no;
4、根据表
1
和表
2
的信息写出
SQL
表
1
:
主码
列标题
列名
数据类型
宽度
小数位数
是否空值
P
书号
TNO
char
15
no
书名
TNAME
varchar
50
no
做者姓名
TAUTHOR
varchar
8
no
出版社编号
CNO
char
5
yes
书类
TCATEGORY
varchar
20
yes
价格
TPRICE
numeric
8
2
yes
表
2
:出版社表
C
主码
列标题
列名
数据类型
宽度
小数位数
是否空值
p
出版社编号
CNO
char
5
NO
出版社名称
CNAME
varchar
20
NO
出版社电话
CPHONE
varchar
15
YES
出版社城市
CCITY
varchar
20
YES
一、查询出版过“计算机”类图书的出版社编号(若一个出版社出版过多部“计算机”类图书,则在查询结果中该出版社编号只显示一次)
select distinctCNOfromtb4_1whereTCATEGORY='计算机';
二、查询南开大学出版社的“经济”类或“数学”类图书的信息。
select * fromtb4_1,tb4_2wheretb4_1.CNO=tb4_2.CNOandtb4_2.CNAME='南开大学出版社' and (tb4_1.TCATEGORY='经济' ortb4_1.TCATEGORY='数学');
3
、查询编号为“
00001
”的出版社出版图书的平均价格。
select avg(TPRICE) fromtb4_1whereCNO='00001';
4
、查询至少出版过
20
套图书的出版社,在查询结果中按出版社编号的升序顺序显示知足条件的出版社编号、出版社名称和每一个出版社出版的图书套数。
selecttb4_2.CNAMEfromtb4_2,tb4_1wheretb4_1.CNO=tb4_2.CNOand group bytb4_1.CNOhaving count(tb4_1.CNO)>20;
5
、查询比编号为“
00001
”的出版社出版图书套数多的出版社编号。
selectCNOfromtb4_1group byCNOhaving count(*)>(select count(*) fromtb4_1whereCNO='20001');
5、假如现有
A
和
B
两个表,
A
表中包括
ID
、
COL1
、
COL2
、
COL3
等字段,
B
表中包括
ID
、
COL1
、
COL2
、
COL3
、
COL4
、
COL5
等字段,现须要
SQL
把
B
表中
COL1
,
COL2
内容更新到
A
表中
COL1
,
COL2
字段,
ID
为关联字段,要求只能写一个
SQL
。
updatetb5_1,tb5_2settb5_1.COL1=tb5_2.COL1,tb5_1.COL2=tb5_2.COL2wheretb5_1.id=tb5_2.id;
6、用一条
SQL
语句查询出每门课都大于
80
分的学生
name
kecheng
fenshu
张三
语文
81
张三
数学
75
李四
语文
76
李四
数学
90
王五
语文
81
王五
数学
100
王五
英语
90
selecta.namefrom
(selectname,count(*)jige_numfromtb6wherefenshu>80 group byname)a,
(selectname,count(*)kecheng_numfromtb6group byname)b
wherea.name=b.nameandjige_num=kecheng_num;
7、怎么把这样一个表查成这样一个结果
Year
month
amount
1991
1
1.1
1991
2
1.2
1991
3
1.3
1991
4
1.4
1992
1
2.1
1992
2
2.2
1992
3
2.3
1992
4
1.4
year
M1
M2
M3
M4
1991
1.1
1.2
1.3
1.4
1992
2.1
2.2
2.3
2.4
Select year,sum(if(month=1,amount,0))M1,sum(if(month=2,amount,0))M2,sum(if(month=3,amount,0))M3,sum(if(month=4,amount,0))M4fromtb7group by year;
8、已知表
A
记录着登陆
FTP
服务器的计算机
IP
、时间等字段信息
请写出
SQL
查询表
A
中存在
ID
重复三次以上的记录。
selectip_namefromtb8group byip_namehaving count(*)>3;
######################################################################################################################
show databases ;
create databasecasemanageDEFAULT CHARACTER SETutf8;
usecasemanage;
show tables ;
create tableafinfo(idint,namevarchar(20),ageint,birthdatetime,sexvarchar(10),memovarchar(20));
descafinfo;
insert intoafinfovalues (1,"徐洪国",37,19790323,"男","高中");
insert intoafinfovalues (2,"王芳芳",26,19880206,"女","本科");
insert intoafinfovalues (3,"徐晓盛",24,19900402,"男","硕士");
insert intoafinfovalues (4,"陈晓",30,19840912,"女","博士");
insert intoafinfovalues (5,"郑凯",27,19871230,"男","大专");
select * fromafinfo;
#1)请编写sql语句对年龄进行升序排列
#2)请编写sql语句查询对“徐”姓开头的人员名单
#3)请编写sql语句修改“陈晓”的年龄为“45”
#4)请编写sql删除王芳芳这表数据记录。
select * fromafinfoorder bybirth;
select * fromafinfowherenamelike '徐%';
updateafinfosetage=45 andbirth=birth-YEAR(45) wherename="陈晓";
delete fromafinfowherename="王芳芳";
create tablestu(namevarchar(20),idint);
create tableexam(idint,subjectvarchar(20),scoreint);
insert intostuvalues ("张三",001);
insert intostuvalues ("李四",002);
insert intostuvalues ("马五",003);
insert intostuvalues ("甲六",004);
insert intoexamvalues (001,"数学",80);
insert intoexamvalues (002,"数学",75);
insert intoexamvalues (001,"语文",90);
insert intoexamvalues (002,"语文",80);
insert intoexamvalues (001,"英语",90);
insert intoexamvalues (002,"英语",85);
insert intoexamvalues (003,"英语",80);
insert intoexamvalues (004,"英语",70);
select * fromstu;
select * fromexam;
#1)查询出全部学生信息,SQL怎么编写?
#2)新学生小明,学号为005,须要将信息写入学生信息表,SQL语句怎么编写?
#3)李四语文成绩被登记错误,成绩实际为85分,更新到考试信息表中,SQL语句怎么编写?
#4)查询出各科成绩的平均成绩,显示字段为:学科、平均分,SQL怎么编写?
#5)查询出全部学生各科成绩,显示字段为:姓名、学号、学科、成绩,并以学号与学科排序,没有成绩的学生也须要列出,SQL怎么编写?
#6)查询出单科成绩最高的,显示字段为:姓名、学号、学科、成绩,SQL怎么编写?
#7)列出每位学生的各科成绩,要求输出格式:姓名、学号、语文成绩、数学成绩、英语成绩,SQL怎么编写?
select * fromstu;
insert intostuvalues ("小明",005);
updateexamsetscore=85 whereid=(selectidfromstuwherename="李四") andsubject="语文";
selectsubject,avg(score) fromexamgroup bysubject;
selects.name,s.id,e.subject,e.scorefromstu sleft joinexam eons.id=e.idorder byid,subject;
selects.name,s.id,e.subject,e.scorefromstu sjoinexam eons.id=e.idwhere (e.subject,e.score) in (selectsubject,max(score) fromexamgroup bysubject);
selects.name,s.id,sum(if(e.subject='语文',e.score,0)) ,sum(if(e.subject='数学',e.score,0)) ,sum(if(e.subject='英语',e.score,0)) fromstu sleftjoinexam eons.id=e.idgroup bys.name,s.id;
/*3、根据要求写出SQL语句。
Student(s_no,sname,sage,sex)学生表
Course(c_no,cname,t_no)课程表
Sc(s_no,c_no,score)成绩表
Teacher(t_no,tname)教师表
1、查询“001”课程比“002”课程成绩高的全部学生的学号。
2、查询平均成绩大于60分的同窗的学号和平均成绩。
3、查询全部同窗的学号、姓名、选课数、总成绩。
4、查询姓李的老师的个数。
5、查询没学过“叶平”老师课的同窗的学号、姓名
6、查询学过“001”而且也学过编号“002”课程的同窗的学号、姓名。
7、查询全部课程成绩小于60分的同窗的学号、姓名。
8、查询没有学全全部课的同窗的学号、姓名。
10、查询至少学过学号为“001”同窗全部一门课的其余同窗学号和姓名。
11、把“sc”表中“叶平”老师教的课的成绩都更改成此课程的平均成绩。
12、查询和“1002”号同窗学习的课程彻底相同的其余同窗学号和姓名。
13、删除学习“叶平”老师课的sc表记录。
14、向sc表中插入一些记录,这些记录要求符合一下条件:没有上过编号“003”课程的同窗学号
15、查询各科成绩最高和最低的分:以以下形式显示:课程ID,最高分,最低分。
16、查询不一样老师所教不一样课程平均分从高到低显示。
17、统计各科成绩,各分数段人数:课程ID,课程名称,【100-85】,【85-70】,【70-60】,【<60】
18、查询每门课程被选修的学生数
19、查询出只选修了一门课程的所有学生的学号和姓名
20、查询男生、女生人数
21、查询姓“张”的学生名单
22、查询同名同性学生名单,并统计同名人数。
23、查询1994年出生的学生名单(注:student表中sage列的类型是datatime)
24、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列。
25、查询平均成绩都大于85的全部学生的学号,姓名和平均成绩
26、查询课程名称为“数据库”且分数低于60的学生姓名和分数
27、查询全部学生的选课状况
28、查询不及格的课程,并按课程号从大到小排序。
29、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名。
30、求选修了课程的学生人数。
31、查询选修了“冯老师”所授课程的学生中,成绩最高的学生姓名及其成绩。
32、查询各个课程及相应的选修人数。
33、查询每门课程最好的前两名。
34、查询每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列。
35、检索至少选修两门课程的学生学号。
36、查询所有学生都选修的课程的课程号和课程名。
37、查询两门以上不及格课程的同窗的学号及其平均成绩。*/
CREATE DATABASEstu_test2;
usestu_test2;
show tables ;
CREATE TABLEStudent(s_noINT,sname NVARCHAR(32),sageINT,sex NVARCHAR(8));
CREATE TABLECourse(c_noINT,cname NVARCHAR(32),t_noINT );
CREATE TABLESc(s_noINT,c_noINT,scoreINT );
CREATE TABLETeacher(t_noINT,tname NVARCHAR(16) );
select * fromSc;
INSERT INTOStudent
SELECT 1,N'刘一',18,N'男' UNION ALL
SELECT 2,N'钱二',19,N'女' UNION ALL
SELECT 3,N'张三',17,N'男' UNION ALL
SELECT 4,N'李四',18,N'女' UNION ALL
SELECT 5,N'王五',17,N'男' UNION ALL
SELECT 6,N'赵六',19,N'女' ;
INSERT INTOTeacher
SELECT 1,N'叶平' UNION ALL
SELECT 2,N'贺高' UNION ALL
SELECT 3,N'杨艳' UNION ALL
SELECT 4,N'周磊';
INSERT INTOCourseSELECT 1,N'语文',1 UNION ALL
SELECT 2,N'数学',2 UNION ALL
SELECT 3,N'英语',3 UNION ALL
SELECT 4,N'物理',4;
INSERT INTOSc
SELECT 1,1,56 UNION ALL
SELECT 1,2,78 UNION ALL
SELECT 1,3,67 UNION ALL
SELECT 1,4,58 UNION ALL
SELECT 2,1,79 UNION ALL
SELECT 2,2,81 UNION ALL
SELECT 2,3,92 UNION ALL
SELECT 2,4,68 UNION ALL
SELECT 3,1,91 UNION ALL
SELECT 3,2,47 UNION ALL
SELECT 3,3,88 UNION ALL
SELECT 3,4,56 UNION ALL
SELECT 4,2,88 UNION ALL
SELECT 4,3,90 UNION ALL
SELECT 4,4,93 UNION ALL
SELECT 5,1,46 UNION ALL
SELECT 5,3,78 UNION ALL
SELECT 5,4,53 UNION ALL
SELECT 6,1,35 UNION ALL
SELECT 6,2,68 UNION ALL
SELECT 6,4,71;
#一、查询“001”课程比“002”课程成绩高的全部学生的学号;
selecta.s_nofrom (selects_no,scorefromScwherec_no='1')a,(selects_no,scorefromScwherec_no='2')bwherea.score>b.scoreanda.s_no=b.s_no;
#二、查询平均成绩大于60分的同窗的学号和平均成绩;
selects_no,avg(score) fromScgroup bys_nohaving avg(score)>60;
#三、查询全部同窗的学号、姓名、选课数、总成绩;
selectStudent.s_no,Student.sname,count(Sc.c_no),sum(score) fromStudentleft outer joinSconStudent.s_no=Sc.s_nogroup byStudent.s_no,Student.sname;
#四、查询姓“李”的老师的个数;
select count(distinct(tname)) fromTeacherwheretnamelike '李';
#五、查询没学过“叶平”老师课的同窗的学号、姓名;
selectStudent.s_no,Student.snamefromStudentwheres_nonot in(select distinct (Sc.s_no) fromSc,Course,TeacherwhereSc.s_no=Course.c_noandTeacher.t_no=Course.t_noandTeacher.tname='叶平');
#六、查询学过“001”而且也学过编号“002”课程的同窗的学号、姓名;
selectStudent.s_no,Student.snamefromStudent,ScwhereStudent.s_no=Sc.s_noandSc.c_no='002' and exists(select * fromScasSc1whereSc.s_no=Sc1.s_noandSc1.s_no='002');
#七、查询全部课程成绩小于60分的同窗的学号、姓名;
selects_no,snamefromStudentwheres_nonot in (selectS.s_nofromStudentASS,ScwhereS.s_no=Sc.s_noandscore>60);
#八、查询没有学全全部课的同窗的学号、姓名;
selectStudent.s_no,Student.snamefromStudent,ScwhereStudent.s_no=Sc.s_nogroup byStudent.s_no,Student.snamehaving count(c_no)
#十、查询至少有一门课与学号为“1001”的同窗所学相同的同窗的学号和姓名;
select distincts_no,snamefromStudent,ScwhereStudent.s_no=Sc.s_noandSc.c_noin (selectc_nofromScwheres_no='1001');
#十一、把“SC”表中“叶平”老师教的课的成绩都更改成此课程的平均成绩;
updateScsetscore=(select avg(Sc_2.score) fromSc Sc_2whereSC_2.c_no=Sc.c_no) fromCourse,TeacherwhereCourse.c_no=Sc.c_noandCourse.t_no=Teacher.t_noandTeacher.tname='叶平');
#十二、查询和“1002”号的同窗学习的课程彻底相同的其余同窗学号和姓名;
selects_nofromScwherec_noin (selectc_nofromScwheres_no='1002') group bys_nohaving count(*)=(select count(*) fromScwheres_no='1002');
#1三、删除学习“叶平”老师课的SC表记录;
deleteScfromcourse,TeacherwhereCourse.c_no=SC.c_noandCourse.t_no=Teacher.t_noandtname='叶平';
#1四、向sc表中插入一些记录,这些记录要求符合一下条件:没有上过编号“003”课程的同窗学号
insert intoScselects_nofromStudentwheres_nonot in (Selects_nofromScwherec_no='003');
#1五、查询各科成绩最高和最低的分:以以下形式显示:课程ID,最高分,最低分
SELECTL.c_noAsc_no,L.scoreASmax_score,R.scoreASmix_scoreFROMSc L,ScASR
WHEREL.c_no=R.c_noand
L.score= (SELECT MAX(IL.score)
FROMScASIL,StudentASIM
WHEREL.c_no=IL.c_noandIM.s_no=IL.s_no
GROUP BYIL.c_no)
AND
R.Score= (SELECT MIN(IR.score)
FROMScASIR
WHERER.c_no=IR.c_no
GROUP BYIR.c_no
) order byL.c_no;
# 1六、查询不一样老师所教不一样课程平均分从高到低显示。
selectc_no,avg(score)avg_scorefromScgroup byc_noorder byavg_scoredesc ;
# 1七、统计各科成绩,各分数段人数:课程ID,课程名称,【100-85】,【85-70】,【70-60】,【<60】
selectCourse.c_no,cname,
count(case whenscore>85 andscore<=100 thenscoreend) '[85-100]',
count(case whenscore>70 andscore<=85 thenscoreend) '[70-85]',
count(case whenscore>=60 andscore<=70 thenscoreend) '[60-70]',
count(case whenscore<60 thenscoreend) '[<60]'
fromCourse,Sc
whereCourse.c_no=Sc.c_no
group byCourse.c_no,c_name;
# 1八、查询每门课程被选修的学生数
selectc_no,count(*) fromScgroup byc_no;
# 1九、查询出只选修了一门课程的所有学生的学号和姓名
selectStudent.s_no,Student.sname,count(c_no) fromStudentjoinSconStudent.s_no=Sc.s_nogroup byStudent.s_no,Student.snamehaving count(c_no)=1;
# 20、查询男生、女生人数
select count(*) fromStudentgroup bysex;
# 2一、查询姓“张”的学生名单
select * fromStudentwheresnamelike '张%';
# 2二、查询同名同性学生名单,并统计同名人数。
selectsname,count(*) fromStudentgroup bysnamehaving count(*)>1;
# 2三、查询1994年出生的学生名单(注:student表中sage列的类型是datatime)
select * fromStudentwhere year(curdate())-age='1994';
# 2四、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列。
selectc_no,avg(score)fromScgroup byc_noorder by avg(score) asc,c_nodesc;
# 2五、查询平均成绩都大于85的全部学生的学号,姓名和平均成绩
selectStudent.s_no,Student.sname,avg(score) fromStudent,ScwhereStudent.s_no=Sc.s_nogroup byStudent.s_no,Student.snamehaving avg(score)>85;
# 2六、查询课程名称为“数据库”且分数低于60的学生姓名和分数
selectStudent.sname,Sc.scorefromStudent,ScwhereStudent.s_no=Sc.s_noandSc.score<60 andSc.c_no=(selectc_nofromCoursewherecname='数据库');
# 2七、查询全部学生的选课状况
selectStudent.s_no,Student.sname,Sc.s_no,Course.cnamefromStudent,Sc,CoursewhereStudent.s_no=Sc.s_noandSc.c_no=Course.c_no;
# 2八、查询不及格的课程,并按课程号从大到小排序。
selectStudent.sname,Sc.c_no,Course.cname,Sc.scorefromStudent,Sc,CoursewhereStudent.s_no=Sc.s_noandSc.c_no=Course.c_noandSc.score<60 order byc_no;
# 2九、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名。
selectStudent.s_no,Student.snamefromStudent,Sc,CoursewhereSc.score>80 andCourse.c_no='003';
# 30、求选修了课程的学生人数。
select count(*) from (select count(*) fromScgroup bys_no)b;
# 3一、查询选修了“冯老师”所授课程的学生中,成绩最高的学生姓名及其成绩。
selectStudent.sname,Sc.scorefromStudent,Sc,CoursewhereStudent.s_no=Sc.s_noandSc.c_no=Course.c_noorder byscoredesc limit 1;
# 3二、查询各个课程及相应的选修人数。
selectCourse.c_no,Course.cname,count(s_no) fromCoursejoinSconCourse.c_no=Sc.c_nogroup byCourse.c_no,Course.cname;
# 3三、查询每门课程最好的前两名。
selecta.s_no,a.c_no,a.scorefromSc awhere (select count(distinctscore) fromSc bwhereb.c_no=a.c_noandb.score>=a.score)<=2 order bya.c_no,a.scoredesc ;
# 3四、查询每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列。
selectSc.c_no,count(*) fromScgroup byc_nohaving count(*)>10 order by count(*) desc,c_no;
# 3五、检索至少选修两门课程的学生学号。
selects_nofromScgroup bys_nohaving count(*)>2;
# 3六、查询所有学生都选修的课程的课程号和课程名。
selectCourse.c_no,Course.cnamefromCoursejoinSconCourse.c_no=Sc.c_nojoin (selectc_no,count(s_no) fromScgroup byc_nohaving count(s_no)=(select count(*) fromStudent) )asaonCourse.c_no=a.c_no;
# 3七、查询两门以上不及格课程的同窗的学号及其平均成绩。
selects_no,avg(score) fromScwheres_noin (selects_nofromScwherescore<60 group bys_nohaving count(*)>2) group bys_no;
# 4、根据表1和表2的信息写出SQL
create tabletb4_1(TNOchar(15) not null,TNAMEvarchar(50) not null ,TAUTHORvarchar(8) not null,CNOchar(5),TCATEGORYvarchar(20),TPRICEnumeric(8,2));
select * fromtb4_1;
desctb4_1;
drop tabletb4_1;
create tabletb4_2(CNOchar(5) not null,CNAMEvarchar(20) not null,CPHONEvarchar(15),CCITYvarchar(20));
select * fromtb4_2;
desctb4_2;
/*1、查询出版过“计算机”类图书的出版社编号(若一个出版社出版过多部“计算机”类图书,则在查询结果中该出版社编号只显示一次)
2、查询南开大学出版社的“经济”类或“数学”类图书的信息。
3、查询编号为“00001”的出版社出版图书的平均价格。
4、查询至少出版过20套图书的出版社,在查询结果中按出版社编号的升序顺序显示知足条件的出版社编号、出版社名称和每一个出版社出版的图书套数。
5、查询比编号为“00001”的出版社出版图书套数多的出版社编号。*/
select distinctCNOfromtb4_1whereTCATEGORY='计算机';
select * fromtb4_1,tb4_2wheretb4_1.CNO=tb4_2.CNOandtb4_2.CNAME='南开大学出版社' and (tb4_1.TCATEGORY='经济' ortb4_1.TCATEGORY='数学');
select avg(TPRICE) fromtb4_1whereCNO='00001';
selecttb4_2.CNAMEfromtb4_2,tb4_1wheretb4_1.CNO=tb4_2.CNOand group bytb4_1.CNOhaving count(tb4_1.CNO)>20;
selectCNOfromtb4_1group byCNOhaving count(*)>(select count(*) fromtb4_1whereCNO='20001');
/*5、假如现有A和B两个表,A表中包括ID、COL一、COL二、COL3等字段,B表中包括ID、COL一、COL二、COL三、COL四、COL5等字段,
现须要SQL把B表中COL1,COL2内容更新到A表中COL1,COL2字段,ID为关联字段,要求只能写一个SQL。*/
create tabletb5_1(idint,COL1int,COL2int,COL3int);
create tabletb5_2(idint,COL1int,COL2int,COL3int,COL4int,COL5int);
insert intotb5_1
select 11,1,1,1 union all
select 12,1,1,1 union all
select 13,1,1,1 ;
insert intotb5_2
select 21,2,2,2,2,2 union all
select 22,2,2,2,2,2 union all
select 23,2,2,2,2,2 ;
select * fromtb5_1;
select * fromtb5_2;
updatetb5_1,tb5_2settb5_1.COL1=tb5_2.COL1,tb5_1.COL2=tb5_2.COL2wheretb5_1.id=tb5_2.id;
#6、用一条SQL语句查询出每门课都大于80分的学生
create tabletb6(namevarchar(20),kechengvarchar(20),fenshuint);
INSERT INTOtb6
SELECT "张三","语文",81 UNION ALL
SELECT "张三","数学",75 UNION ALL
SELECT "李四","语文",76 UNION ALL
SELECT "李四","数学",90 UNION ALL
SELECT "王五","语文",81 UNION ALL
SELECT "王五","数学",100 UNION ALL
SELECT "王五","英语",90 ;
select * fromtb6;
#用一条SQL语句查询出每门课都大于80分的学生
selecta.namefrom
(selectname,count(*)jige_numfromtb6wherefenshu>80 group byname)a,
(selectname,count(*)kecheng_numfromtb6group byname)b
wherea.name=b.nameandjige_num=kecheng_num;
/*
7、
*/
create tabletb7(year int,month int,amountdouble);
insert intotb7
select 1991,1,1.1 union all
select 1991,2,1.2 union all
select 1991,3,1.3 union all
select 1991,4,1.4 union all
select 1992,1,2.1 union all
select 1992,2,2.2 union all
select 1992,3,2.3 union all
select 1992,4,1.4;
select * fromtb7;
select year,sum(if(month=1,amount,0))M1,sum(if(month=2,amount,0))M2,sum(if(month=3,amount,0))M3,sum(if(month=4,amount,0))M4fromtb7group by year;
/*8、已知表A记录着登陆FTP服务器的计算机IP、时间等字段信息
请写出SQL查询表A中存在ID重复三次以上的记录。
*/
create tabletb8(ip_nameint,qitaint);
drop tabletb8;
insert intotb8
select 1,1 union all
select 2,1 union all
select 2,1 union all
select 2,1 union all
select 1,1 ;
select * fromtb8;
selectip_namefromtb8group byip_namehaving count(*)>3;