-- ex) 급여 평균값 이상의 급여를 받는 사원을 검색하시오
SELECT AVG(SALARY) FROM EMPLOYEE;
SELECT * FROM EMPLOYEE
WHERE SALARY >= (SELECT AVG(SALARY) FROM EMPLOYEE);
-- ex) 전지연 직원의 관리자 이름을 출력하여라.
SELECT MANAGER_ID FROM EMPLOYEE
WHERE EMP_NAME = '전지연';
SELECT EMP_NAME FROM EMPLOYEE
WHERE EMP_ID = (SELECT MANAGER_ID FROM EMPLOYEE WHERE EMP_NAME = '전지연');
--@실습문제
--1. 윤은해와 급여가 같은 사원들을 검색해서, 사원번호,사원이름, 급여를 출력하라.
select emp_name, emp_id, salary from employee
where salary = (select salary from employee where emp_name = '윤은해') and emp_name != '윤은해';
--2. employee 테이블에서 기본급여가 제일 많은 사람과 제일 적은 사람의 정보를
-- 사번, 사원명, 기본급여를 나타내세요.
select emp_id, emp_name, salary from employee
where salary = (select max(salary) from employee) or salary =(select min(salary) from employee);
--3. D1, D2부서에 근무하는 사원들 중에서
--기본급여가 D5 부서 직원들의 '평균월급' 보다 많은 사람들만
--부서번호, 사원번호, 사원명, 월급을 나타내세요.
select avg(salary) from employee
where dept_code = 'D5';
select dept_code, emp_id, emp_name, salary
from employee
where salary > (select avg(salary) from employee where dept_code = 'D5')
and dept_code in ('D1','D2');
--dept_code = 'D1' or dept_code = 'D2'
where 문에 () 안에 selcet 문을 다시 넣어서 맞는 조건문을 넣는것
select dept_code from employee
where emp_name in ('송종기', '박나라');
select * from employee
where dept_code in (select dept_code from employee where emp_name in ('송종기','박나라'));
--
select * from employee
where dept_code not in (select dept_code from employee where emp_name in ('송종기' , '박나라'));
서브쿼리 위치는where 뿐만이 아니라 from, select 뒤에도올수있다.
상관쿼리는 select뒤에 온다 상관쿼리의 결과 값이 1개인것을 스칼라 쿼리라고 한다
from 뒤에 오는것은 인라인 뷰
select * from employee
where 1 = 1;
select * from employee
where exists (select 1 from employee where dept_code = 'D5')
select emp_name, dept_code
from employee e
where exists (select 1 from department where dept_id = e.dept_code);
--> where dept_code is null
서브 쿼리를 사용했지만 dept_code is null과 같은 조건문이라고 할수있다.
--3. 직급이 J1, J2, J3이 아닌 사원중에서 자신의 부서별 평균급여보다 많은 급여를 받는 사원출력.
-- 부서코드, 사원명, 급여, 부서별 급여평균
select e.dept_code, emp_name, salary
from employee e join (select dept_code, round(avg(salary))"평균" from employee
group by dept_code)a
on e.dept_code = a.dept_code
where job_code not in ('J1','J2','J3') and salary > "평균";
select dept_code, emp_name, salary, (select round(avg(salary)) from employee where dept_code = e.dept_code) "평균"
from employee e
where job_code not in('J1','J2','J3') and salary > (select round(avg(salary)) from employee where dept_code = e.dept_code);
select e.emp_id, e.emp_name, e.manager_id,
nvl((select emp_name from employee m where e.manager_id = m.emp_id),'없음') "관리자명"
from employee e;
select * from
(select emp_name, emp_no, decode(substr(emp_no,8,1),'1','남',3,'남','여') "성별"
from employee)
where "성별" = '남';
-- rownum을 이용하여 랭킹 5 구하기
select rownum, emp_name, salary
from (select emp_name, salary from employee order by salary )
where rownum<6;
create table emp_01(
emp_id number,
emp_name varchar2(30),
dept_title varchar2(20)
);
select * from emp_01;
insert into emp_01(
select emp_id, emp_name, dept_title from employee
left join DEPARTMENT on dept_code = dept_id);
desc employee;
--------출력결과-------
이름 널? 유형
---------- -------- ------------
EMP_ID NOT NULL VARCHAR2(3)
EMP_NAME NOT NULL VARCHAR2(20)
EMP_NO NOT NULL CHAR(14)
EMAIL VARCHAR2(25)
PHONE VARCHAR2(12)
DEPT_CODE CHAR(2)
JOB_CODE NOT NULL CHAR(2)
SAL_LEVEL NOT NULL CHAR(2)
SALARY NUMBER
BONUS NUMBER
MANAGER_ID VARCHAR2(3)
HIRE_DATE DATE
ENT_DATE DATE
ENT_YN CHAR(1)
create table employee_copy
as select emp_id,emp_name,dept_code,hire_date
from employee
where 1=0; --거짓 이 값을 참으로쓰면 내용까지 복사된다
create table user_notnull(
user_no number not null,
user_id varchar2(20) not null,
user_pwd varchar2(30) not null,
user_name varchar2(30),
gender varchar2(10),
phone varchar(30),
email varchar(50));
insert into user_notnull
VALUES(NULL, 'khuser01', 'pass01', '일용자', '남', '01082728333', 'khuser01@iei.or.kr');
CREATE TABLE USER_UNIQUE(
USER_NO NUMBER,
USER_ID VARCHAR2(20) UNIQUE,
USER_PWD VARCHAR2(30) NOT NULL,
USER_NAME VARCHAR2(30),
GENDER VARCHAR2(10),
PHONE VARCHAR2(30),
EMAIL VARCHAR2(50)
);
insert into user_unique values('220','khuser02','pass02','이용자','여','01062923939','khuser02@iei.or.kr');
CREATE TABLE USER_UNIQUE( USER_NO NUMBER, USER_ID VARCHAR2(20), -- 컬럼 레벨
USER_PWD VARCHAR2(30) NOT NULL,
USER_NAME VARCHAR2(30),
GENDER VARCHAR2(10),
PHONE VARCHAR2(30),
EMAIL VARCHAR2(50),
UNIQUE(USER_ID) -- 테이블 레벨);
이 경우 테이블 레벨에서 제약조건을 걸었다고 할수있다
CREATE TABLE USER_CHECK(
USER_NO NUMBER PRIMARY KEY,
USER_ID VARCHAR2(20) UNIQUE,
USER_PWD VARCHAR2(30) NOT NULL,
USER_NAME VARCHAR2(30),
GENDER VARCHAR2(10) CHECK (GENDER IN ('남', '여')),
PHONE VARCHAR2(30),
EMAIL VARCHAR2(50),
GRADE_CODE NUMBER
);
젠더에서 ‘남’,’여’외엔 입력할수 없도록 체크되어있다.