방대한 데이터의 세계로, JUST DO DBMS!
SELECT ABS(-23)
FROM dual
;
/*
출력겨로가)
23
*/
SELECT ROUND(0.123), ROUND(0.543)
FROM dual
;
/*
출력결과)
0
1
*/
SELECT TRUNC(1234.37273)
FROM dual
;
SELECT TRUNC(1234.37273, 2) -- 소숫점 둘째자리까지
FROM dual
;
/*
출력결과)
1234
1234.37
*/
SELECT CONCAT('Hello', 'bye') ,
CONCAT('good', 'bad')
FROM dual
;
SELECT CONCAT('Hello', 'bye') concats ,
'good' || 'bad'
FROM dual
;
/*
출력결과)
Hellobye
goodbad
Hellobye
goodbad
*/
-- 첫 글자를 대문자로
SELECT INITCAP('good morning')
FROM dual
;
/*
출력결과)
Good morning
*/
-- 대/소문자 처리lower(char), upper(char)
SELECT LOWER('GOOD'), UPPER('good')
FROM dual
;
/*
출력결과)
good
GOOD
*/
SELECT LPAD('good', 6) LPAD1,
LPAD('good', 7, '#') LPAD2,
LPAD('good', 8, 'L') LPAD3,
RPAD('good', 7, '#') RPAD1,
RPAD('good', 8, 'L') RPAD2
FROM dual
;
/*
출력결과)
good //채움문자를 따로 입력하지 않으면 공백이 들어가요.
good###
goodLLLL
###good
LLLLgood
*/
-- SUBSTR('문자열', '시작위치', '길이)
SELECT SUBSTR('good morning joy', 1, 4) substr1,
SUBSTR('good morning joy', 8, 2) substr2,
SUBSTR('good morning joy', 8) substr3,
-- 맨 마지막 파라미터를 주지 않으면 끝까지 출력
SUBSTR('good morning joy', -2) substr4
-- -는 뒤에서부터
FROM dual
;
/*
출력결과)
good
rn
rning joy
oy
*/
SELECT REPLACE ('good morning joy',
'morning', 'evnning')
FROM dual
;
/*
출력결과)
good evnning joy
*/
-- 작성하는 현재 시간을 출력
SELECT SYSDATE
FROM dual
;
SELECT ADD_MONTHS(SYSDATE, 7) -- 현재 날짜로부터 7개월을 더함
FROM dual
;
-- 현재달의 마지막 날짜
SELECT LAST_DAY(SYSDATE)
FROM dual
;
-- 년, 달, 일, 시간, 분, 초 뒤에
SELECT SYSDATE + (INTERVAL '1' YEAR) yearLater,
SYSDATE + (INTERVAL '1' MONTH) monthLater,
SYSDATE + (INTERVAL '1' DAY) dayLater,
SYSDATE + (INTERVAL '1' HOUR) hourLater,
SYSDATE + (INTERVAL '1' minute) minuteLater,
SYSDATE + (INTERVAL '1' second) secondLater
FROM dual
;
SELECT TO_CHAR(SYSDATE, 'yyyy-mm-dd') ,
TO_CHAR(SYSDATE, 'yyyy/mm/dd'),
SYSDATE
FROM EMPLOYEES e
;
SELECT TO_CHAR(SYSDATE, 'yyyymmdd'),
TO_CHAR(SYSDATE, 'yyyy/mm/dd'),
TO_CHAR(SYSDATE, 'yyyy-mm-dd'),
TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mi:ss')
FROM dual
;
-- 날짜형으로 변환
SELECT TO_DATE('2015/03/04', 'yyyy/mm/dd')
FROM dual
;
-- null 값을 다른 데이터로 변경하는 함수
SELECT FIRST_NAME , LAST_NAME ,
NVL(COMMISSION_PCT, 0)
FROM EMPLOYEES e
;
SELECT *
FROM DEPARTMENTS d
;
SELECT *
FROM EMPLOYEES e
;
SELECT DEPARTMENT_ID ,
DECODE(DEPARTMENT_ID, 20, 'MA',
60, 'IT', 90, 'EX', 'ETC')
FROM EMPLOYEES e
ORDER BY DEPARTMENT_ID
;
SELECT FIRST_NAME , DEPARTMENT_ID ,
CASE WHEN DEPARTMENT_ID = 20 THEN 'MA'
WHEN DEPARTMENT_ID = 60 THEN 'IT'
WHEN DEPARTMENT_ID = 90 THEN 'EX'
ELSE ''
END department
FROM EMPLOYEES e
ORDER BY DEPARTMENT_ID
;
-- 집계함수와 짝을 이루어 사용할 수 있어요.
SELECT DEPARTMENT_ID
FROM EMPLOYEES e
GROUP BY DEPARTMENT_ID
;
-- 부서별 사원수, 평균 급여, 급여의 합을 구해오기
SELECT DEPARTMENT_ID,
COUNT(DEPARTMENT_ID) ,
SUM(SALARY),
AVG(SALARY)
FROM EMPLOYEES e
GROUP BY DEPARTMENT_ID
;
-- 부서별 직급별 사원수와 평균 급여를 구하기
SELECT DEPARTMENT_ID,
JOB_ID,
COUNT(EMPLOYEE_ID),
AVG(SALARY)
FROM EMPLOYEES e
GROUP BY DEPARTMENT_ID , JOB_ID
ORDER BY DEPARTMENT_ID , JOB_ID
;
/*
* WHERE절에서는 집계 함수를 사용할 수 없다.
* HAVING절 : 집계함수를 가지고 조건 비교를 할 때, 사용한다.
* HAVING 절은 GROUP BY 절과 함께 사용된다.
*/
SELECT DEPARTMENT_ID , COUNT(*)
FROM EMPLOYEES e
WHERE DEPARTMENT_ID IS NOT NULL
-- AND COUNT(*) >= 10
GROUP BY DEPARTMENT_ID
-- 그룹핑 된 후 조건을 줄 때 HAVING 을 사용한다.
HAVING COUNT(*) >= 10
;
SELECT LAST_NAME,
SALARY
FROM EMPLOYEES e
WHERE SALARY >= 12000
;
SELECT LAST_NAME ,
DEPARTMENT_ID
FROM EMPLOYEES e
WHERE EMPLOYEE_ID = 176
;
SELECT LAST_NAME ,
SALARY
FROM EMPLOYEES e
WHERE NOT SALARY BETWEEN 5000 AND 12000
;
SELECT LAST_NAME,
DEPARTMENT_ID
FROM EMPLOYEES e
WHERE DEPARTMENT_ID = 20
OR DEPARTMENT_ID = 50
-- WHERE DEPARTMENT_ID IN(20, 50)
ORDER BY LAST_NAME ASC
;
20번 및 50번 부서에 근무하며, 연봉이 5000~12000 사이인 사원들의 last_name 및 연봉 조회
SELECT LAST_NAME ,
SALARY,
DEPARTMENT_ID
FROM EMPLOYEES e
WHERE SALARY BETWEEN 5000 AND 12000
AND (DEPARTMENT_ID = 20 OR DEPARTMENT_ID = 50)
;
SELECT LAST_NAME
FROM EMPLOYEES e
WHERE LAST_NAME LIKE 'A%'
;
SELECT LAST_NAME ,
JOB_ID
FROM EMPLOYEES e
WHERE MANAGER_ID IS NULL
;
SELECT LAST_NAME ,
SALARY ,
COMMISSION_PCT
FROM EMPLOYEES e
WHERE COMMISSION_PCT IS NOT NULL
-- WHERE COMMISSION_PCT = 0
-- 회사에 따라 커미션이 0 OR NULL 일 수 있다.
ORDER BY SALARY DESC
;
SELECT EMPLOYEE_ID ,
LAST_NAME ,
LOWER(JOB_ID) ,
DEPARTMENT_ID
FROM EMPLOYEES e
WHERE LOWER(LAST_NAME) = 'king'
;
SELECT EMPLOYEE_ID ,
LAST_NAME ,
UPPER(JOB_ID) ,
DEPARTMENT_ID
FROM EMPLOYEES e
WHERE UPPER(LAST_NAME) = 'KING'
;
SELECT CONCAT(EMPLOYEE_ID, CONCAT(FIRST_NAME, MANAGER_ID))
FROM EMPLOYEES e
WHERE DEPARTMENT_ID = 30
;
SELECT AVG(SALARY),
MAX(SALARY),
MIN(SALARY),
SUM(SALARY)
FROM EMPLOYEES e
WHERE JOB_ID LIKE 'SA%'
;
SELECT COUNT(*),
COUNT(COMMISSION_PCT),
-- NULL은 카운팅이 되지 않는다.
AVG(SALARY),
COUNT (DISTINCT DEPARTMENT_ID)
FROM EMPLOYEES e
;
SELECT COUNT(EMPLOYEE_ID),
TRUNC(AVG(SALARY)) ,
MIN(SALARY),
MAX(SALARY),
SUM(SALARY)
FROM EMPLOYEES e
GROUP BY DEPARTMENT_ID
;
SELECT DEPARTMENT_ID ,
JOB_ID ,
COUNT(EMPLOYEE_ID),
TRUNC(AVG(SALARY)),
SUM(SALARY)
FROM EMPLOYEES e
GROUP BY DEPARTMENT_ID , JOB_ID
ORDER BY DEPARTMENT_ID
;
SELECT DEPARTMENT_ID ,
COUNT(*),
SUM(SALARY)
FROM EMPLOYEES e
HAVING COUNT(*) > 4
GROUP BY DEPARTMENT_ID
;
SELECT DEPARTMENT_ID ,
TRUNC(AVG(SALARY)) ,
SUM(SALARY) ,
MAX(SALARY)
FROM EMPLOYEES e
HAVING MAX(SALARY) >= 10000
GROUP BY DEPARTMENT_ID
;
SELECT JOB_ID ,
AVG(SALARY) ,
SUM(SALARY)
FROM EMPLOYEES e
HAVING AVG(SALARY) >= 10000
GROUP BY JOB_ID
;
SELECT JOB_ID ,
SUM(SALARY)
FROM EMPLOYEES e
WHERE NOT JOB_ID LIKE 'SA_%'
HAVING SUM(SALARY) > 10000
GROUP BY JOB_ID
ORDER BY SUM(SALARY) DESC
;
SELECT MAX(SALARY),
MIN(SALARY),
SUM(SALARY),
AVG(SALARY)
FROM EMPLOYEES e
GROUP BY JOB_ID
ORDER BY JOB_ID DESC
;
SELECT EMPLOYEE_ID ,
FIRST_NAME ,
SALARY ,
NVL(COMMISSION_PCT, 0) COMMISSION_PCT,
SALARY + (SALARY * NVL(COMMISSION_PCT, 0)) monthlyPay,
CASE WHEN SALARY + (SALARY * NVL(COMMISSION_PCT, 0)) >= 10000 THEN 'good'
WHEN SALARY + (SALARY * NVL(COMMISSION_PCT, 0)) >= 5000 THEN 'average'
WHEN 1 < SALARY + (SALARY * NVL(COMMISSION_PCT, 0)) AND SALARY + (SALARY * NVL(COMMISSION_PCT, 0)) < 5000 THEN 'bad'
ELSE 'no good'
END MonthlyComment
FROM EMPLOYEES e
ORDER BY MonthlyComment
;
출처
https://media.giphy.com/media/1hVi7JFFzplHW/giphy.gif
https://media.giphy.com/media/jUwpNzg9IcyrK/giphy.gif