아래 링크에서 실습에 사용할 데이터를 다운로드 받으세요.
http://naver.me/GUDeCYu9
employee 테이블
http://naver.me/5T3pUJK4
department 테이블
SELECT
empno
, empname
, title
FROM
employee
;
SELECT
deptno
, deptname
FROM
department
;
SELECT
empno AS "사원번호"
, empname AS "사원 이름"
, title
FROM
employee "사용자"
;
(Access에서
SELECT
*
FROM
employee
;
SELECT
DISTINCT title
FROM
employee
;
SELECT
empno
, empname
, title
, salary
FROM
employee
WHERE
salary >= 3000000
AND empname LIKE '*아'
;
SELECT
empno
, empname
, title
, salary
FROM
employee
WHERE
empno BETWEEN 1000 AND 3000
;
SELECT
title
, salary
, dno
FROM
employee
WHERE
dno >= 2
;
-- 방법 1 : 논리 연산자로만 조건 제시하기
SELECT
*
FROM
employee
WHERE
empname LIKE '*영*'
AND (title = '대리' OR title = '과장')
;
-- 방법 2 : IN 연산자 사용하기
SELECT
*
FROM
employee
WHERE
empname LIKE '*영*'
AND title IN ('대리', '과장')
;
SELECT
*
FROM
employee
WHERE
manager IS NULL
;
SELECT
*
FROM
employee
WHERE
title = '사원'
;
SELECT
*
FROM
employee
WHERE
title <> '사원'
;
SELECT
empno
, empname
, title
, salary
FROM
employee
WHERE
title IN ('과장', '대리', '부장')
;
SELECT
empname
, title
, dno
, dno * 2 AS '확인 요망'
FROM
employee
;
SELECT
empno
, empname
, title
, salary
FROM
employee
ORDER BY
salary ASC
;
SELECT
empname
, title
, dno
FROM
employee
WHERE
dno = 3
ORDER BY
empno DESC
;
SELECT
COUNT(empname)
, COUNT(manager)
, AVG(salary)
, MAX(empno)
, MIN(empno)
FROM
employee
;
SELECT
COUNT(empname)
, AVG(salary)
, SUM(salary)
, MAX(salary)
, MIN(salary)
FROM
employee
WHERE
dno = 3
;
SELECT
title
, AVG(salary)
, SUM(salary)
FROM
employee
GROUP BY
title
;
SELECT
dno
, AVG(salary)
, MAX(salary)
, MIN(salary)
FROM
employee
WHERE
title IN ('사원', '대리')
GROUP BY
dno
;
-- 방법 1 : HAVING절 사용
SELECT
dno
, AVG(salary)
, MAX(salary)
, MIN(salary)
FROM
employee
GROUP BY
dno
HAVING
dno >= 2
;
-- 방법 2 : WHERE절 사용
SELECT
dno
, AVG(salary)
, MAX(salary)
, MIN(salary)
FROM
employee
WHERE
dno >= 2
GROUP BY
dno
;
-- 방법 1 : HAVING절 사용
SELECT
dno
, AVG(salary)
, MAX(salary)
, MIN(salary)
FROM
employee
WHERE
empname LIKE '김*'
GROUP BY
dno
HAVING
dno IN (1, 3)
ORDER BY
dno
;
-- 방법 2 : WHERE절 사용
SELECT
dno
, AVG(salary)
, MAX(salary)
, MIN(salary)
FROM
employee
WHERE
empname LIKE '김*'
AND dno IN (1, 3)
GROUP BY
dno
ORDER BY
dno
;
INSERT INTO department
VALUES
(5, '마케팅', 12)
;
UPDATE
department
SET
deptname = 'R&D'
WHERE
deptname = '마케팅'
;
DELETE FROM
department
WHERE
deptname = 'R&D'
;
SELECT
d.deptno
, d.deptname
FROM
department d
INNER JOIN employee e
ON d.deptno = e.dno
;
SELECT
DISTINCT d.deptno
, d.deptname
FROM
department d
INNER JOIN employee e
ON d.deptno = e.dno
;
SELECT
d.deptno
, d.deptname
, e.empno
, e.empname
FROM
department d
LEFT JOIN employee e
ON d.deptno = e.dno
;
SELECT
d.deptno
, d.deptname
, e.empno
, e.empname
FROM
department d
LEFT JOIN employee e
ON d.deptno = e.dno
WHERE
e.dno IS NOT NULL
;
SELECT
d.deptno
, d.deptname
, e.empno
, e.empname
FROM
department d
FULL JOIN employee e
ON d.deptno = e.dno
;
SELECT
empno
, empname
FROM
employee
UNION
SELECT
deptno
, deptname
FROM
department
;
-- 1단계 : 부서 이름이 '개발'인 부서번호 반환하기 => 부서번호 : 3
SELECT
deptno
FROM
department
WHERE
deptname = '개발'
;
-- 2단계 : 부서 번호를 위에서 가져온 부서번호로 데이터 조회
SELECT
empname
, title
, dno
FROM
employee
WHERE
dno = 3
;
-- 위 단계를 하나로 합치기 -> 서브쿼리로 만들기
SELECT
empname
, title
, dno
FROM
employee
WHERE
dno IN (
SELECT
deptno
FROM
department
WHERE
deptname = '개발'
)
;