MYSQL經典面試題,後面有建表過程和SQL執行語句 html
select * from afinfo order by birth;
select * from afinfo where name like '徐%';
update afinfo set age=45 and birth=birth-YEAR(45) where name="陳曉";
delete from afinfo where name="王芳芳";
select distinct CNO from tb4_1 where TCATEGORY='計算機';
select * from tb4_1,tb4_2 where tb4_1.CNO=tb4_2.CNO and tb4_2.CNAME='南開大學出版社' and (tb4_1.TCATEGORY='經濟' or tb4_1.TCATEGORY='數學');
select avg(TPRICE) from tb4_1 where CNO='00001';
select tb4_2.CNAME from tb4_2,tb4_1 where tb4_1.CNO=tb4_2.CNO and group by tb4_1.CNO having count(tb4_1.CNO)>20;
select CNO from tb4_1 group by CNO having count(*)>(select count(*) from tb4_1 where CNO='20001');
update tb5_1,tb5_2 set tb5_1.COL1=tb5_2.COL1,tb5_1.COL2=tb5_2.COL2 where tb5_1.id=tb5_2.id;
select a.name from
(select name,count(*) jige_num from tb6 where fenshu>80 group by name) a,
(select name,count(*) kecheng_num from tb6 group by name) b
where a.name=b.name and jige_num=kecheng_num;
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)) M4 from tb7 group by year;
select ip_name from tb8 group by ip_name having count(*)>3;
show databases ;
create database casemanage DEFAULT CHARACTER SET utf8 ;
use casemanage;
show tables ;
create table afinfo(id int,name varchar(20),age int,birth datetime,sex varchar(10),memo varchar(20));
desc afinfo;
insert into afinfo values (1,"徐洪國",37,19790323,"男","高中");
insert into afinfo values (2,"王芳芳",26,19880206,"女","本科");
insert into afinfo values (3,"徐曉盛",24,19900402,"男","碩士");
insert into afinfo values (4,"陳曉",30,19840912,"女","博士");
insert into afinfo values (5,"鄭凱",27,19871230,"男","大專");
select * from afinfo;
#1)請編寫sql語句對年齡進行升序排列
#2)請編寫sql語句查詢對「徐」姓開頭的人員名單
#3)請編寫sql語句修改「陳曉」的年齡爲「45」
#4)請編寫sql刪除王芳芳這表數據記錄。
select * from afinfo order by birth;
select * from afinfo where name like '徐%';
update afinfo set age=45 and birth=birth-YEAR(45) where name="陳曉";
delete from afinfo where name="王芳芳";
create table stu(name varchar(20),id int);
create table exam(id int,subject varchar(20),score int);
insert into stu values ("張三",001);
insert into stu values ("李四",002);
insert into stu values ("馬五",003);
insert into stu values ("甲六",004);
insert into exam values (001,"數學",80);
insert into exam values (002,"數學",75);
insert into exam values (001,"語文",90);
insert into exam values (002,"語文",80);
insert into exam values (001,"英語",90);
insert into exam values (002,"英語",85);
insert into exam values (003,"英語",80);
insert into exam values (004,"英語",70);
select * from stu;
select * from exam;
#1)查詢出全部學生信息,SQL怎麼編寫?
#2)新學生小明,學號爲005,須要將信息寫入學生信息表,SQL語句怎麼編寫?
#3)李四語文成績被登記錯誤,成績實際爲85分,更新到考試信息表中,SQL語句怎麼編寫?
#4)查詢出各科成績的平均成績,顯示字段爲:學科、平均分,SQL怎麼編寫?
#5)查詢出全部學生各科成績,顯示字段爲:姓名、學號、學科、成績,並以學號與學科排序,沒有成績的學生也須要列出,SQL怎麼編寫?
#6)查詢出單科成績最高的,顯示字段爲:姓名、學號、學科、成績,SQL怎麼編寫?
#7)列出每位學生的各科成績,要求輸出格式:姓名、學號、語文成績、數學成績、英語成績,SQL怎麼編寫?
select * from stu;
insert into stu values ("小明",005);
update exam set score=85 where id=(select id from stu where name="李四") and subject="語文";
select subject,avg(score) from exam group by subject;
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;
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);
select s.name,s.id ,sum(if(e.subject='語文',e.score,0)) ,sum(if(e.subject='數學',e.score,0)) ,sum(if(e.subject='英語',e.score,0)) from stu sleft join exam e on s.id=e.id group by s.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 DATABASE stu_test2;
use stu_test2;
show tables ;
CREATE TABLE Student(s_no INT, sname NVARCHAR(32), sage INT,sex NVARCHAR(8));
CREATE TABLE Course (c_no INT, cname NVARCHAR(32), t_no INT );
CREATE TABLE Sc(s_no INT, c_no INT, score INT );
CREATE TABLE Teacher ( t_no INT, tname NVARCHAR(16) );
select * from Sc;
INSERT INTO Student
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 INTO Teacher
SELECT 1,N'葉平' UNION ALL
SELECT 2,N'賀高' UNION ALL
SELECT 3,N'楊豔' UNION ALL
SELECT 4,N'周磊';
INSERT INTO Course SELECT 1,N'語文',1 UNION ALL
SELECT 2,N'數學',2 UNION ALL
SELECT 3,N'英語',3 UNION ALL
SELECT 4,N'物理',4;
INSERT INTO Sc
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」課程成績高的全部學生的學號;
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;
#二、查詢平均成績大於60分的同窗的學號和平均成績;
select s_no,avg(score) from Sc group by s_no having avg(score)>60;
#三、查詢全部同窗的學號、姓名、選課數、總成績;
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;
#四、查詢姓「李」的老師的個數;
select count(distinct(tname)) from Teacher where tname like '李';
#五、查詢沒學過「葉平」老師課的同窗的學號、姓名;
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='葉平');
#六、查詢學過「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');
#七、查詢全部課程成績小於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);
#八、查詢沒有學全全部課的同窗的學號、姓名;
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)<(select count(*) from Course);
#十、查詢至少有一門課與學號爲「1001」的同窗所學相同的同窗的學號和姓名;
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');
#十一、把「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='葉平');
#十二、查詢和「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');
#1三、刪除學習「葉平」老師課的SC表記錄;
delete Sc from course,Teacher where Course.c_no=SC.c_no and Course.t_no=Teacher.t_no and tname='葉平';
#1四、向sc表中插入一些記錄,這些記錄要求符合一下條件:沒有上過編號「003」課程的同窗學號
insert into Sc select s_no from Student where s_no not in (Select s_no from Sc where c_no='003');
#1五、查詢各科成績最高和最低的分:以以下形式顯示:課程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;
# 1六、查詢不一樣老師所教不一樣課程平均分從高到低顯示。
select c_no,avg(score) avg_score from Sc group by c_no order by avg_score desc ;
# 1七、統計各科成績,各分數段人數:課程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;
# 1八、查詢每門課程被選修的學生數
select c_no,count(*) from Sc group by c_no;
# 1九、查詢出只選修了一門課程的所有學生的學號和姓名
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;
# 2一、查詢姓「張」的學生名單
select * from Student where sname like '張%';
# 2二、查詢同名同性學生名單,並統計同名人數。
select sname ,count(*) from Student group by sname having count(*)>1;
# 2三、查詢1994年出生的學生名單(注:student表中sage列的類型是datatime)
select * from Student where year(curdate())-age='1994';
# 2四、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列。
select c_no ,avg(score)from Sc group by c_no order by avg(score) asc,c_no desc;
# 2五、查詢平均成績都大於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;
# 2六、查詢課程名稱爲「數據庫」且分數低於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='數據庫');
# 2七、查詢全部學生的選課狀況
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;
# 2八、查詢不及格的課程,並按課程號從大到小排序。
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;
# 2九、查詢課程編號爲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;
# 3一、查詢選修了「馮老師」所授課程的學生中,成績最高的學生姓名及其成績。
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;
# 3二、查詢各個課程及相應的選修人數。
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;
# 3三、查詢每門課程最好的前兩名。
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 ;
# 3四、查詢每門課程的學生選修人數(超過10人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,查詢結果按人數降序排列,若人數相同,按課程號升序排列。
select Sc.c_no,count(*) from Sc group by c_no having count(*)>10 order by count(*) desc,c_no;
# 3五、檢索至少選修兩門課程的學生學號。
select s_no from Sc group by s_no having count(*)>2;
# 3六、查詢所有學生都選修的課程的課程號和課程名。
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;
# 3七、查詢兩門以上不及格課程的同窗的學號及其平均成績。
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
create table tb4_1(TNO char(15) not null,TNAME varchar(50) not null ,TAUTHOR varchar(8) not null,CNO char(5),TCATEGORY varchar(20),TPRICE numeric(8,2));
select * from tb4_1;
desc tb4_1;
drop table tb4_1;
create table tb4_2(CNO char(5) not null,CNAME varchar(20) not null,CPHONE varchar(15),CCITY varchar(20));
select * from tb4_2;
desc tb4_2;
/*1、查詢出版過「計算機」類圖書的出版社編號(若一個出版社出版過多部「計算機」類圖書,則在查詢結果中該出版社編號只顯示一次)
2、查詢南開大學出版社的「經濟」類或「數學」類圖書的信息。
3、查詢編號爲「00001」的出版社出版圖書的平均價格。
4、查詢至少出版過20套圖書的出版社,在查詢結果中按出版社編號的升序順序顯示知足條件的出版社編號、出版社名稱和每一個出版社出版的圖書套數。
5、查詢比編號爲「00001」的出版社出版圖書套數多的出版社編號。*/
select distinct CNO from tb4_1 where TCATEGORY='計算機';
select * from tb4_1,tb4_2 where tb4_1.CNO=tb4_2.CNO and tb4_2.CNAME='南開大學出版社' and (tb4_1.TCATEGORY='經濟' or tb4_1.TCATEGORY='數學');
select avg(TPRICE) from tb4_1 where CNO='00001';
select tb4_2.CNAME from tb4_2,tb4_1 where tb4_1.CNO=tb4_2.CNO and group by tb4_1.CNO having count(tb4_1.CNO)>20;
select CNO from tb4_1 group by CNO having count(*)>(select count(*) from tb4_1 where CNO='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 table tb5_1(id int,COL1 int,COL2 int,COL3 int);
create table tb5_2(id int,COL1 int,COL2 int,COL3 int,COL4 int,COL5 int);
insert into tb5_1
select 11,1,1,1 union all
select 12,1,1,1 union all
select 13,1,1,1 ;
insert into tb5_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 * from tb5_1;
select * from tb5_2;
update tb5_1,tb5_2 set tb5_1.COL1=tb5_2.COL1,tb5_1.COL2=tb5_2.COL2 where tb5_1.id=tb5_2.id;
#6、用一條SQL語句查詢出每門課都大於80分的學生
create table tb6(name varchar(20),kecheng varchar(20),fenshu int);
INSERT INTO tb6
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 * from tb6;
#用一條SQL語句查詢出每門課都大於80分的學生
select a.name from
(select name,count(*) jige_num from tb6 where fenshu>80 group by name) a,
(select name,count(*) kecheng_num from tb6 group by name) b
where a.name=b.name and jige_num=kecheng_num;
/*
7、
*/
create table tb7(year int,month int,amount double);
insert into tb7
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 * from tb7;
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)) M4 from tb7 group by year;
/*8、已知表A記錄着登陸FTP服務器的計算機IP、時間等字段信息
請寫出SQL查詢表A中存在ID重複三次以上的記錄。
*/
create table tb8(ip_name int,qita int);
drop table tb8;
insert into tb8
select 1,1 union all
select 2,1 union all
select 2,1 union all
select 2,1 union all
select 1,1 ;
select * from tb8;
select ip_name from tb8 group by ip_name having count(*)>3;