drop database if exists newTest;
create database newTest;
use newTest;
create table student(
sno varchar(10) primary key,
sname varchar(20),
sage int,
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 decimal(4,2),
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 s.sno from sc c,sc s
where c.sno=s.sno
and c.cno= ‘c001’ and s.cno = ‘c002’ and c.score>s.score;
select sno,avg(score) from sc group by sno
having avg(score)>60;
select s.sno,s.sname,count(c.cno),sum(c.score)
from student s left join sc c on s.sno=c.sno group by sno;
select s.sno,s.sname,t.cno_count,t.total_score
from student s
left join (
select sno, count(cno) cno_count, sum(score) total_score
from sc
group by sno) t on s.sno=t.sno;
select count(*)from teacher where tname like ‘刘%’;
select sno,sname from student where sno not in(
select distinct st.sno from teacher t
join course c on t.tno=c.tno
join sc s on c.cno=s.cno
join student st on s.sno=st.sno
where t.tname=’谌燕’);
select sno ,sname from student s where sno not in(
#学过谌燕老师的课程的学号
select sc.sno from sc ,teacher t,course c where sc.cno=c.cno and c.tno=t.tno and t.tname=’谌燕’);
select sno,sname from student
where sno in (select sno from sc where sno in
(select sno from sc where cno=’c001’)and cno=’c002’);
select a.sno,s.sname from sc a,sc b ,student s
where a.sno=b.sno and b.sno=s.sno
and a.cno=’c001’ and b.cno=’c002’;
select distinct st.sno,st.sname
from teacher t join course c on t.tno=c.tno
join sc s on c.cno=s.cno
join student st on s.sno=st.sno
where t.tname=’谌燕’ group by s.sno having count(s.cno)=4;
select count(c.cno) from teacher t
join course c on t.tno=c.tno where t.tname=’谌燕’;
select s.sno,s.sname
from student s join sc c on s.sno=c.sno
group by sno having max(score)<60;
select t.sno,t.sname from student t
join sc s on t.sno=s.sno
join course c on s.cno=c.cno
group by s.sno having count(s.cno)<10;
select count(course.cname)from course ;
select distinct s.sno,s.sname from sc,student s where sc.cno in(
select cno from sc where sno=’s001’)
and sc.sno=s.sno and sc.sno!=’s001’;
update sc a join (select cno,round(avg(score),2) ascore from sc group by cno) b on a.cno=b.cno
set a.score = b.ascore
where a.cno in (select cno from course c join teacher t on c.tno=t.tno and t.tname=’谌燕’) ;
select t.sno,t.sname from sc,student t where cno in
(select cno from sc where sc.sno=’s001’) and t.sno!=’s001’ and t.sno=sc.sno
group by sc.sno having count(cno)=(select count(cno) from sc where sc.sno=’s001’)
and count(cno)=(select count(cno) from sc b where b.sno=sc.sno group by sno );
delete from sc where cno in
(select c.cno from course c,teacher t where c.tno=t.tno and t.tname=’谌燕’);
select cno,max(score),min(score) from sc group by cno;
select cno,100*sum(case when score>60 then 1 else 0 end)/
count(sno) rate from sc group by cno order by rate desc;
select sno,cno,score
from sc r1
where (select count(1) from sc r2
where r2.cno=r1.cno and r2.score >= r1.score) <=3;
select c.cno,count(s.sno)
from course c left join sc s on c.cno=s.cno group by cno;
select t.sno,t.sname from student t join sc s on t.sno=s.sno group by sno having count(s.cno)=1;
select m.boy_count,n.girl_count
from(
select count(*) boy_count from student where ssex=’男’) m,(
select count(*) girl_count from student where ssex=’女’ ) n;
select count(*) from student group by ssex;
select * from student where sname like ‘张%’;
select s.sname,count(s.sno) from student s join(
select distinct a.sno, a.sname, a.ssex from student a,student b
where a.sname = b.sname and a.sno!=b.sno and a.ssex=b.ssex) b on s.sno=b.sno
where s.sname=b.sname
group by s.sname;?
select * from student where year(current_timestamp)-‘1981’=sage;
select *from student where year(date_sub(now(),interval sage year)) =1994;
select distinct sc.cno,b.a from sc
join (select cno, avg(score) a from sc group by cno) b
on sc.cno=b.cno order by b.a asc,sc.cno desc;
select cno, avg(score) from sc group by cno order by avg(score) asc,cno desc;
select t.sno,t.sname,avg(s.score)
from student t join sc s on t.sno=s.sno group by sno
having avg(score)>85;
select t.sname,s.score
from student t join sc s on t.sno=s.sno
join course c on c.cno=s.cno where c.cname=’数据库’ and s.score<60;
select t.sno,t.sname,t.sage,t.ssex,c.cname from
student t left join sc s on t.sno=s.sno
left join course c on c.cno=s.cno;
select t.sname,c.cname,s.score
from student t join sc s on t.sno=s.sno
join course c on c.cno=s.cno and s.score>70;
select cno
from sc where score<60 order by cno desc;
select t.sno,t.sname,s.score
from student t join sc s on t.sno=s.sno
where s.cno=’c001’ and s.score>80;
select count(b.sno) from(
select distinct sno from sc) b;
select t.sname,max(s.score)
from student t join sc s on t.sno=s.sno
join course c on c.cno=s.cno
join teacher h on h.tno=c.tno where h.tname=’谌燕’group by s.sno;
select c.cno,c.cname,count(s.sno)
from course c left join sc s on c.cno=s.cno group by c.cno;
select sno,cno,score
from sc r1
where (select count(1) from sc r2
where r2.cno=r1.cno and r2.score >= r1.score) <=2;
select sc.cno, b.a from sc join(
select cno,sno,count(sno) a from sc group by cno having count(sno)>10) b
on sc.cno=b.cno group by sc.cno having count(sc.sno)=count(b.sno)
order by b.a desc,sc.cno asc ;
select cno,sno,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)>=2;
select c.cno,c.cname from course c join sc s on c.cno=s.cno
group by s.cno having count(s.sno)=(
select count(sno)from student);
select sname from student where sno not in(
select distinct st.sno from teacher t
join course c on t.tno=c.tno
join sc s on c.cno=s.cno
join student st on s.sno=st.sno
where t.tname=’谌燕’);
select c.sno,avg(c.score) from(select s.sno,s.cno,s.score from sc s where s.score<60
group by s.cno) c group by c.sno having count(c.cno)>=2;
select sc.sno,avg(sc.score) from sc where sc.score<60 group by sno having count(c.cno)>=2;
select sno from sc
where cno=’c004’ and score<60 order by score desc;
delete from sc where sno=’s002’ and cn0=’c001’;