SELECT * FROM EMPLOYEES e ;
SELECT EMPLOYEE_ID
, FIRST_NAME , EMAIL
, HIRE_DATE , JOB_ID
, LAST_NAME
FROM EMPLOYEES e
WHERE LAST_NAME = 'Smith'
AND FIRST_NAME = 'William'
;
/*
SELECT *
FROM EMPLOYEES e
WHERE DEPARTMENT_ID = 100
AND JOB_ID = 'FI_MGR'
;
/*
/*
SELECT EMPLOYEE_ID , FIRST_NAME
, LAST_NAME , JOB_ID
FROM EMPLOYEES e
WHERE DEPARTMENT_ID = 50
AND MANAGER_ID = 121
;
SELECT e.EMPLOYEE_ID AS "ID"
, SALARY
FROM EMPLOYEES e
;
SELECT DISTINCT JOB_ID
FROM EMPLOYEES e
;
SELECT EMPLOYEE_ID , LAST_NAME
, MANAGER_ID , DEPARTMENT_ID
FROM EMPLOYEES e
WHERE DEPARTMENT_ID = 50
OR MANAGER_ID = 100
;
SELECT FIRST_NAME , LAST_NAME
, DEPARTMENT_ID
FROM EMPLOYEES e
WHERE NOT (DEPARTMENT_ID = 50)
;
SELECT FIRST_NAME , LAST_NAME
, DEPARTMENT_ID
FROM EMPLOYEES e
WHERE DEPARTMENT_ID <> 50
;
SELECT FIRST_NAME , LAST_NAME
, DEPARTMENT_ID
FROM EMPLOYEES e
WHERE DEPARTMENT_ID != 50
;
SELECT FIRST_NAME , LAST_NAME
, SALARY
FROM EMPLOYEES e
WHERE SALARY >= 4000
AND SALARY <= 8000
;
SELECT FIRST_NAME , LAST_NAME
, SALARY
FROM EMPLOYEES e
WHERE SALARY BETWEEN 4000 AND 8000
;
SELECT *
FROM EMPLOYEES e
WHERE SALARY IN (6500,7700,13000)
;
/* 문1
SELECT EMPLOYEE_ID , FIRST_NAME
, LAST_NAME , JOB_ID
, SALARY
FROM EMPLOYEES e
WHERE JOB_ID = 'ST_MAN'
AND MANAGER_ID = 100
AND SALARY >= 5000
;
-- 문2.
-- EMPLOYEES 테이블에서 EMPLOYEE_ID, first_name, last_name, JOB_ID
-- MANAGER_ID, SALARY 를 조회
-- DEPARTMENT_ID가 10 또는 30 또는 100 또는 90에 속하고,
-- 급여(SALARY)가 5000에서 10000사이 이고
-- 매니저(MANAGER_ID)가 100이 아닌 사람을 조회
SELECT EMPLOYEE_ID , FIRST_NAME
, LAST_NAME , JOB_ID
, MANAGER_ID , SALARY
FROM EMPLOYEES e
WHERE DEPARTMENT_ID IN (10, 30, 100, 90)
AND SALARY BETWEEN 5000 AND 10000
AND MANAGER_ID != 100
;
SELECT FIRST_NAME , LAST_NAME , EMPLOYEE_ID
FROM EMPLOYEES e
WHERE FIRST_NAME LIKE '%d' --앞에 어떤문자가와도 상관없다 '%'
;
SELECT FIRST_NAME , LAST_NAME , EMPLOYEE_ID
FROM EMPLOYEES e
WHERE FIRST_NAME LIKE '__a%'
;
SELECT FIRST_NAME , LAST_NAME , EMPLOYEE_ID , COMMISSION_PCT
FROM EMPLOYEES e
WHERE COMMISSION_PCT IS NULL
;
SELECT FIRST_NAME , LAST_NAME , COMMISSION_PCT
FROM EMPLOYEES e
WHERE COMMISSION_PCT IS NOT NULL
;
SELECT FIRST_NAME , LAST_NAME , COMMISSION_PCT
FROM EMPLOYEES e
WHERE COMMISSION_PCT IS NOT NULL
ORDER BY FIRST_NAME ASC
;
SELECT FIRST_NAME , LAST_NAME , COMMISSION_PCT
FROM EMPLOYEES e
WHERE COMMISSION_PCT IS NOT NULL
ORDER BY FIRST_NAME DESC
;
SELECT SUM(SALARY)
FROM EMPLOYEES e
;
SELECT COUNT(*)
FROM EMPLOYEES e
;
SELECT COUNT(EMPLOYEE_ID) "cnt"
FROM EMPLOYEES e
;
SELECT count(DEPARTMENT_ID)
FROM EMPLOYEES e
;
SELECT count(COMMISSION_PCT)
FROM EMPLOYEES e
;
SELECT COUNT(DISTINCT DEPARTMENT_ID)
FROM EMPLOYEES e
;
SELECT AVG(SALARY)
FROM EMPLOYEES e
;
SELECT MAX(SALARY)
FROM EMPLOYEES e
;
SELECT MAX(HIRE_DATE)
FROM EMPLOYEES e
;
SELECT MIN(SALARY)
FROM EMPLOYEES e
;
SELECT MIN(HIRE_DATE)
FROM EMPLOYEES e
;
SELECT ABS(-23)
FROM dual
SELECT ROUND(0.123), ROUND(0.5678)
FROM dual
;
SELECT TRUNC(1234.56789)
FROM dual
;
SELECT TRUNC(1234.56789, 2) --소수점뒤로 두자리까지 나타나게
FROM dual
;
SELECT TRUNC(1234.56789, -1) --첫번째 자리 를 0으로 처리 값 잘라내기
FROM dual
;
단, 급여 오름차순으로 조회
SELECT LAST_NAME , SALARY
FROM EMPLOYEES e
WHERE SALARY >= 12000
ORDER BY SALARY ASC
;
-- 2. 사원번호가 176 인 사람의 LAST_NAME 과 부서 번호를 조회한다.
SELECT LAST_NAME , EMPLOYEE_ID
FROM EMPLOYEES e
WHERE EMPLOYEE_ID = 176
;
-- 3. 연봉이 5000 에서 12000의 범위 이외인 사람들의
-- LAST_NAME 및 연봉을 조회힌다.
SELECT LAST_NAME , SALARY
FROM EMPLOYEES e
WHERE NOT SALARY BETWEEN 5000 AND 12000
;
-- 4. 20 번 및 50 번 부서에서 근무하는 모든 사원들의
-- LAST_NAME 및 부서 번호를 알파벳순으로 조회한다.
SELECT LAST_NAME , DEPARTMENT_ID
FROM EMPLOYEES e
WHERE DEPARTMENT_ID IN (20, 50)
ORDER BY LAST_NAME ASC
;
-- 5. 20 번 및 50 번 부서에 근무하며,
-- 연봉이 5000 ~ 12,000 사이인 사원들의
-- LAST_NAME 및 연봉을 조회한다.
SELECT LAST_NAME , SALARY
FROM EMPLOYEES e
WHERE DEPARTMENT_ID IN (20,50)
AND SALARY BETWEEN 5000 AND 12000
;
-- 6. LAST_NAME 첫 글자가 A 인 사원들의 LAST_NAME 을 조회한다.
SELECT LAST_NAME
FROM EMPLOYEES e
WHERE LAST_NAME LIKE ('A%')
;
-- 7. 매니저가 없는 사람들의 LAST_NAME 및 JOB_ID 를 조회한다.
SELECT LAST_NAME , JOB_ID
FROM EMPLOYEES e
WHERE MANAGER_ID IS NULL
;
-- 8. 커미션을 버는 모든 사원들의 LAST_ANME,
-- 연봉 및 커미션을 조회한다.
-- 단,연봉 역순 정렬한다.
SELECT SALARY , COMMISSION_PCT , LAST_NAME
FROM EMPLOYEES e
WHERE COMMISSION_PCT IS NOT NULL
ORDER BY SALARY DESC
;
SELECT CONCAT('Hello', 'Bye')
, concat('Good', 'bad')
, 'good' || 'bad' --위와 똑같은 효과
FROM dual
;
SELECT INITCAP('good morning')
FROM dual
;
SELECT LOWER('GOOD')
, UPPER('good')
FROM dual
;
SELECT LPAD('good', 6) -- 아무것도입력안하면 왼쪽부터 공백으로 채운다.
, LPAD('good', 7, '#') -- 7자까지 왼쪽부터 남은칸은 샵으로채운다.
, LPAD('good', 8, 'L') -- 8자까지 왼쪽부터 남은칸은 L로 채운다.
FROM dual
;
SELECT RPAD('good', 6) -- 아무것도입력안하면 오른쪽부터 공백으로 채운다.
, RPAD('good', 7, '#') -- 7자까지 오른쪽부터 남은칸은 샵으로채운다.
, RPAD('good', 8, 'L') -- 8자까지 오른쪽부터 남은칸은 L로 채운다.
FROM dual
;
SELECT LTRIM('goodbye' , 'g') --왼쪽을 기점으로 정해진 글씨를 지움
, LTRIM('goodbye' , 'o') -- 겹치는게 있다면 다지우나 맨 왼쪾에 o가없어서 안지워짐
, LTRIM('goodbye' , 'go')
FROM dual
;
SELECT RTRIM('ggggoodbye' , 'g') --오른쪽을 기점으로 정해진 글씨를 지움
, RTRIM('goodbye' , 'e')
, RTRIM('goodbye' , 'ye') -- 겹치는게 있다면 다지우나 맨 오른쪽에 부터
FROM dual
;
SELECT SUBSTR('good morning john', 1, 4)
FROM dual
;
SELECT SUBSTR('good morning john', 8, 4)
FROM dual
;
SELECT SUBSTR('good morning john', 4)
FROM dual
;
SELECT SUBSTR('good morning john', -4) -- (-)뒤에서부터 4번째까지
FROM dual
;
SELECT REPLACE ('good morning tom', 'morning', 'evenning')
FROM dual
;
SELECT SYSDATE
FROM dual
;
SELECT ADD_MONTHS(SYSDATE, 7)
FROM dual
;
SELECT LAST_DAY(SYSDATE)
FROM dual
;
SELECT SYSDATE
, TO_CHAR(SYSDATE , 'yyyy/mm/dd') "yyyy/mm/dd"
, TO_CHAR(SYSDATE , 'yyyymmdd') "yyyymmdd"
, TO_CHAR(SYSDATE , 'yyyy-mm-dd') "yyyy-mm-dd"
, TO_CHAR(SYSDATE , 'yyyy-mm-dd HH24:MI:SS') "yyyy-mm-dd HH24:MI:SS"
FROM dual
;
SELECT TO_DATE('2023-02-09', 'yyyy-mm-dd')
FROM dual
;
SELECT FIRST_NAME , LAST_NAME
, NVL(COMMISSION_PCT, 0) COMMISSION
FROM EMPLOYEES e
;
SELECT * FROM DEPARTMENTS d ;
SELECT DEPARTMENT_ID
, DECODE(DEPARTMENT_ID, 20, 'MA', 60, 'IT', 90, 'EX', 'ETC') de -- de는 아래 출력 탭이름이길어서 줄임
FROM DEPARTMENTS d
;
SELECT FIRST_NAME , DEPARTMENT_ID
, CASE WHEN DEPARTMENT_ID = 20 THEN 'MA' --20은 ma
WHEN DEPARTMENT_ID = 60 THEN 'IT' --60은 it
WHEN DEPARTMENT_ID = 90 THEN 'EX' --90은 ex
ELSE '' --나머진 대충
END DEPARTMENt
FROM EMPLOYEES e
;
/
문제1) EMPLOYEES 테이블에서 King의 정보를 소문자로 검색하고
사원번호,성명, 담당업무(소문자로),부서번호를 출력하라.
/
SELECT EMPLOYEE_ID , LAST_NAME ,LOWER(JOB_ID) , DEPARTMENT_ID
FROM EMPLOYEES e
WHERE LOWER(LAST_NAME) = 'king'
;
/
문제2) EMPLOYEES 테이블에서 King의 정보를 대문자로 검색하고 사원번호,
성명, 담당업무(대문자로),부서번호를 출력하라.
/
SELECT EMPLOYEE_ID , FIRST_NAME , LAST_NAME , UPPER(JOB_ID) , DEPARTMENT_ID
FROM EMPLOYEES e
WHERE UPPER(LAST_NAME) = 'KING'
;
/
문제3) DEPARTMENTS 테이블에서 부서번호와 부서이름, 위치번호를
합하여 출력하도록 하라.(||사용)
/
SELECT DEPARTMENT_ID || DEPARTMENT_NAME || LOCATION_ID
FROM DEPARTMENTS d
;
/
문제4) EMPLOYEES 테이블에서 30번 부서 중 사원번호 이름과
담당 아이디를 연결하여 출력하여라. (concat 사용)
/
SELECT CONCAT(EMPLOYEE_ID , CONCAT(LAST_NAME, MANAGER_ID))
FROM EMPLOYEES e
WHERE DEPARTMENT_ID = 30
;
/*
SELECT EMPLOYEE_ID , FIRST_NAME , SALARY ,COMMISSION_PCT,
SALARY+SALARYnvl(COMMISSION_PCT ,0) AS sal
, CASE WHEN SALARY+SALARYnvl(COMMISSION_PCT ,0) >= 10000 THEN 'good'
WHEN SALARY+SALARYnvl(COMMISSION_PCT ,0) >= 5000 THEN 'average'
WHEN SALARY+SALARYnvl(COMMISSION_PCT ,0) BETWEEN 1 AND 5000 THEN 'bad'
WHEN SALARY+SALARY*nvl(COMMISSION_PCT ,0) = 0 THEN 'no good'
END AS grade
FROM EMPLOYEES e
ORDER BY sal
;