数据库-select查询语句

来源:互联网 发布:c语言 const 编辑:程序博客网 时间:2024/05/22 13:30
1.表结构如下:
学生表:Student 学生表 (学号,姓名,性别,年龄,组织部门)
              Course 课程表 (编号,课程名称)
              Sc 选课表 (学号,课程编号,成绩)
(1).写一个SQL语句,查询选修了’计算机原理’的学生学号和姓名
select  学号,姓名 from  Student  where  学号  in
   (select 学号 from Sc where 课程编号 in
          (select  编号 from Course where 课程名称='计算机原理'
  )
   );
(2).写一个SQL语句,查询’周星驰’同学选修了的课程名字
select 课程名称 from Course where 编号 in
  (select 课程编号 from Sc where 学号=
    (select 学号 from Student where 姓名='周星驰'
     )
   );
(3).写一个SQL语句,查询选修了5门课程的学生学号和姓名
select 学号,姓名 from Student s join
     (select 学号,count(*) from Sc group by 学号 having count(*) =5)  t
     on (s.学号=t.学号);

2.

有一个职工表employee(eno,ename,esex,deptno,sal),

其中eno代表职工号,数值型(整数),长度为8,eno为student表的主键;ename代表职工姓名,字符型,长度为10;esex代表性别,取值仅为“男”或者“女”;deptno代表部门号,数值型(整数),非空,长度为6;sal是工资

1) :创建表

create table emp(

enonumber(8),

ename     varchar2(10),

esex        varchar2(10),

deptno    number(6),

sal          number(20),

constraint c_esex check(esex in ('男','女')),

primary key(eno)

)

2):查询姓张的员工

select ename from emp where ename like '张%';

3):查询每个部门员工的人数

select count(*) from emp group by deptno;

4):工资不等于1000的员工的人数

select count(*)  from  emp where sal<>1000;
5):编写存储过程:当sal>1000是工资涨200;当sal>2000是工资涨1000;其他的涨150;

create or replace procedure p is

cursor c is select * from emp for update;

begin

for v_emp in c loop

if(v_emp.sal>1000 and v_emp.sal<=2000) then

update emp set sal=sal+200 where current of c;

elsif (v_emp.sal>2000) then

update emp set sal=sal+1000  where current of c;

else  update emp set sal=sal+150 where current of c;

end if;

       end loop;

 commit;

end;



0 0
原创粉丝点击