sql綜合練習


create table student(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
ssex varchar2(5)
);
create table teacher(
tno varchar2(10) primary key,
tname varchar2(20)
);
create table course(
cno varchar2(10),
cname varchar2(20),
tno varchar2(20),
constraint pk_course primary key (cno,tno)
);
create table sc(
sno varchar2(10),
cno varchar2(10),
score number(4,2),
constraint pk_sc primary key (sno,cno)
);數據庫

表的介紹:
 學生表student : sno學號  sname學生姓名  sage學生年齡    ssex學生性別
 教師表teacher : tno教師編號  tname教師姓名
 課程表course : cno課程編號  cname課程名稱  tno該課程對應的老師編號  
 成績表sc :sno對應學生的編號  cno對應課程的編號   score成績分數 學習

數據
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,'女');
commit;spa

insert into teacher values ('t001', '劉陽');
insert into teacher values ('t002', '諶燕');
insert into teacher values ('t003', '胡明星');
commit;orm

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');
commit;ip

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');
commit;ci

一、查詢「c001」課程比「c002」課程成績高的全部學生的學號;
 select sno from sc
WHERE score> some(
select score FROM sc
WHERE cno='c002')
and cno='c001';it


二、查詢平均成績大於60 分的同窗的學號和平均成績;
select avg_score ,sno from(
select avg(score) avg_score , sno from sc
group by sno)
where avg_score >60;
三、查詢全部同窗的學號、姓名、選課數、總成績;
select sname,st.sno,A,score from student st join (
select sno,score,s.cno,a from sc s join (
select cno ,count(*) a from course
group by cno) b
on s.cno in b.cno) c
on st.sno =c.sno;table

四、查詢姓「劉」的老師的個數;
select  count(*) from teacher
where tname='劉陽';
五、查詢沒學過「諶燕」老師課的同窗的學號、姓名;
select sno,sname from student
where sno in (
select sno from sc
where cno in(
select cno from course
where tno in(select tno from teacher
where tname<>'諶燕')));
六、查詢學過「c001」而且也學過編號「c002」課程的同窗的學號、姓名;
select sno from sc
where cno='c002'
and sno in (select sno from sc
where cno='c001');
七、查詢學過「諶燕」老師所教的全部課的同窗的學號、姓名;
select sno,sname from student
where sno in (
select sno from sc
where cno in(
select cno from course
where tno =(select tno from teacher
where tname='諶燕')));
八、查詢課程編號「c002」的成績比課程編號「c001」課程低的全部同窗的學號、姓名;
select sno, sname from student
where sno in (select sno from sc
WHERE score< any(
select score FROM sc
WHERE cno='c002')
and cno='c001');date

九、查詢全部課程成績小於60 分的同窗的學號、姓名;
select sno, sname from student
where sno in(
select sno from sc
where score <60);
十、查詢沒有學全全部課的同窗的學號、姓名;
select sno ,sname from student
where sno in (select sno from (select sno, count(*) count_course from sc
group by sno)
where count_course<(select count(*) from course));
十一、查詢至少有一門課與學號爲「s001」的同窗所學相同的同窗的學號和姓名;
select sno ,sname from student
where sno in (select sno FROM sc
where cno in (select cno from sc
where sno='s001')
and sno<>'s001');select

十二、查詢至少學過學號爲「s001」同窗全部一門課的其餘同窗學號和姓名;

1三、把「SC」表中「諶燕」老師教的課的成績都更改成此課程的平均成績;

 update sc set score=( select avg(score) from sc where cno in(
     select cno from course where tno=( select tno from teacher where tname='諶燕')))
  where cno in(select cno from course where tno=( select tno from teacher where tname='諶燕'));

1四、刪除學習「諶燕」老師課的SC 表記錄;
DELETE  from sc
where cno in (select cno from course
where tno=(select tno from teacher
where tname='諶燕'));
15.查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分
select cno ,max(score) max_score ,min(score) min_score from sc
group by cno;

16.查詢不一樣老師所教不一樣課程平均分從高到低顯示

17.查詢每門課程被選修的學生數
select cno, count(*) from sc
group by cno;
18.查詢出只選修了一門課程的所有學生的學號和姓名
select sno,sname  from student
where sno=(select sno from (select sno, count(*) sno_count from sc
group by sno)
where sno_count=1);
19.查詢男生、女生人數
select ssex, count(*) from student
GROUP by ssex;

20.查詢姓「張」的學生名單
select sno ,sname from student
where substr(sname, 1, 1)='張';

21.查詢同名同性學生名單,並統計同名人數
select sname, count(*) from (select  s1.sname,s1.ssex from student s1 join student s2
on s1.sno<> s2.sno
and s1.ssex= s2.ssex and s1.sname= s2.sname)
group by sname;

22.查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
select cno,avg(score) avg_score from sc group by cno order by avg_score,cno desc
23.查詢平均成績大於85 的全部學生的學號、姓名和平均成績
select s1.sname,s1.sno,avg_score from  student s1 join (select sno, avg(score) avg_score from sc
group by sno) s2 on s1.sno=s2.sno
where avg_score>85;
24.查詢課程名稱爲「數據庫」,且分數低於60 的學生姓名和分數
select sname, s1.sno, score from student s1
join (select sno ,score from sc
where cno=(select cno from course 
where cname='數據庫')) s2 on
s1.sno=s2.sno
;


25.查詢全部學生的選課狀況;
select c1.cname,c1.cno,sname,sno from course c1 join (SELECT cno,sname,s1.sno from sc s1 join (select sno,sname from student) s2
on s1.sno =s2.sno) s3 on c1.cno=s3.cno;

26.查詢任何一門課程成績在70 分以上的姓名、課程名稱和分數;
select c1.cname,sname ,s from course c1 join (select sname,cno,s from student s1 join (select sno,cno,score s from sc 
where score >70) s2
on s1.sno=s2.sno) s3
on c1.tno=s3.cno;

27.查詢課程編號爲c001 且課程成績在80 分以上的學生的學號和姓名;
select sno ,sname from student 
where sno in (select sno from sc
where cno='c001'
and score>80);

28.查詢選修「諶燕」老師所授課程的學生中,成績最高的學生姓名及其成績
select s1.sname ,score from student s1 join ( 
select sno,score from sc 
where score = any( select max_score from (select  cno,max(score) max_score  from sc
group by cno) 
where cno in (select cno from course
where tno =(select tno from teacher
where tname='諶燕')))
and cno in (select cno from course
where tno =(select tno from teacher
where tname='諶燕'))) s3 ON s1.sno=s3.sno;

29.查詢每門功課成績最好的前兩名


30.刪除「s002」同窗的「c001」課程的成績delete sc scorewhere sno='s002'andcno='coo1';

相關文章
相關標籤/搜索