教师资格证上传照片:SQL经典试题(mysql)_沂蒙山好地方

来源:百度文库 编辑:中财网 时间:2024/05/05 02:49:43

3个表:
S(学号(SNO)PK,姓名(SNAME));
C(编号(CNO)PK,名称(CNAME),教师(CTEACHER));
SC((学号(SNO)FK,课程编号(CNO)FK)PK,成绩(SCGRADE));
完成以下查询功能。
1.找出没有报“李明”老师的学生的姓名。
2.列出有2门以上(含2门)课程不及格的学生的姓名和平均成绩。
3.列出学过“1”,“2”号课程的所有学生的姓名。
4.列出1号课程成绩高于2号课程成绩的学生的姓名。
5.列出1号课程成绩高于2号课程成绩的学生的姓名,1号课程成绩,2号课程成绩和平均成绩。
-----------------------------
建表:
create table s(
sno int(3) primary key,
sname varchar(10)
);
-----------------------------
create table c(
cno int(3) primary key,
cname varchar(10),
cteacher varchar(10)
);
----------------------------
create table sc(
sno int(3),
cno int(3),
scgrade int(3),
constraint fk_s foreign key(sno) references s(sno),
constraint fk_c foreign key(cno) references c(cno),
constraint pk_sc primary key(sno,cno)
);
---------------------------------------------------
1、select sname from s where
sno not in(select sno from c,sc where sc.cno=c.cno and cteacher ='李明' );

select sname from s where not
exists (select sno from c,sc where sc.cno=c.cno and s.sno=sc.sno and cteacher ='李明');
---------------------------------------------------
2、select sname,avg(scgrade) from s,sc
where s.sno=sc.sno and
s.sno in(select sno from sc where scgrade<60 group by sno having count(*)>1)
group by s.sno;

select sname,avg(scgrade) from s,sc
where s.sno=sc.sno and
exists (select 'x' from sc a where s.sno=sno and scgrade<60 group by sno having count(*)>1)
group by s.sno;

select sname,avg(scgrade) from sc,s where s.sno=sc.sno and
scgrade<60 group by s.sno having count(*)>1;

求学生平均成绩
select s.sname,avg(scgrade) from s,sc where s.sno=sc.sno group by s.sno;
---------------------------------------------------
3、select a.sname from
    (select sname,s.sno from s,sc where s.sno=sc.sno and cno = 1) a,
    (select sname,s.sno from s,sc where s.sno=sc.sno and cno = 2) b
    where a.sno =b.sno;

select sname from sc,s
where sc.sno=s.sno and cno in(1,2)
group by s.sno having count(*)>1;
---------------------------------------------------
4、select a.sname from
    (select sname,s.sno,scgrade from s,sc where s.sno=sc.sno and cno = 1) a,
    (select sname,s.sno,scgrade from s,sc where s.sno=sc.sno and cno = 2) b
    where a.sno =b.sno and a.scgrade>b.scgrade;
---------------------------------------------------
5、(平均成绩指这两门)
select a.sname,a.scgrade "1",b.scgrade "2",(a.scgrade+b.scgrade)/2 avg from
    (select sname,s.sno,scgrade from s,sc where s.sno=sc.sno and cno = 1) a,
    (select sname,s.sno,scgrade from s,sc where s.sno=sc.sno and cno = 2) b
    where a.sno =b.sno and a.scgrade>b.scgrade;

(平均成绩指该学生所有课程的平均成绩)
select c.sname,c.one,c.two,avg(scgrade) avg from sc,
(select a.sname,a.scgrade one,b.scgrade two,a.sno from
    (select sname,s.sno,scgrade from s,sc where s.sno=sc.sno and cno = 1) a,
    (select sno,scgrade from sc where cno = 2 group by sno) b   
    where a.sno =b.sno and a.scgrade>b.scgrade) c
where c.sno=sc.sno group by c.sno;
---------------------------------------------------
列出学生学过的课程
select s.sname,
min(case when c.cname='java' then c.cname else 'null' end) 'java',
min(case when c.cname='c++' then c.cname else 'null' end) 'c++',
min(case when c.cname='.net' then c.cname else 'null' end) '.net'
from s,c,sc
where s.sno=sc.sno and sc.cno=c.cno
group by s.sname,s.sno;

---------------------------------------------------
添加记录
insert into s values(101,'luo');
insert into s values(102,'li');
insert into s values(103,'ye');
insert into s values(104,'wang');
insert into s values(105,'huo');
insert into s values(106,'luo');
insert into s values(107,'xiao');
insert into s values(108,'ye');
insert into s values(109,'zhang');
insert into s values(110,'chen');
insert into s(sno) values(111);

insert into c values(1,'java','李明');
insert into c values(2,'c++','chen');
insert into c values(3,'.net','luo');

insert into sc values(101,1,78);
insert into sc values(101,2,64);
insert into sc values(101,3,90);
insert into sc values(102,1,35);
insert into sc values(102,3,87);
insert into sc values(103,1,37);
insert into sc values(103,2,53);
insert into sc values(103,3,59);
insert into sc values(104,2,54);
insert into sc values(104,3,88);
insert into sc values(105,1,91);
insert into sc values(105,2,55);
insert into sc values(106,2,90);
insert into sc values(107,2,47);
insert into sc values(107,3,79);
insert into sc values(108,2,83);
insert into sc values(109,1,18);
insert into sc values(110,1,35);
insert into sc values(110,2,56);
insert into sc values(110,3,90);