• oracle-sql入门练习及答案


    1,通过命令行方式打开sqlplus
    2,执行登录命令
    sqlplus scott/scott@192.168.248.129/orcl
    3进行sqlplus命令测试

    Set time on
    Set pagesize 数字
    Set linesizes 数字
    Set pause on/off

    Describe命令  查看表或视图结构 也可以desc

    Show 查看相应参数 如:show parameter,show user,show error等

    Get命令 把一个sql脚本文件内容放入缓冲区

    用系统编辑程序编辑命令
    在SQL*PLUS中运行操作系统缺省的文本编辑程序(EDIT)
    命令为:edit或者ed

    将缓存中的sql语句保存到文件中:SAVE 文件名【具体路径】
    查看缓存sql:list

    执行文件中的sql语句:
    START my.sql
    @ my.sql

    清空缓冲区:clear buffer
    格式化命令:column
    column sal format $999,999.00
    如:
    SQL> select ename, sal from emp
      2
    SQL> /


    ENAME               SAL
    ---------- ------------
    SMITH           $800.00
    ALLEN         $1,600.00
    WARD          $1,250.00
    JONES         $2,975.00
    MARTIN        $1,250.00
    BLAKE         $2,850.00
    CLARK         $2,450.00

    重置为默认值:clear columns;
    将屏幕上的内容写入到文件中
    spool fileName
    结束写入
    spool off

    3.Oracle查询
    1) 查询EMP表,显示部门号为10的所有雇员的NAME 、JOB、SALARY和 DEPTNO,并以岗位降序、工资升序进行排序。
    select ename,job,sal,deptno from emp where deptno=10 order by job desc , sal asc

    2) 从EMP表中查询所有雇员的NAME和补助(SALARY+COMM),并处理null行。
    select ename,(sal+nvl(comm,0)) from emp

    3) 统计30号部门的总人数、人均工资、最高工资和最低工资。
    select count(*) totle,avg(sal),max(sal),min(sal) from emp where deptno=30

    4) 查询各个部门各个工种的平均工资、每个部门的平均工资和所有员工的平均工资。
    select deptno,job,avg(sal) from emp group by rollup(deptno,job);

    5) 查询各个部门中各个工种的平均工资、每个部门的平均工资、每个工种的平均工资和所有员工的平均工资。
    select deptno,job,avg(sal) from emp group by cube(deptno,job);

    6) 查询EMP,显示1999年之后参加工作人员的NAME、JOB和HIREDATE,并以工作日期的升序进行排序。
    select * from emp where hiredate>'1-JAN-99';

    7) 查询名字以“S”开始的所有职工:
    select *from emp where ename like 'S%';

    8) 让员工姓名右对齐显示
    col ename jus left

    9) 显示姓名只有5个字母组成的员工信息
    select *from emp where ename like '_____'
    或者
    select *from emp where length(ename)=5

    10) 查询在当月倒数第三天入职的员工信息
    select *from emp where hiredate = last_day(hiredate)-2



    2) 对各表中的数据进行不同条件的查询;
    a)查询全体学生的学号和姓名
    select sno,sname from student;

    b)查询全体学生的详细记录
    select *from student;

    c)查询所有选修过课程的学生学号
    select distinct sno from sc;
    或者
    select sno from student where sno in (select sno from sc);

    d)查询考试有不及格的学生学号
    select sno from sc where nvl(grade,0) <60;

    e)查询不是信息系(IS)、计算机系(CS)的学生性别、年龄、系别
    select ssex,sage,sdept from student where sdept not in('is','cs');

    f)查询选修了4号课的学生学号和成绩,结果按成绩降序排列
    select sc1.sno,sc1.grade from sc sc1,sc sc2 where sc2.cno=4 and sc1.sno=sc2.sno;

    g)查询每个课程号和相应的选课人数
    select cno,count(sno) from sc group by cno;

    h)查询计算机系(CS)的学生姓名、年龄、系别
    select sname,sage,sdept from student where sdept='cs'

    i)查询年龄18-20岁的学生学号、姓名、系别、年龄;
    select sno,sname,sage,sdept from student where sage between 18 and 20

    j)查询姓刘的学生情况
    select *from student where sname like '刘%'

    k)查询既选修1号课程,又选修2号课程的学生学号和成绩
    select sno,grade from sc where cno = 1 and cno in(select sno from sc where cno=2);

    l)查询学生的姓名和出生年份(今年2003年)
    select sname,2017-sage born from student;

    m)查询没有成绩的学生学号和课程号
    select s.sno,c.cno from student s,course c where (s.sno,c.cno) not in(select sno,cno from sc);

    n)查询总成绩大于200分的学生学号
    select sno from sc group by sno having sum(grade)>200;

    o)查询每门课程不及格学生人数
    select cno ,count(sno) from sc where nvl(grade,0)<60 group by cno【不及格分组】

    p)查询不及格课程超过3门的学生学号
    select sno from (select sno,cno from sc where nvl(grade,0)<60) r group by sno having count(r.cno)>3

    q)查询年龄在10到19岁之间的学生信息
    select *from student where sage>=10 and sage<=19;
    select *from student where sage between 10 and 19;【between and 包含两头】

    r)查询全体学生情况,按所在系升序排列,同一个系的学生按年龄降序排列
    select *from student order by sdept asc,sage desc;

    s)查询选了1号课程的学生平均成绩
    select cno, avg(grade) avgGrade from sc where cno=1 group by cno;

    t)查询选了3号课程的学生的最高分
    select max(grade) from sc where cno=3;

    u)查询每个同学的总成绩
    select sno, count(grade) from sc group by sno;

    复杂查询
    内容和主要步骤:
    1)实验一中的数据为基础

    2) 对各表中的数据进行不同条件的连接查询和嵌套查询;

    (1)?查询每个学生及其选课情况;
    select * from sc;

    (2)?查询每门课的间接先修课
    select a.cno,b.cpno from course a,course b where a.cpno=b.cno;

    (3)?将STUDENT,SC进行右连接
    select *from student right join sc on student.sno=sc.sno;

    (4)?查询有不及格的学生姓名和所在系
    select sname,sdept from student where sno in (select distinct sno from sc where nvl(grade,0)<60);

    (5)?查询所有成绩为优秀(大于90分)的学生姓名
    select sname from student where sno in (select distinct sno from sc where nvl(grade,0)>90);

    (6)?查询既选修了2号课程又选修了3号课程的学生姓名、学号;
     select sname,sno from student where sno  in (select sno from sc where cno = 3 and cno in(select sno from sc where cno=2));

    (7)?查询和刘晨同一年龄的学生
    select *from student where sname<>'刘晨' and sage in (select sage from student where sname='刘晨');

    (8)?选修了课程名为“数据库”的学生姓名和年龄
    select sname,sage from student  where sno in(select sno from sc where cno in(select cno from course where cname='数据库'))

    (9)?查询其他系比IS系任一学生年龄小的学生名单
    select sname from student where sdept<>'is' and sage < (select max(sage) from student where sdept='is')

    (10)?查询其他系中比IS系所有学生年龄都小的学生名单
    select sname from student where sdept<>'is' and sage < (select min(sage) from student where sdept='is')

    (11)?查询选修了全部课程的学生姓名
     select sname from student where sno in(select sno from sc group by sno having count(cno) = (select count(cno)from sc))

    (12)?查询计算机系学生及其性别是男的学生
    select *from student where sdept='cs' and ssex='男'

    (13)?查询选修课程1的学生集合和选修2号课程学生集合的差集
    select *from sc where cno='1' and sno not in(select sno from sc where cno='2')

    (14)?查询李丽同学不学的课程的课程号
    select cno from course where cno not in(select cno from sc where sno in (select sno from student where sname='李丽'))

    (15)?查询选修了3号课程的学生平均年龄
    select avg(sage) from student where sno in(select sno from sc where cno=3)

    (16)?求每门课程学生的平均成绩
    select cno ,avg(grade) from sc group by cno;

    (17)?统计每门课程的学生选修人数(超过3人的才统计)。
    要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列
    select cno ,count(sno) from sc group by cno having count(sno)>3 order by count(sno) desc,cno asc;

    (18)?查询学号比刘晨大,而年龄比他小的学生姓名。
    select a.sname from student a,(select sno,sname,sage from student where sname='刘晨') b where a.sno>all b.sno and a.sage<all b.sage;

    (19)?求年龄大于女同学平均年龄的男同学姓名和年龄
    select sname,sage from student where sage  > (select avg(sage) from student where ssex='女');

    (20)?求年龄大于所有女同学年龄的男同学姓名和年龄
    select sname,sage from student where sage  >all (select sage from student where ssex='女');

    (21)?查询至少选修了95002选修的全部课程的学生号码
    思想:首先查找学号为95002的同学的课程号集合,然后查找sc中所有学号在该集合中的选课信息,【此时该选课信息均为95002选过的课程信息】记为 msg
    然后从msg中以学号进行分组,然后选出cno个数和95002选课个数相同的学号,该学号就是至少选修了95002选修的全部课程的学生学号
    select sno from sc where sno<>95002
    and
    cno in (select cno from sc where sno=95002)
    group by sno
    having
    count(cno)=(select count(cno) from sc where sno=95002)

    (22)?查询95001和95002两个学生都选修的课程的信息
    select cno from sc where sno =95001 and cno in (select cno from sc where sno=95002);


    更新查询题目:
    1)    应用INSERT,UPDATE,DELETE语句进行更新操作;
    a)    插入如下学生记录(学号:95030,姓名:李莉,年龄:18)
    insert into student (sno,sname,sage) values (95030,'李莉',18)

    b)    插入如下选课记录(95030,1)
    insert into sc (sno,cno) values(95030,1)

    c)    计算机系学生年龄改成20
    update student set sage=20  where sdept='cs';

    d)    数学系所有学生成绩改成0
    update sc set grade = 0
    where sno in (select sno from student where sdept='ma')

    e)    把低于总平均成绩的女同学成绩提高5分
    update sc set grade = grade+5 where sno in (select sno from student where ssex='女' and grade < (select avg(grade) from sc))

    f)    修改2号课程的成绩,若成绩小于75分提高5%,成绩大于75时提高%1
    update sc set  grade = grade+grade*0.05 where grade<75
    update sc set grade=grade+grade*0.01 where grade>75

    4)(两个语句实现,注意顺序)
    g)    删除95030学生信息
    delete student where sno=95030

    h)    删除SC表中无成绩的记录
    delete sc where grade is null;

    i)    删除张娜的选课记录
    delete sc where sno in (select sno from student where sname='张娜')

    j)    删除数学系所有学生选课记录
    delete sc where sno in (select sno from student where sdept='ma')

    k)    删除不及格的学生选课记录
    delete sc where grade <60

    l)    查询每一门课程成绩都大于等于80分的学生学号、姓名和性别,把值送往另一个已经存在的基本表STU(SNO,SNAME,SSEX)中
    create table stu as (select sno,sname,ssex from student where sno in(select sno from sc where sno not in(select distinct sno from sc where grade<80)))

    m)    把所有学生学号和课程号连接追加到新表中
    create table newTable as select * from student,sc;

    n)    所有学生年龄增1
    update student set sage=sage+1;

    o)    统计3门以上课程不及格的学生把相应的学生姓名、系别追加到另外一个表中
    create table failT as select sname,sdept from student where sno in(select sno from sc where grade<60 group by sno having count(cno)>=3)



  • 相关阅读:
    VC CComboBox用法总结
    WideCharToMultiByte和MultiByteToWideChar函数的用法
    JavaScript
    标题:外星日历
    C语言顺序栈
    C语言循环队列
    每日一题
    标题:李白打酒
    标题:分数
    标题:复数幂
  • 原文地址:https://www.cnblogs.com/sunshinekevin/p/6813532.html
Copyright © 2020-2023  润新知