oracle学习记录

select EMPLOYEE_ID, LAST_NAME, SALARY, GRADE_LEVEL
from EMPLOYEES e,
     JOB_GRADES j
where e.SALARY between j.LOWEST_SAL and j.HIGHEST_SAL;

select last_name, department_name, city
from departments d,
     employees e,
     locations l
where d.department_id = e.department_id
  and d.location_id = l.location_id;


select *
from EMPLOYEES
where EMPLOYEE_ID = (select MANAGER_ID from EMPLOYEES where LAST_NAME = 'Chen');


select e.LAST_NAME, e.SALARY, j.GRADE_LEVEL
from EMPLOYEES e,
     JOB_GRADES j
where SALARY between j.LOWEST_SAL and j.HIGHEST_SAL;

-- 左外连接  右外连接
select e.LAST_NAME, d.DEPARTMENT_ID, d.DEPARTMENT_NAME
from EMPLOYEES e,
     DEPARTMENTS d
where e.DEPARTMENT_ID = d.DEPARTMENT_ID(+);

select e.LAST_NAME, d.DEPARTMENT_ID, d.DEPARTMENT_NAME
from EMPLOYEES e,
     DEPARTMENTS d
where e.DEPARTMENT_ID(+) = d.DEPARTMENT_ID;


-- 左外连接  右外连接
select LAST_NAME, DEPARTMENT_NAME
from EMPLOYEES e
         left outer join DEPARTMENTS d on e.DEPARTMENT_ID = d.DEPARTMENT_ID;

select LAST_NAME, DEPARTMENT_NAME
from EMPLOYEES e
         right join DEPARTMENTS d on e.DEPARTMENT_ID = d.DEPARTMENT_ID;

-- 全外连接
select LAST_NAME, d.DEPARTMENT_NAME, d.DEPARTMENT_ID
from EMPLOYEES e
         full join DEPARTMENTS d on e.EMPLOYEE_ID = d.MANAGER_ID


select e.LAST_NAME, d.DEPARTMENT_ID, d.DEPARTMENT_NAME
from EMPLOYEES e,
     DEPARTMENTS d
where e.DEPARTMENT_ID = d.DEPARTMENT_ID(+);

select e.JOB_ID, d.LOCATION_ID, d.DEPARTMENT_ID
from EMPLOYEES e
         left join DEPARTMENTS d on e.DEPARTMENT_ID = d.DEPARTMENT_ID
where d.DEPARTMENT_ID = '90';


select e.LAST_NAME, d.DEPARTMENT_NAME, d.LOCATION_ID, l.CITY
from EMPLOYEES e
         left join DEPARTMENTS d on e.DEPARTMENT_ID = d.DEPARTMENT_ID
         inner join LOCATIONS l on l.LOCATION_ID = d.LOCATION_ID
where COMMISSION_PCT is not null;


select e.LAST_NAME, e.JOB_ID, d.DEPARTMENT_ID, d.DEPARTMENT_NAME, l.CITY
from EMPLOYEES e
         inner join DEPARTMENTS d on e.DEPARTMENT_ID = d.DEPARTMENT_ID
         inner join LOCATIONS l on d.LOCATION_ID = l.LOCATION_ID
where l.CITY = 'Toronto';

-- 透过现象看本质 虽然写的是manger 但是本质还是名字 so……
select e1.LAST_NAME employees, e1.EMPLOYEE_ID, e2.LAST_NAME manager, e2.EMPLOYEE_ID Mgr#
from EMPLOYEES e1
         right join EMPLOYEES e2 on e1.MANAGER_ID = e2.EMPLOYEE_ID
where lower(e1.LAST_NAME) = 'kochhar'

select count(1)
from EMPLOYEES;

select count(DEPARTMENT_ID)
from EMPLOYEES;

select DEPARTMENT_ID, JOB_ID, round(avg(SALARY), 2)
from EMPLOYEES
group by DEPARTMENT_ID, JOB_ID;

select DEPARTMENT_ID, avg(SALARY) salary
from EMPLOYEES
group by DEPARTMENT_ID
having avg(SALARY) > 6000
order by SALARY desc;


select distinct count(*)
from EMPLOYEES;

select DEPARTMENT_ID, avg(SALARY)
from EMPLOYEES
group by DEPARTMENT_ID;

select avg(SALARY)
from EMPLOYEES e
         join DEPARTMENTS d on e.DEPARTMENT_ID = d.DEPARTMENT_ID
         join LOCATIONS l on d.LOCATION_ID = l.LOCATION_ID
where CITY = 'Toronto';

select CITY, avg(SALARY)
from EMPLOYEES e
         inner join DEPARTMENTS d on e.DEPARTMENT_ID = d.DEPARTMENT_ID
         inner join LOCATIONS l on d.LOCATION_ID = l.LOCATION_ID
group by CITY
having CITY is not null;

select d.DEPARTMENT_ID, avg(SALARY)
from EMPLOYEES e
         inner join DEPARTMENTS d on e.DEPARTMENT_ID = d.DEPARTMENT_ID
group by d.DEPARTMENT_ID
having avg(SALARY) > 8000;

select j.JOB_TITLE, avg(SALARY)
from EMPLOYEES e
         inner join JOBS j on e.JOB_ID = j.JOB_ID
group by JOB_TITLE
having avg(SALARY) > 6000;


select LAST_NAME, SALARY
from EMPLOYEES
where SALARY > (select SALARY from EMPLOYEES where LAST_NAME = 'Abel');


-- manger_id 也是employee_id  所以使用子查询就行。
select *
from EMPLOYEES
where EMPLOYEE_ID = (select MANAGER_ID from EMPLOYEES where LAST_NAME = 'Chen');

select LAST_NAME, JOB_ID, SALARY
from EMPLOYEES
where JOB_ID = (select JOB_ID from EMPLOYEES where EMPLOYEE_ID = 141)
  and SALARY > (select SALARY from EMPLOYEES where EMPLOYEE_ID = 143);


select LAST_NAME, JOB_ID, SALARY
from EMPLOYEES
where SALARY = (select min(SALARY) from EMPLOYEES);

select DEPARTMENT_ID, min(SALARY)
from EMPLOYEES
group by DEPARTMENT_ID
having min(SALARY) > (select min(SALARY) from EMPLOYEES where DEPARTMENT_ID = 50);

select LAST_NAME, JOB_ID, SALARY
from EMPLOYEES
where SALARY < (select min(SALARY) from EMPLOYEES where JOB_ID = 'IT_PROG')
  and JOB_ID != 'IT_PROG';


select LAST_NAME, SALARY
from EMPLOYEES
where SALARY > (select SALARY from EMPLOYEES where LAST_NAME = 'Abel');


--求平均工资最低的部门信息
select *
from DEPARTMENTS
where DEPARTMENT_ID = (select DEPARTMENT_ID
                       from EMPLOYEES
                       group by DEPARTMENT_ID
                       having avg(SALARY) = (select min(avg(SALARY)) from EMPLOYEES group by EMPLOYEES.DEPARTMENT_ID))

--平均工资最高的job信息

select *
from JOBS
where JOB_ID = (select JOB_ID
                from EMPLOYEES
                group by JOB_ID
                having max(SALARY) = (select max(avg(SALARY)) from EMPLOYEES group by EMPLOYEES.JOB_ID));

SELECT job_id
FROM employees
GROUP BY job_id
HAVING avg(salary) = (
    SELECT max(avg(salary))
    FROM employees
    GROUP BY job_id
);

select DEPARTMENT_ID
from EMPLOYEES
group by DEPARTMENT_ID
having avg(SALARY) > (select avg(SALARY) from EMPLOYEES);

SELECT department_id
FROM employees
GROUP BY department_id
HAVING avg(salary) > (
    SELECT avg(salary)
    FROM employees
);

--创建视图

create view empview
as
select EMPLOYEE_ID, LAST_NAME, SALARY
from EMPLOYEES
where DEPARTMENT_ID = 80;

select *
from empview;

update empview
set SALARY=20000
where EMPLOYEE_ID = 179;

update empview
set SALARY=20000
where EMPLOYEE_ID = 177

select EMPLOYEE_ID, SALARY
from EMPLOYEES
where EMPLOYEE_ID = 177

commit;

create view empview2
as
select EMPLOYEE_ID id, LAST_NAME name, SALARY, DEPARTMENT_NAME
from EMPLOYEES e,
     DEPARTMENTS d
where d.DEPARTMENT_ID = e.DEPARTMENT_ID;


create view empview3
as
select DEPARTMENT_NAME, avg(SALARY) avg_sal
from EMPLOYEES e,
     DEPARTMENTS d
where e.DEPARTMENT_ID = d.DEPARTMENT_ID
group by DEPARTMENT_NAME;

select ROWNUM, EMPLOYEE_ID, LAST_NAME, SALARY
from (select EMPLOYEE_ID, LAST_NAME, SALARY from EMPLOYEES order by SALARY desc)
where ROWNUM <= 10;

select rn, EMPLOYEE_ID, LAST_NAME, SALARY
from (
         select ROWNUM rn, EMPLOYEE_ID, LAST_NAME, SALARY
         from (select EMPLOYEE_ID, LAST_NAME, SALARY from EMPLOYEES order by SALARY desc))
where rn >= 40
  and rn <= 50


--创建序列
create sequence empseq
    increment by 10 --每次增长10
    start with 10 --从10开始增长
    maxvalue 100 -- 最大值100
    cycle --需要循环
    nocache -- 不需要缓存登录


select EMPLOYEE_ID, LAST_NAME, JOB_ID
from EMPLOYEES e1
where 2 <= (select count(*) from JOB_HISTORY where e1.EMPLOYEE_ID = JOB_HISTORY.EMPLOYEE_ID);

select LAST_NAME, DEPARTMENT_ID, SALARY
from EMPLOYEES
where (SALARY, DEPARTMENT_ID) in
      (select SALARY, DEPARTMENT_ID from EMPLOYEES where EMPLOYEES.COMMISSION_PCT is not null);

call SP_emp();
declare
    --声明变量
    V_sal      number(20, 2);
    V_email    varchar2(20);
    V_hiredate date ;
begin
    --SQL语句的操作  select ... into ....from ... where ...
    select SALARY, EMAIL, HIRE_DATE into V_sal, V_email,V_hiredate from EMPLOYEES where EMPLOYEE_ID = 100;
    --打印
    DBMS_OUTPUT.PUT_line(V_sal || ',' || V_email || ',' || V_hiredate);
end;

declare
    --声明一个记录类型
    type emp_record is record
                       (
                           V_sal       EMPLOYEES.salary%type,
                           V_email     employees.email%type,
                           V_hire_date EMPLOYEES.hire_date%type
                       );
    V_emp_record emp_record;
begin
    --SQL语句的操作  select ... into ....from ... where ...
    select SALARY, EMAIL, HIRE_DATE into V_emp_record from EMPLOYEES where EMPLOYEE_ID = 100;
    --打印
    DBMS_OUTPUT.PUT_line(V_emp_record.V_sal || ',' || V_emp_record.V_email || ',' || V_emp_record.V_hire_date);
end;

declare
    v_sal    number(8, 2) := 0;
    v_emp_id number(10);
begin
    select SALARY, EMPLOYEE_ID into v_sal,v_emp_id from EMPLOYEES where EMPLOYEE_ID = 123;
    DBMS_OUTPUT.PUT_LINE('empployid: ' || v_emp_id || ' salary:' || v_sal);
end;

declare
    v_emp_record EMPLOYEES%rowtype;
    v_emp_id     number(10);
begin
    v_emp_id := 132;
    select * into v_emp_record from EMPLOYEES where EMPLOYEE_ID = v_emp_id;

    DBMS_OUTPUT.PUT_LINE(v_emp_record.SALARY || v_emp_record.EMAIL);

end;

declare
    v_sal employees.salary%type;
begin
    select SALARY into v_sal from EMPLOYEES where EMPLOYEE_ID = 150;
    if v_sal >= 10000 then
        DBMS_OUTPUT.PUT_LINE('salary>10000');
    elsif v_sal >= 5000 then
        DBMS_OUTPUT.PUT_LINE('salary>5000');
    else
        DBMS_OUTPUT.PUT_LINE('salary<5000');
    end if;
end;

declare
    v_i number(3) :=0;
    begin
    loop
        DBMS_OUTPUT.PUT_LINE(v_i);
        v_i:=v_i+1;
        exit when v_i>100;

    end loop;
end;

declare
    vi_i number(3) :=0;
    begin
    while vi_i<=100 loop
        DBMS_OUTPUT.PUT_LINE(vi_i);
        vi_i:=vi_i+1;
        end loop;
end;
posted @ 2021-05-11 16:58  Cherish°  阅读(37)  评论(0编辑  收藏  举报