SQL基本操作练习

--创建学生表
 create table student
   ( no    number(10) not null,  -- 学生学号
    name    varchar2(20),         -- 学生姓名
    age     varchar2(10));        -- 学生年龄
 
 -- 插入四条记录
 insert into student
   select 1001,'AE','12' from dual
   union
   select 1002,'BT','14' from dual
   union
   select 1003,'AS','20' from dual
   union
   select 1004,'SE','18' from dual;
   commit;
 --查询年纪为12的学生姓名
 select name 学生姓名 from student where age='12';
 
 --查询年纪在12至16岁之间的学生姓名
 select name 学生姓名 from student where age between 12 and 16;
 
 --查询年纪不在12至16岁之间的学生姓名
 select name 学生姓名 from student where age not between 12 and 16;
 
 --查询所有姓名以A开头的学生的姓名
 select name 姓名 from student where name like 'A%';
 
 --列出所有学生年纪的和,年纪的平均值,最大值,最小值,最大值与最小值之间的差值
 
 select sum(age) "Total" from student; --所有学生年纪的和
 
 select avg(age) "Average" from student; --所有学生年纪的平均值
 
 select max(age)  from student; -- 所有学生年纪的最大值
 
 select min(age) from student;--所有学生年纪的最小值
 
  select (max(age)-min(age)) from student
;--最大值与最小值之间的差值
 
 
--将所有学生按学号顺序升序排列
  select * from student order by no;
 
 
--将所有学生按学号顺序降序排列
  select * from student order by no desc;
 
 
  -- 作业二
 
  --创建COURSE表
  create table course
  ( no     varchar2(10),
    course_name   varchar2(20));
 
 --向COURSE表插入四条记录
  insert into  course(no,course_name)
   select 'A001','ORACLE' from dual
   union
   select 'A002','SQLSERVER' from dual
   union
   select 'A003','JavaEE'  from dual
   union
   select 'A004','.NET' from  dual;
   commit;

--创建课程选修表:STUDENT_COURSE
   create table student_course
    ( stu_no   number(10)  not null,
    course_no  varchar2(10)  not null);

--向STUDEN_COURSE中插入记录   
insert into student_course(stu_no,course_no)
   select 1001,'A001' from dual
   union
   select 1001,'A003' from dual
   union
   select 1004,'A002' from dual
   union
   select 1004,'A004' from dual
   union
   select 1004,'A001' from dual
   union
   select 1003,'A003' from dual;
   commit;

--列出每个学生姓名以及他的学习的所有课程
  
   select A.name,B.course_name from student A,
   course B,student_course C where (A.NO=C.STU_NO
   and B.NO=C.COURSE_NO);
  
--查询出没有选修任何课程的学生的学号和姓名

select no,name from student A
   where A.no not in
  (select stu_no from student_course);


--统计每个学生学习的课程数量并按课程数从大到小排序。(包括没有学习任何课程的学生记录)

    select name,count(course_no)
    from student A left outer join student_course C
    on(A.no=C.stu_no)
    group by name
    order by count(course_no) desc;


--查询所有学习JavaEE的学生的学号和姓名

    select no,name --最后在关系表STUDENT中取no,name
    from student
    where no in
    (select stu_no     
--然后在STUDENT_COURSE表中找出选修了'A003'课程的学生的学号
    from student_course
    where course_no in
    (select no          
-- 首先在course关系表中找到'JavaEE'的课程号为'A003'
    from course
    where course_name='JavaEE')
    );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值