Oracle 常見筆試題查詢練習

創建四個表:學生表、成績表,課程表、教師表。

學生表

create table student(
s_id varchar(20) primary key,
s_name varchar(20) not null,
s_birth varchar(20) not null,
s_sex varchar(10) not null
);

成績表:

create table Course(
c_id varchar(20) primary key,
c_name varchar(20) not null,
t_id varchar(20) not null
);

課程表:

create table Score(
s_id varchar(20),
c_id varchar(20),
s_score number(3),
CONSTRAINT PK_Score PRIMARY KEY (s_id,c_id)
);

教師表:

create table Teacher(
t_id varchar(20) primary key,
t_name varchar(20) not null
);

其中在課程表中用到了聯合主鍵。

–在學生表中插入數據:

insert into Student values('01' , '趙雷' , '1990-01-01' , '男');
insert into Student values('02' , '錢電' , '1990-12-21' , '男');
insert into Student values('03' , '孫風' , '1990-05-20' , '男');
insert into Student values('04' , '李雲' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吳蘭' , '1992-03-01' , '女');
insert into Student values('07' , '鄭竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');

–課程表測試數據

insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數學' , '01');
insert into Course values('03' , '英語' , '03');

–教師表測試數據

insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

–成績表測試數據

insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[60-0] :

select c.c_id  , c.c_name , (
   case when sc.s_score >= 85 then '85-100'
        when sc.s_score >= 70 and sc.s_score < 85 then '70-85'
        when sc.s_score >= 60 and sc.s_score < 70 then '60-70'
        else '0-60'
   end) as px, 
   count(1) 
 from Course c , SCORE sc
 where c.c_id = sc.c_id 
 group by c.c_id , c.c_name , (
   case when sc.s_score >= 85 then '85-100'
        when sc.s_score >= 70 and sc.s_score < 85 then '70-85'
        when sc.s_score >= 60 and sc.s_score < 70 then '60-70'
        else '0-60'
   end)
 order by c.c_id , c.c_name , px;

統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[60-0] 及所佔百分比:

select t1.*,round(t1.num/t2.all_num*100,2) || '%' 
from 
(select m.c_id , m.c_name , (
  case when n.s_score >= 85 then '85-100'
  when n.s_score >= 70 and n.s_score < 85 then '70-85'
  when n.s_score >= 60 and n.s_score < 70 then '60-70'
  else '0-60'
  end) as px,  
  count(1) num
from Course m , score n
where m.c_id = n.c_id  
group by m.c_id , m.c_name , (
  case when n.s_score >= 85 then '85-100'
  when n.s_score >= 70 and n.s_score < 85 then '70-85'
  when n.s_score >= 60 and n.s_score < 70 then '60-70'
  else '0-60'
  end)
order by m.c_id , m.c_name , px) t1,

(select m.c_id , m.c_name ,  
  count(1) all_num
from Course m , score n
where m.c_id = n.c_id  
group by m.c_id , m.c_name
order by m.c_id , m.c_name) t2
where t1.c_id=t2.c_id
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章