暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SQL查询题

叶子闲聊 2021-06-24
287


1)oracle分页查询语句


使用rownum,两种如下:


第一种:
select * from (
select a.*, rownum rid from scott.emp a where rownum<= 10
 ) where rid> 5 ;




第二种:
select * from (
select a.*,row_number() over(order by 1) rid from scott.emp a
)
where  rid between 6 and 10;


第三种:
SELECT * FROM scott.emp where rownum<=10 
minus
SELECT * FROM scott.emp where rownum<6;


2)从数据库中随机取10条

select * from (
select * from scott.emp order by dbms_random.random
) where rownum <= 10

3)怎样把这样一个表(表名:t_finance )

year month amount 
2012 1 1.1
2012 2 1.2
2012 3 1.3
2012 4 1.4
2012 4 1.6
2013 1 2.1
2013 2 2.2
2013 2 2.2
2013 3 2.3
2013 4 2.4   


查成这样一个结果:
year m1 m2 m3 m4
2012 1.1 1.2 1.3 3
2013 2.1 4.4 2.3 2.4


select year,
(select amount from t_finance f1 where f1.year=f.year and month=1) m1,
(select amount from t_finance f2 where f2.year=f.year and month=2) m2,
(select amount from t_finance f3 where f3.year=f.year and month=3) m3,
(select amount from t_finance f4 where f4.year=f.year and month=4) m4
from t_finance f group by year;


4)emp中存在deptno重复三次以上的记录,完整的查询语句如下

select * from scott.emp a
where a.deptno in (
select deptno from scott.emp group by deptno having count(deptno)>3
);

5)删除除了编号不同,其他都相同的学生冗余信息

学生表 如下:

编号   学号         姓名      课程编号      课程名称    分数

1        2005001  张三         0001          数学         69

2        2005002  李四         0001          数学         89

3        2005001  张三         0001          数学         69

delete from 学生表 a
where  a.编号 > (select min(编号) from 学生表 b where a.学号=b.学号);


6)用一条SQL语句 查询出每门课都参加考试且都大于80分的学生姓名  

name   kecheng   fenshu 

         张三     语文       81

         张三     数学       75

         李四     语文       76

         李四     数学       90

         王五     语文       81

         王五     数学       100

         王五     英语       90

select sname from t_marks 
group by sname
having min(score)>=80 and count(*)=3


7)学生信息查询

学生表   S (SNO,SNAME)        

             SNO 为学号,SNAME 为姓名

课程表 C (CNO,CNAME,CTEACHER)  

            CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师

选课表  SC(SNO,CNO,SCGRADE)       SCGRADE 为成绩


1.找出没有选修过“李明”老师讲授课程的所有学生姓名

select s.sname as '学生姓名' from s
where not exists (
select * from sc,c where s.sno=sc.sno and c.cno=sc.cno
and c.cteacher='李明')


2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩 

sselect s.sname as '学生姓名',avg(sc.scgrade) as '平均成绩' 
from s,sc,c
where s.sno = sc.sno and c.cno = sc.cno and s.sno in
(
select sno from sc
where sc.scgrade<60
group by sno
having COUNT(DISTINCT CNO)>=2
)
group by s.sname

3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名

select s.sname '学生姓名'  
from s,(
select sc.sno from sc,c where sc.cno = c.cno and c.cno in(1,2)
group by sc.sno
having count(distinct c.cno)=2
) sc
where sc.sno = s.sno;

4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号

select distinct s.sno '学生学号',s.sname
from s,sc sc1,sc sc2
where sc1.cno=1 and sc2.cno=2  and sc1.sno = s.sno  
and sc2.scgrade>sc1.scgrade;

5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩

select sc1.sno '学号','1号课成绩'=sc1.scgrade,[2号课成绩]=sc2.scgrade
from sc sc1,sc sc2
where sc1.cno=1 and sc2.cno=2 and sc2.scgrade>sc1.scgrade;


8)管理岗位业务培训信息

S (S#,SN,SD,SA)   

      S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄

C (C#,CN )        

      C#,CN       分别代表课程编号、课程名称

SC ( S#,C#,G )    

      S#,C#,G     分别代表学号、所选修的课程编号、学习成绩

1.使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名

select SN,SD from Student where S# in( 
select S# from Course C , SC where C.C#=SC.C# and CN= ’税收基础’);

2. 查询选修课程超过5门的学员学号和所属单位

select SN,SD from Student where S# in ( 
select S# from SC group by S# having count(distinct C#) > 5);

3.查询选修全部课程的学员姓名和所属单位

SELECT SN,SD FROM S
WHERE S# IN(
    SELECT S# FROM SC  RIGHT JOIN C ON SC.C#=C.C#
    GROUP BY S#
    HAVING COUNT(*)=COUNT(DISTINCT S#))

9)其他查询

user表:id ,name,regdate

temp表:(id,name)

1.统计A表中每个月注册用户数 

select count(*),to_char(regdate,'yyyy-mm') 
from user group by to_char(regdate,'yyyy-mm');

2.统计A表中有姓名相同的用户数

select count(*) from (
select name from user group by name having count(*) >1
);


3.如果表A中有姓名相同的用户,把相同的查出,写入表C中

insert into C(id,name
select min(id),name from user group by name having count(*) >1;


文章转载自叶子闲聊,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论