读书人

oracle复杂查询练习

发布时间: 2012-11-15 15:16:14 作者: rapoo

oracle复杂查询练习题

1.删除重复记录(当表中无主键时)

Sql代码
  1. create table TESTTB(
  2. bm varchar(4),
  3. mc varchar2(20)
  4. )
  5. insert into TESTTB values(1,'aaaa');
  6. insert into TESTTB values(1,'aaaa');
  7. insert into TESTTB values(2,'bbbb');
  8. insert into TESTTB values(2,'bbbb');
  9. /*方案一*/
  10. delete from TESTTB where rowid not in
  11. (select max(rowid) from TESTTB group by TESTTB.BM,TESTTB.MC)
  12. /*方案二*/
  13. delete from TESTTB a where a.rowid!= (
  14. select max(rowid) from TESTTB b where a.bm=b.bm and a.mc=b.mc
  15. )

2.bookEnrol是用来登记的,不管你是借还是还,都要添加一条记录。

请写一个SQL语句,获取到现在状态为已借出的所有图书的相关信息,

ID为3的java书,由于以归还,所以不要查出来。要求查询结果应为:(被借出的书和被借出的日期)

Sql代码
  1. create table book(
  2. id int ,
  3. name varchar2(30),
  4. PRIMARY KEY (id)
  5. )
  6. insert into book values(1,'English');
  7. insert into book values(2,'Math');
  8. insert into book values(3,'JAVA');
  9. create table bookEnrol(
  10. id int,
  11. bookId int,
  12. dependDate date,
  13. state int,
  14. FOREIGN KEY (bookId) REFERENCES book(id) ON DELETE CASCADE
  15. )
  16. insert into bookEnrol values(1,1,to_date('2009-01-02','yyyy-mm-dd'),1);
  17. insert into bookEnrol values(2,1,to_date('2009-01-12','yyyy-mm-dd'),2);
  18. insert into bookEnrol values(3,2,to_date('2009-01-14','yyyy-mm-dd'),1);
  19. insert into bookEnrol values(4,1,to_date('2009-01-17','yyyy-mm-dd'),1);
  20. insert into bookEnrol values(5,2,to_date('2009-02-14','yyyy-mm-dd'),2);
  21. insert into bookEnrol values(6,2,to_date('2009-02-15','yyyy-mm-dd'),1);
  22. insert into bookEnrol values(7,3,to_date('2009-02-18','yyyy-mm-dd'),1);
  23. insert into bookEnrol values(8,3,to_date('2009-02-19','yyyy-mm-dd'),2);
  24. /*方案一*/
  25. select a.id,a.name,b.dependdate from book a,bookenrol b where
  26. a.id=b.bookid
  27. and
  28. b.dependdate in(select max(dependdate) from bookenrol group by bookid )
  29. and b.state=1
  30. /*方案二*/
  31. select k.id,k.name,a.dependdate
  32. from bookenrol a, BOOK k
  33. where a.id in (select max(b.id) from bookenrol b group by b.bookid)
  34. and a.state = 1
  35. and a.bookid = k.id;

3.查询每年销量最多的产品的相关信息

Sql代码
  1. create table t2 (
  2. year_ varchar2(4),
  3. product varchar2(4),
  4. sale number
  5. )
  6. insert into t2 values('2005','a',700);
  7. insert into t2 values('2005','b',550);
  8. insert into t2 values('2005','c',600);
  9. insert into t2 values('2006','a',340);
  10. insert into t2 values('2006','b',500);
  11. insert into t2 values('2007','a',220);
  12. insert into t2 values('2007','b',350);
  13. insert into t2 values('2007','c',350);
  14. /**方案一*/
  15. select a.year_,a.sale,a.product from t2 a inner join(
  16. select max(sale) as sl from t2 group by year_) b
  17. on a.sale=b.sl order by a.year_
  18. /*方案二*/
  19. select sa.year_, sa.product, sa.sale
  20. from t2 sa,
  21. (select t.year_ pye, max(t.sale) maxcout
  22. from t2 t
  23. group by t.year_) tmp
  24. where sa.year_ = tmp.pye
  25. and sa.sale = tmp.maxcout

4.排序问题,如果用总积分做降序排序..因为总积分是字符型,所以排出来是这样子(9,8,7,6,5...),要求按照总积分的数字大小排序。

Sql代码
  1. create table t4(
  2. 姓名 varchar2(20),
  3. 月积分 varchar2(20),
  4. 总积分 char(3)
  5. )
  6. insert into t4 values('WhatIsJava','1','99');
  7. insert into t4 values('水王','76','981');
  8. insert into t4 values('新浪网','65','96');
  9. insert into t4 values('牛人','22','9');
  10. insert into t4 values('中国队','64','89');
  11. insert into t4 values('信息','66','66');
  12. insert into t4 values('太阳','53','66');
  13. insert into t4 values('中成药','11','33');
  14. insert into t4 values('西洋参','257','26');
  15. insert into t4 values('大拿','33','23');
  16. /*方案一*/
  17. select * from t4 order by cast(总积分 as int) desc
  18. /*方案二*/
  19. select * from t4 order by to_number(总积分) desc;

5.得出所有人(不区分人员)每个月及上月和下月的总收入

Sql代码
  1. create table t5 ( tmonth int,
  2. tname varchar2(10),
  3. income number
  4. )
  5. insert into t5 values('08','a',1000);
  6. insert into t5 values('09','a',2000);
  7. insert into t5 values('10','a',3000);
  8. /*方案一*/
  9. select o.tmonth,sum(o.income) as cur,(select sum(t.income) from t5 t where t.tmonth=(o.tmonth+1) group by t.tmonth) as next,
  10. (select sum(t.income) from t5 t where t.tmonth=(o.tmonth-1) group by t.tmonth) as last
  11. from t5 o where o.tmonth=2 group by o.tmonth
  12. /*方案二*/
  13. select tmonth as 月份 ,tname as 姓名,sum(income) as 当月工资,
  14. (select sum(income)
  15. from t5
  16. where tmonth = to_number(substr(to_char(sysdate,'yyyy-mm-dd'),7,1))-1) AS 上月工资 ,
  17. (select sum(income)
  18. from t5
  19. where tmonth = to_number(substr(to_char(sysdate,'yyyy-mm-dd'),7,1))+1) AS 下月工资
  20. from t5 where tmonth=substr(to_char(sysdate,'yyyy-mm-dd'),7,1)
  21. group by tmonth,tname

6.根据现有的学生表,课程表,选课关系表,查询一。没有修过李明老师的课的学生,查询二,既学过a课程,又学过b课程的学生姓名

Sql代码
  1. S表 [SNO,SNAME]--学生表
  2. C表 [CNO,CNAME,CTEATHER] --课程表
  3. SC表 [SNO,CNO,SCGRADE] --选课关系表
  4. 查询一:没有修过李明老师的课的学生的姓名
  5. select sname from s where not exists
  6. (select*from sc,c where sc.cno=c.cno and c.cteather='李明' and sc.sno=s.sno)
  7. 查询二:既学过a课程,又学过b课程的学生姓名
  8. SELECT S.SNO,S.SNAME
  9. FROM S,(
  10. SELECT SC.SNO
  11. FROM SC,C
  12. WHERE SC.CNO=C.CNO
  13. AND C.CNAME IN('a','b')
  14. GROUP BY SNO
  15. )SC WHERE S.SNO=SC.SNO
  16. 查询三: 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
  17. SELECT S.SNO,S.SNAME,AVG(SC.SCGRADE)
  18. FROM S,SC,(
  19. SELECT SNO
  20. FROM SC
  21. WHERE SCGRADE <60
  22. GROUP BY SNO
  23. HAVING COUNT(DISTINCT CNO)>=2
  24. )A WHERE S.SNO=A.SNO AND SC.SNO=A.SNO
  25. GROUP BY S.SNO,S.SNAME

读书人网 >其他数据库

热点推荐