1. 程式人生 > >sql語句總結

sql語句總結

初始化 課程表 let core 顯示 ble student 練習 教師

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)
);
/*******初始化學生表的數據******/
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;
/******************初始化教師表***********************/
insert into teacher values (‘t001‘, ‘劉陽‘);
insert into teacher values (‘t002‘, ‘諶燕‘);
insert into teacher values (‘t003‘, ‘胡明星‘);
commit;
/***************初始化課程表****************************/
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;
/***************初始化成績表***********************/
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;


練習:
註意:以下練習中的數據是根據初始化到數據庫中的數據來寫的SQL 語句,請大家務必註意。

select * from student;
select * from teacher;
select * from course;
select * from sc;
1、查詢“c001”課程比“c002”課程成績高的所有學生的學號;
select a.ssno1 from
(select s.sno ssno1,s.score sscore1 from sc s where s.cno=‘c001‘) a left join
(select s.sno ssno2,s.score sscore2 from sc s where s.cno=‘c002‘) b on a.ssno1=b.ssno2
where a.sscore1 > b.sscore2

2、查詢平均成績大於60 分的同學的學號和平均成績;

select s.sno as 學號,avg(s.score) 平均成績 from sc s group by s.sno having avg(s.score)>60

3、查詢所有同學的學號、姓名、選課數、總成績;
--方法1
select s.sno,s.sname,count(c.cno),sum(c.score) from student s inner join sc c on s.sno = c.sno group by s.sno,s.sname
--方法2
select s.sno,s.sname,a.countcno,a.sumcscore from student s inner join
(select c.sno csno,count(c.cno) countcno,sum(c.score) sumcscore from sc c group by c.sno) a
on s.sno = a.csno
4、查詢姓“劉”的老師的個數;
select count(te.tname) from teacher te where te.tname like ‘劉%‘;
5、查詢沒學過“諶燕”老師課的同學的學號、姓名;
select stu.sno,stu.sname from student stu where not stu.sname in (
select distinct st.sname from student st left join
(select * from sc s left join
(select * from teacher te inner join course co on te.tno = co.tno) a on s.cno =a.cno) b
on st.sno = b.sno where b.tname =‘諶燕‘
)
6、查詢學過“c001”並且也學過編號“c002”課程的同學的學號、姓名;

select st.sno,st.sname from student st join sc ac on st.sno=ac.sno join sc bc on bc.sno=ac.sno
where ac.cno=‘c001‘ and bc.cno=‘c002‘


select st.sno,st.sname from student st inner join
(select a.sno from
(select * from sc s where s.cno=‘c001‘) a inner join
(select * from sc s where s.cno=‘c002‘)
b on a.sno = b.sno) c on st.sno= c.sno
7、查詢學過“諶燕”老師所教的所有課的同學的學號、姓名;
select distinct st.sno,st.sname from student st join sc ac on st.sno = ac.sno join course co on ac.cno=co.cno join teacher
te on co.tno=te.tno where te.tname=‘諶燕‘

select distinct st.sno,st.sname from student st left join
(select * from sc s left join
(select * from teacher te inner join course co on te.tno = co.tno) a on s.cno =a.cno) b
on st.sno = b.sno where b.tname =‘諶燕‘
8、查詢課程編號“c002”的成績比課程編號“c001”課程低的所有同學的學號、姓名;


select st.sno,st.sname from student st where st.sno in
(select a.sno from
(select * from sc s1 where s1.cno = ‘c002‘) a,
(select * from sc s2 where s2.cno = ‘c001‘) b where a.sno = b.sno and a.score < b.score)

--9、查詢所有課程成績小於60 分的同學的學號、姓名;
select distinct st.sno,st.sname from student st left join sc ac on st.sno=ac.sno
where ac.score >= 60


select sno,sname from student where sno not in (select sno from sc where score>=60)

--10、查詢沒有學全所有課的同學的學號、姓名;
select st.sno,st.sname from student st left join sc ac on st.sno = ac.sno
group by st.sno,st.sname having count(ac.cno) < (select count(distinct co.cno) from course co)

select * from student;
select * from teacher;
select * from course;
select * from sc;

11、查詢至少有一門課與學號為“s001”的同學所學相同的同學的學號和姓名;
--1
select distinct st.sno,st.sname from student st join sc bc on st.sno=bc.sno where not bc.sno=‘s001‘ and bc.cno in (
select ac.cno from sc ac where ac.sno=‘s001‘)
--2
select su.sno,su.sname from student su where su.sno in (
select sc.sno from sc where sc.cno in (select s.cno from sc s where s.sno=‘s001‘)
and sc.sno<>‘s001‘)
12、查詢至少學過學號為“s001”同學所有一門課的其他同學學號和姓名;

select * from sc
left join student st on st.sno=sc.sno where not sc.sno=‘s001‘
and sc.cno in (select cno from sc where sno=‘s001‘)


13、把“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=‘諶燕‘));

14、查詢和“s001”號的同學學習的課程完全相同的其他同學學號和姓名;
select bc.cno from sc bc where bc.sno <> ‘s001‘ minus
(select sc.cno from sc where sno=‘s001‘)

15、刪除學習“諶燕”老師課的SC 表記錄;
delete from sc
where cno in (select cno from course
where tno in (select tno from teacher
where tname=‘諶燕‘))

16、向SC 表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“c002”課程的同學學號,
“c002”號課的平均成績;
insert into sc (sno,cno,score)
select distinct st.sno,sc.cno,
(select avg(score)from sc where cno=‘c002‘)
from student st,sc
where not exists
(select * from sc where cno=‘c002‘ and sc.sno=st.sno) and sc.cno=‘c002‘;


17、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分
select cno ,max(score),min(score) from sc group by cno;
select ac.cno,max(ac.score),min(ac.score) from sc ac group by ac.cno
18、按各科平均成績從低到高和及格率的百分數從高到低順序
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
19、查詢不同老師所教不同課程平均分從高到低顯示

select co.tno,ac.cno,avg(ac.score) from sc ac join course co on ac.cno=co.cno
group by co.tno,ac.cno order by avg(ac.score) desc

select max(t.tno),max(t.tname),max(c.cno),max(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

20、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]

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 ,c.cname;

21、查詢各科成績前三名的記錄:(不考慮成績並列情況)
select * from
(select s.sno,s.cno,s.score,row_number()over(partition by cno order by s.score desc)
rn from sc s) where rn<4

select * from
(select sno,cno,score,row_number()over(partition by cno order by score desc) rn from sc)
where rn<4

22、查詢每門課程被選修的學生數
select s.cno,count(*) from sc s group by s.cno
23、查詢出只選修了一門課程的全部學生的學號和姓名
select bc.sno from sc bc join
(select s.cno scno,count(*) a from sc s group by s.cno) scc on bc.cno=scc.scno where a=1
24、查詢男生、女生人數
select * from student st
select st.ssex,count(1) from student st group by st.ssex having st.ssex=‘男‘ or st.ssex=‘女‘
25、查詢姓“張”的學生名單
select * from student st where st.sname like ‘張%‘
26、查詢同名同性學生名單,並統計同名人數
select st.sname,st.ssex,count(1) from student st group by st.sname,st.ssex having count(1)>1
27、1981 年出生的學生名單(註:Student 表中Sage 列的類型是number)

select * from student st where to_char(sysdate,‘yyyy‘)-st.sage=1994
28、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列

select c.cno,avg(c.score) from sc c group by c.cno order by avg(c.score),c.cno
29、查詢平均成績大於85 的所有學生的學號、姓名和平均成績
select * from student st join
(select c.sno c,avg(c.score) from sc c group by c.sno having avg(c.score)>85) aa on st.sno=aa.c
30、查詢課程名稱為“數據庫”,且分數低於60 的學生姓名和分數
select * from student;
select * from teacher;
select * from course;
select * from sc;
select * from student st join sc ac on st.sno=ac.sno join course co on ac.cno=co.cno
where co.cname=‘Oracle‘ and ac.score<60
31、查詢所有學生的選課情況;
select ac.sno,count(1) from student st join sc ac on st.sno=ac.sno group by ac.sno
32、查詢任何一門課程成績在70 分以上的姓名、課程名稱和分數;
select st.sname,co.cname,bc.score from student st join sc bc on st.sno=bc.sno join course co on bc.cno=co.cno
where bc.sno in
(select ac.sno from sc ac
where ac.score>70 group by ac.sno) and bc.score>70
33、查詢不及格的課程,並按課程號從大到小排列
select ac.cno from sc ac where ac.score<60 group by ac.cno order by ac.cno desc
34、查詢課程編號為c001 且課程成績在80 分以上的學生的學號和姓名;
select * from sc ac where ac.cno=‘c001‘ and ac.score>80
35、求選了課程的學生人數
select count(1) from sc
36、查詢選修“諶燕”老師所授課程的學生中,成績最高的學生姓名及其成績
select st.sname,c.score from student st join sc c on st.sno=c.sno where c.score in (
select max(c.score) from student st join sc c on st.sno=c.sno join course co
on c.cno=co.cno join teacher te on co.tno=te.tno
where te.tname=‘諶燕‘ )

37、查詢各個課程及相應的選修人數
select co.cno,count(co.cno) from sc c left join course co on c.cno=co.cno group by co.cno
38、查詢不同課程成績相同的學生的學號、課程號、學生成績
select * from sc ac group by ac.cno
39、查詢每門功課成績最好的前兩名
select * from
(select ac.sno,ac.score,ac.cno,row_number()over(partition by ac.cno order by ac.score desc)
rn from sc ac) where rn<=2

40、統計每門課程的學生選修人數(超過10 人的課程才統計)。要求輸出課程號和選修人數,
查詢結果按人數降序排列,若人數相同,按課程號升序排列
select ac.cno,count(ac.sno) from sc ac group by ac.cno having count(ac.sno)>2
order by count(ac.sno) desc,ac.cno

41、檢索至少選修兩門課程的學生學號
select * from sc
select ac.sno,count(ac.cno) from sc ac group by ac.sno having count(ac.cno)>=2
42、查詢全部學生都選修的課程的課程號和課程名
select st.sno,co.cno,co.cname from student st
inner join sc ac on st.sno=ac.sno left join course co on ac.cno=co.cno

43、查詢沒學過“諶燕”老師講授的任一門課程的學生姓名

select st.sname from student st join sc ac on st.sno=ac.sno join course co on ac.cno=co.cno join teacher te
on co.tno=te.tno where not te.tname=‘諶燕‘
44、查詢兩門以上不及格課程的同學的學號及其平均成績
select bc.sno,avg(bc.score) from sc bc where bc.sno in
(select ac.sno from sc ac where ac.score<60
group by ac.sno having count(ac.score)>2) group by bc.sno

45、檢索“c004”課程分數小於60,按分數降序排列的同學學號
select ac.sno from sc ac where ac.cno=‘c004‘ and ac.score<60 order by ac.score desc
46、刪除“s002”同學的“c001”課程的成績
delete sc ac where ac.sno=‘s002‘ and ac.cno=‘c001‘

sql語句總結