方方的我马上要准备面试了,感觉自己脑子里面什么都没有,只能拿出50个sql题先复习一下基础了。。
哎。。。
drop database if exists student_manager;
create database student_manager;
use student_manager;
create table student(
snu varchar(2) primary key,
sname varchar(10) not null,
age date default "1994-12-09",
sex enum('male','famela')
);
create table cource(
cnu varchar(2) primary key,
cname varchar(10) not null,
tnu int
);
create table teacher(
tnu varchar(2) primary key,
tname varchar(10) not null
);
alter table cource
add constraint fk_cource_t foreign key(cnu)
references teacher(tnu) on delete cascade;
create table score(
snu varchar(2) references student(snu),
cnu varchar(2) references cource(cnu),
score float(10,2) not null
);
insert into student(snu,sname,age,sex) values
("01" , "赵雷" , "1990-01-01" , "famela"),
("02" , "钱电" , "1990-12-21" , "famela"),
("03" , "孙风" , "1990-05-20" , "famela"),
("04" , "李云" , "1990-08-06" , "famela"),
("05" , "周梅" , "1991-12-01" , "male"),
("06" , "吴兰" , "1992-03-01" , "male"),
("07" , "郑竹" , "1989-07-01" , "male"),
("08" , "王菊" , "1990-01-20" , "male");
insert into teacher(tnu,tname) values
("01" , "张三"),
("02" , "李四"),
("03" , "王五");
insert into cource(cnu,cname,tnu) values
("01" , "语文" , "02"),
("02" , "数学" , "01"),
("03" , "英语" , "03");
insert into score(snu, cnu, score) values
("01" , "01" , 80.2),
("01" , "02" , 90.5),0
("01" , "03" , 99),
("02" , "01" , 70),
("02" , "02" , 60),
("02" , "03" , 80),
("03" , "01" , 80),
("03" , "02" , 80),
("03" , "03" , 80),
("04" , "01" , 50),
("04" , "02" , 30),
("04" , "03" , 20),
("05" , "01" , 76),
("05" , "02" , 87),
("06" , "01" , 31),
("06" , "03" , 34);
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select * from student join
(select A.snu,A.score s1, B.score s2
from
(select * from score sc where sc.cnu='01') as A cross join
(select * from score sc where sc.cnu='02') as B
where A.snu = B.snu and A.score > B.score) as r
on student.snu=r.snu;
-- 思路
将选择了01课程和02课程的同学分表select出两个表做笛卡尔积,通过where后的条件过滤数据
再与学生表相连接,取出相关记录
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select * from student join
(select A.snu,A.score s1, B.score s2
from
(select * from score sc where sc.cnu='01') as A,
(select * from score sc where sc.cnu='02') as B
where A.snu = B.snu and A.score < B.score) as r
on student.snu=r.snu;
3、查询平均成绩大于等于60分的同学的学生编号、学生姓名和平均成绩
select s.snu,s.sname,round(avg(score),2) as avg_point
from student s
join score
on s.snu = score.snu
group by s.snu
having round(avg(score),2) >= 60;
-- 注意group的用法,结合having过滤数据
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select s.snu,s.sname,round(avg(score),2) as avg_point
from student s
join score
on s.snu = score.snu
group by s.snu
having round(avg(score),2) < 60;
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select s.snu,s.sname,count(1) as course_selection,sum(sc.score) as total_point
from student s
join score sc on (s.snu = sc.snu)
group by s.snu;
6、查询"李"姓老师的数量
select *
from teacher
where tname like '李%';
-- like 模糊查询, %匹配任意多个字符, _匹配任意一个字符。
7、查询学过"张三"老师授课的同学的信息***
select *
from student s
where s.snu in (
select distinct sc.snu
from teacher t
join cource c on (c.tnu=t.tnu)
join score sc on (sc.cnu=c.cnu)
where t.tname like "张三"
);
-- 输出内容只有学生信息,则过滤条件的方式选择子查询 选择distinct去除重复字段
select s.sname,s.snu,sc.score,r.cname,r.tname
from student as s join score as sc
on s.snu = sc.snu
join (
select c.cnu,c.cname,t.tnu,t.tname
from teacher as t join cource as c
on t.tnu = c.tnu
where t.tname like'张三') as r
on sc.cnu = r.cnu
-- 确定表之间的连接关系,已经过滤条件,再做题时划分成小的步骤在做,然后再全部拼接。
–8、查询没学过"张三"老师授课的同学的信息
select *
from student s
where s.snu in (
select distinct sc.snu
from teacher t
join cource c on (c.tnu=t.tnu)
join score sc on (sc.cnu=c.cnu)
where t.tname not like "张三"
);
select s.sname,s.snu,sc.score,r.cname,r.tname
from student as s join score as sc
on s.snu = sc.snu
join (se
select c.cnu,c.cname,t.tnu,t.tname
from teacher as t join cource as c
on t.tnu = c.tnu
where t.tname not like'张三') as r
on sc.cnu = r.cnu
–9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select *
from student
where snu in (
select s1.snu
from score s1,score s2
where s1.snu = s2.snu
and s1.cnu ='01'
and s2.cnu ='02'
);
-- 比较复杂的一种做法
select s.sname,s.snu
from student as s right join
(select A.snu from -- select后的字段应尽量避免重复,要不然后续连接出现重复列可能会报错。
(select * from score as s where s.cnu='01') as A cross join
(select * from score as s where s.cnu='02') as B
where A.snu = B.snu) as r
on s.snu=r.snu;
–10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select *
from student
where snu not in
(select snu from score where cnu ='02') and
snu in
(select snu from score where cnu ='01');
–11、查询没有学全所有课程的同学的信息
select s.sname,sc.snu
from student s,score sc
where s.snu =sc.snu
group by sc.snu
having count(sc.cnu) < 3;
-- 用in时,比较的表中只能存在一列数据。
select s.sname
from student as s
where s.snu in
(select s.snu
from score as s
group by s.snu
having count(1)<3);
–12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select *
from student
where snu in (
select distinct score.snu
from score
where score.cnu in (
select snu from score where snu='01'
)
);
select *
from student
where snu in
(select snu from score
where cnu in
(select cnu from score
where snu ='01'));
–13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select *
from student
where snu in
(select snu from score where snu not in
(select snu from score where cnu not in
(select cnu from score where snu='01'))
group by snu
having count(*)=(select count(*) from score where snu='01') and snu <> '01');
1. 查询01学生选秀课程的cnu
2. 和01学生选择不同课程的同学的snu
–14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student where snu in (
select snu from score where snu not in (
select snu from score
where cnu in (select cnu
from cource as c right join teacher as t
on c.tnu = t.tnu
where tname like '张三')));
–15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select s.snu,s.sname,a.mean from student as s join (
select snu,round(avg(score),2) as mean from score
where score<60
group by snu
having count(distinct cnu) >=2
) as a
on s.snu =a.snu;
–16、检索"01"课程分数小于60,按分数降序排列的学生信息
select s.snu,s.sname,r.score from student as s join
(select *
from score
where cnu='01' and score<60
order by score desc) as r
on s.snu = r.snu;
–17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select snu,round(avg(score),2) as mean, sum(score) as total_point
from score
group by snu
order by mean desc;
–18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
–及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
– if(), case when then else end 别名
–19、按各科成绩进行排序,并显示排名
–20、查询学生的总成绩并进行排名
select snu,sum(score) as total_point
from score
group by snu
order by total_point;
–21、查询不同老师所教不同课程平均分从高到低显示
–22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
–23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
–24、查询学生平均成绩及其名次
–25、查询各科成绩前三名的记录
–26、查询每门课程被选修的学生数
–27、查询出只有两门课程的全部学生的学号和姓名
–28、查询男生、女生人数
select count(1)as sex
from student
group by sex
–29、查询名字中含有"风"字的学生信息
–30、查询同名同性学生名单,并统计同名人数
–31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
–32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号
–33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
–34、查询课程名称为"数学",且分数低于60的学生姓名和分数
–35、查询所有学生的课程及分数情况;
–36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
–37、查询不及格的课程
–38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
–39、求每门课程的学生人数
–40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
–41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
–42、查询每门功成绩最好的前两名
–43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
–44、检索至少选修两门课程的学生学号
–45、查询选修了全部课程的学生信息
–46、查询各学生的年龄
way 1 :select sname , year(now()) - year(age) as age from student;
way 2 :select sn,name,sex,ceil(datediff(now(), age)/365) from student;
–47、查询本周过生日的学生
select dateofweek
–48、查询下周过生日的学生
–49、查询本月过生日的学生
–50、查询下月过生日的学生
面试题:
现有表A数据:
year month amout
1991 1 1.1
1991 2 1.2
1992 1 2.1
1992 2 2.2
如何编写SQL脚本,实现如下的查询结果:
year m1 m2
1991 1.1 1.2
1992 2.1 2.2
select year,
max(if(month=1, amout,0)) as m1,
max(if(month=2, amout,0)) as m2
from A
group by year;