赞
踩
面试经常被问,记录练习方便查看,原文不是mysql,有些许差别,已经逐条验证改为mysql,标题带!的有问题,之后会再修改的
转载自:参考文章
//进入mysql环境
mysql -u root -p
create database wjj;
use wjj;
表说明: student(学生表)、teacher(教师表)、course(课程表)、sc(分数表)
//学生表 create table student( sno varchar(10) primary key, sname varchar(20), sage int(2), ssex varchar(5) ); //教师表 create table teacher( tno varchar(10) primary key, tname varchar(20) ); //课程表 create table course( cno varchar(10), cname varchar(20), tno varchar(20), constraint pk_course primary key (cno,tno) ); //分数表 create table sc( sno varchar(10), cno varchar(10), score int(11), constraint pk_sc primary key (sno,cno) );
/*******初始化学生表的数据******/ insert into student values ('s001','张三',23,'男'); insert into student values ('s002','李四',23,'男'); insert into student values ('s003','吴鹏',25,'男'); insert into student values ('s004','琴沁',20,'女'); insert into student values ('s005','王丽',20,'女'); insert into student values ('s006','李波',21,'男'); insert into student values ('s007','刘玉',21,'男'); insert into student values ('s008','萧蓉',21,'女'); insert into student values ('s009','陈萧晓',23,'女'); insert into student values ('s010','陈美',22,'女'); /******************初始化教师表***********************/ insert into teacher values ('t001', '刘阳'); insert into teacher values ('t002', '谌燕'); insert into teacher values ('t003', '胡明星'); /***************初始化课程表****************************/ insert into course values ('c001','J2SE','t002'); insert into course values ('c002','Java Web','t002'); insert into course values ('c003','SSH','t001'); insert into course values ('c004','Oracle','t001'); insert into course values ('c005','SQL SERVER 2005','t003'); insert into course values ('c006','C#','t003'); insert into course values ('c007','JavaScript','t002'); insert into course values ('c008','DIV+CSS','t001'); insert into course values ('c009','PHP','t003'); insert into course values ('c010','EJB3.0','t002'); /***************初始化成绩表***********************/ insert into sc values ('s001','c001',78.9); insert into sc values ('s002','c001',80.9); insert into sc values ('s003','c001',81.9); insert into sc values ('s004','c001',60.9); insert into sc values ('s001','c002',82.9); insert into sc values ('s002','c002',72.9); insert into sc values ('s003','c002',81.9); insert into sc values ('s001','c003','59');
学生表
教师表
课程表
成绩表
select a.* from
(select * from sc a where a.cno='c001') a,
(select * from sc b where b.cno='c002') b
where a.sno=b.sno and a.score > b.score;
或
select * from sc a
where a.cno='c001'
and exists(select * from sc b where b.cno='c002' and a.score>b.score and a.sno = b.sno)
如果exists里面返回的结果行数大于1,则返回true,则外面的查询数据可以返回。
select sno,avg(score) from sc group by sno having avg(score)>60;
select a.*,s.sname from
(select sno,sum(score),count(cno) from sc group by sno) a ,student s where a.sno=s.sno;
select count(*) from teacher where tname like '刘%';
select a.sno,a.sname from student a where a.sno not in ( select distinct s.sno from sc s, ( select c.* from course c , (select tno from teacher t where tname='谌燕') t where c.tno=t.tno ) b where s.cno = b.cno ); 或 select st.sno,st.sname from student st where st.sno not in ( select distinct sno from sc s join course c on s.cno=c.cno join teacher t on c.tno=t.tno where tname='谌燕' )
select st.sno,st.sname from student st
join sc a on st.sno=a.sno
join sc b on st.sno=b.sno
where a.cno='c002' and b.cno='c001' and a.score < b.score;
select st.*,s.score from student st
join sc s on st.sno=s.sno
join course c on s.cno=c.cno
where s.score <60;
select stu.sno,stu.sname,count(sc.cno) from student stu
left join sc on stu.sno=sc.sno
group by stu.sno,stu.sname
having count(sc.cno)<(select count(distinct cno)from course);
select st.* from student st,
(select distinct a.sno from
(select * from sc) a,
(select * from sc where sc.sno='s001') b
where a.cno=b.cno
) h
where st.sno=h.sno and st.sno<>'s001'
//<>表示!=
“谌燕”老师教的课的平均成绩:
select avg(sc.score) from course a,teacher b ,sc
where a.tno=b.tno and b.tname='谌燕' and a.cno=sc.cno
group by sc.cno
有问题:把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;
update sc c set score=
(
select avg(c.score) from course a,teacher b
where a.tno=b.tno and b.tname='谌燕' and a.cno=c.cno
group by c.cno
)
where cno in( select cno from course a,teacher b where a.tno=b.tno and b.tname='谌燕')
delete from sc where sc.cno in
(
select cno from course c
left join teacher t on c.tno=t.tno
where t.tname='谌燕'
)
select cno ,max(score),min(score) from sc group by cno;
select cno,avg(score),sum(case when score>=60 then 1 else 0 end)/count(*) as 及格率
from sc group by cno
order by avg(score) , 及格率 desc
再加点数据
select t.tno,t.tname,c.cno,c.cname,c.cno,avg(score) from sc , course c,teacher t
where sc.cno=c.cno and c.tno=t.tno
group by c.cno
order by avg(score) desc;
select sc.cno,c.cname,
sum(case when score between 85 and 100 then 1 else 0 end) AS "[100-85]",
sum(case when score between 70 and 85 then 1 else 0 end) AS "[85-70]",
sum(case when score between 60 and 70 then 1 else 0 end) AS "[70-60]",
sum(case when score <60 then 1 else 0 end) AS "[<60]"
from sc, course c
where sc.cno=c.cno
group by sc.cno;
select * from
(select sno,cno,score,row_number()over(partition by cno order by score desc) rn from sc) h
where rn<4
select cno,count(sno)from sc group by cno;
select sc.sno,st.sname,count(cno) from student st
left join sc
on sc.sno=st.sno
group by st.sname,sc.sno having count(cno)=1;
select ssex,count(*)from student group by ssex;
select * from student where sname like '张%';
select sname,count(*)from student group by sname having count(*)>1;
select sno,sname,sage,ssex from student t where sage =1981;
select cno,avg(score) from sc group by cno order by avg(score)asc,cno desc;
select st.sno,st.sname,avg(score) from student st
left join sc on sc.sno=st.sno
group by st.sno,st.sname having avg(score)>85;
select sname,score from student st,sc,course c
where st.sno=sc.sno and sc.cno=c.cno and c.cname='Oracle' and sc.score<60;
select st.sno,st.sname,c.cname from student st,sc,course c
where sc.sno=st.sno and sc.cno=c.cno;
select st.sname,c.cname,sc.score from student st,sc,course c
where sc.sno=st.sno and sc.cno=c.cno and sc.score>70;
select sc.sno,c.cname,sc.score from sc,course c
where sc.cno=c.cno and sc.score<60 order by sc.cno desc;
select st.sno,st.sname,sc.score from sc,student st
where sc.sno=st.sno and cno='c001' and score>80;
select count(distinct sno) from sc;
select st.sname,score from student st,sc ,course c,teacher t
where
st.sno=sc.sno and sc.cno=c.cno and c.tno=t.tno and t.tname='谌燕' and sc.score=
(select max(score)from sc where sc.cno=c.cno)
select cno,count(sno) from sc group by cno;
select a.* from sc a ,sc b where a.score=b.score and a.cno != b.cno;
select * from
(select sno,cno,score from sc group by cno order by score desc) my_rn
limit 0,2;
select cno,count(sno) from sc
group by cno having count(sno)>10
order by count(sno) desc,cno asc;
select sno from sc group by sno having count(cno)>1;
或
select sno from sc group by sno having count(sno)>1;
select distinct(c.cno),c.cname from course c ,sc where sc.cno=c.cno;
或
select cno,cname from course c where c.cno in (select cno from sc group by cno);
select st.sname from student st
where st.sno not in
(select distinct sc.sno from sc,course c,teacher t where sc.cno=c.cno and c.tno=t.tno and t.tname='谌燕');
select sno,avg(score)from sc
where sno in
(
select sno from sc where sc.score<60
group by sno having count(sno)>1
) group by sno;
select sno from sc where cno='c004' and score<90 order by score desc;
delete from sc where sno='s002' and cno='c001';
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。