-- 7-1
CREATE TABLE CUSTOMER(
USERID CHAR(10) NOT NULL,
USERNAME VARCHAR2(20) NOT NULL,
AGE INT,
RATONG VARCHAR2(20),
JOB VARCHAR2(20),
DEPOSIT INT DEFAULT 0,
PRIMARY KEY(USERID)
);
-- 7-2
CREATE TABLE PRODUCT(
PRODUCTID CHAR(10) NOT NULL,
PRODUCTNAME VARCHAR2(20) NOT NULL,
INVENT INT,
PRICE INT CHECK(PRICE BETWEEN 0 AND 10000),
COMPANY VARCHAR(20),
PRIMARY KEY(PRODUCTID)
);
-- 7-3
CREATE TABLE ORDERS(
ORDERID CHAR(10) NOT NULL,
ORDERCUSTOMER CHAR(10),
ORDERPRODUCT CHAR(10),
POS INT,
SHIPPING VARCHAR2(20),
ORDERDATE DATE,
PRIMARY KEY(ORDERID),
FOREIGN KEY(ORDERCUSTOMER) REFERENCES CUSTOMER(USERID),
FOREIGN KEY(ORDERPRODUCT) REFERENCES PRODUCT(PRODUCTID)
);
DROP TABLE ORDERS;
DROP TABLE CUSTOMER;
-- 7-4
CREATE TABLE DELIVERY(
DELIVERYID INT NOT NULL,
DELIVERYNAME int not null,
SHIPPING VARCHAR2(20),
PHONE VARCHAR2(20),
PRIMARY KEY(DELIVERYID)
);
-- 7-5
ALTER TABLE CUSTOMER
ADD 가입날짜 DATE;
-- 7-6
ALTER TABLE CUSTOMER
DROP COLUMN 가입날짜;
-- 7-7
ALTER TABLE CUSTOMER ADD CONSTRAINT conpos CHECK (AGE >= 20);
-- 7-8
ALTER TABLE CUSTOMER DROP CONSTRAINT conpos;
COMMIT;
-- 7-9
DROP TABLE DELIVERY;
-- 데이터 입력
INSERT INTO CUSTOMER VALUES('apple', '정소화', 20, 'gold', '학생', 1000);
INSERT INTO CUSTOMER VALUES('banana', '김선우', 25, 'vip', '간호사', 2500);
INSERT INTO CUSTOMER VALUES('carrot', '고명석', 28, 'gold', '교사', 4500);
INSERT INTO CUSTOMER VALUES('orange', '김용욱', 22, 'silver', '학생', 0);
INSERT INTO CUSTOMER VALUES('melon', '성원용', 35, 'gold', '회사원', 5000);
INSERT INTO CUSTOMER VALUES('peach', '요형준', NULL, 'silver', '의사', 300);
INSERT INTO CUSTOMER VALUES('pear', '채광주', 31, 'silver', '회사원', 500);
SELECT * FROM CUSTOMER;
COMMIT;
INSERT INTO PRODUCT VALUES('p01','그냥만두',5000,4500,'대한식품');
INSERT INTO PRODUCT VALUES ('p02', '매운쫄면', 2500, 5500, '민국푸드');
INSERT INTO PRODUCT VALUES ('p03', '쿵떡파이', 3600, 2600, '한빛제과');
INSERT INTO PRODUCT VALUES ('p04', '맛난초콜릿', 1250, 2500, '한빛제과');
INSERT INTO PRODUCT VALUES ('p05', '얼큰라면', 2200, 1200, '대한식품');
INSERT INTO PRODUCT VALUES ('p06', '통통우동', 1000, 1550, '민국푸드');
INSERT INTO PRODUCT VALUES ('p07', '달콤비스킷', 1650, 1500, '한빛제과');
commit;
INSERT INTO ORDERS VALUES ('o01', 'apple', 'p03', 10, '서울시 마포구', '22/01/01');
INSERT INTO ORDERS VALUES ('o02', 'melon', 'p01', 5, '인천시 계양구', '22/01/10');
INSERT INTO ORDERS VALUES ('o03', 'banana', 'p06', 45, '경기도 부천시', '22/01/11');
INSERT INTO ORDERS VALUES ('o04', 'carrot', 'p02', 8, '부산시 금정구', '22/02/01');
INSERT INTO ORDERS VALUES ('o05', 'melon', 'p06', 36, '경기도 용인시', '22/02/20');
INSERT INTO ORDERS VALUES ('o06', 'banana', 'p01', 19, '충청북도 보은군', '22/03/02');
INSERT INTO ORDERS VALUES ('o07', 'apple', 'p03', 22, '서울시 영등포구', '22/03/15');
INSERT INTO ORDERS VALUES ('o08', 'pear', 'p02', 50, '강원도 춘천시', '22/04/10');
INSERT INTO ORDERS VALUES ('o09', 'banana', 'p04', 15, '전라남도 목포시', '22/04/11');
INSERT INTO ORDERS VALUES ('o10', 'carrot', 'p03', 20, '경기도 안양시', '22/05/22');
commit;
select * from ORDERS;
-- 7-10
SELECT USERID, USERNAME, RATONG
FROM CUSTOMER;
-- 7-11
SELECT *
FROM CUSTOMER;
-- 7-12
SELECT *
FROM CUSTOMER;
-- 7-13
SELECT COMPANY
FROM PRODUCT;
-- 7-14
SELECT ALL COMPANY
FROM PRODUCT;
-- 7-15
SELECT DISTINCT COMPANY
FROM PRODUCT;
-- 7-16
SELECT *
FROM PRODUCT;
SELECT PRODUCTNAME, PRICE AS 단가
FROM product;
-- 7-17
SELECT PRODUCTNAME, PRICE + 500 AS "조정 단가"
FROM product;
--- 7-18
SELECT PRODUCTNAME, INVENT, PRICE
FROM product
WHERE company='한빛제과';
-- 7-19
SELECT *
FROM CUSTOMER;
SELECT ORDERID, POS, ORDERDATE
FROM ORDERS
WHERE ORDERCUSTOMER = 'apple' AND POS>=15;
-- 7-20
SELECT ORDERPRODUCT, POS, ORDERDATE, ORDERCUSTOMER
FROM ORDERS
WHERE ORDERCUSTOMER = 'apple' OR POS>=15;
-- 7-21
SELECT PRODUCTNAME, PRICE, COMPANY
FROM PRODUCT
WHERE PRICE BETWEEN 2000 AND 3000;
-- 7-22
SELECT USERNAME, AGE, RATONG, DEPOSIT
FROM CUSTOMER
WHERE USERNAME LIKE '김%';
-- 7-23
SELECT USERID, USERNAME, RATONG
FROM CUSTOMER
WHERE USERID LIKE '_____';
-- 7-24
SELECT USERNAME
FROM CUSTOMER
WHERE AGE IS NULL;
-- 7-25
SELECT USERNAME
FROM CUSTOMER
WHERE AGE IS NOT NULL;
-- 7-26
SELECT USERNAME, RATONG, AGE
FROM CUSTOMER
ORDER BY AGE DESC;
-- 7-27
SELECT ORDERCUSTOMER, ORDERPRODUCT, POS, ORDERDATE
FROM ORDERS
ORDER BY ORDERPRODUCT ASC, POS DESC;
-- 7-28
SELECT AVG(PRICE)
FROM PRODUCT;
-- 7-29
SELECT SUM(INVENT) AS "재고량 합계"
FROM PRODUCT
WHERE COMPANY = '한빛제과';
-- 7-30
SELECT COUNT(*)
FROM CUSTOMER;
-- 7-31
SELECT COUNT(DISTINCT COMPANY)
FROM PRODUCT;
-- 7-32
SELECT ORDERPRODUCT, SUM(POS)
FROM ORDERS
GROUP BY ORDERPRODUCT;
-- 7-33
SELECT *
FROM PRODUCT;
SELECT COMPANY AS 제조업체, COUNT(COMPANY) "제품수", MAX(PRICE) "최고가"
FROM PRODUCT
GROUP BY COMPANY;
-- 7-34
SELECT COMPANY AS 제조업체, COUNT(COMPANY) "제품수", MAX(PRICE) "최고가"
FROM PRODUCT
GROUP BY COMPANY
HAVING COUNT(COMPANY) >=3;
-- 7-35
SELECT *
FROM ORDERS;
SELECT *
FROM PRODUCT;
SELECT *
FROM CUSTOMER;
SELECT RATONG, COUNT(RATONG), AVG(DEPOSIT)
FROM CUSTOMER
GROUP BY RATONG
HAVING AVG(DEPOSIT) >= 1000;
-- 7-36
SELECT ORDERPRODUCT, ORDERCUSTOMER, SUM(POS)
FROM ORDERS
GROUP BY ORDERPRODUCT, ORDERCUSTOMER;
-- 7-37
SELECT PRODUCT.PRODUCTNAME
FROM ORDERS, PRODUCT
WHERE orders.orderproduct = PRODUCT.PRODUCTID AND ORDERS.ORDERCUSTOMER='banana';
-- 7-38
SELECT PRODUCT.PRODUCTID, ORDERS.ORDERDATE
FROM PRODUCT
JOIN ORDERS ON ORDERS.ORDERPRODUCT = PRODUCT.PRODUCTID
JOIN CUSTOMER ON CUSTOMER.USERID = ORDERS.ORDERCUSTOMER
WHERE CUSTOMER.AGE >= 30;
-- 7-39
SELECT PRODUCT.PRODUCTNAME
FROM PRODUCT
JOIN ORDERS ON ORDERS.ORDERPRODUCT = PRODUCT.PRODUCTID
JOIN CUSTOMER ON CUSTOMER.USERID = ORDERS.ORDERCUSTOMER
WHERE CUSTOMER.USERNAME ='고명석';
교차조인(CROSS JOIN)
두 테이블의 모든 행이 각각 한번씩 조인되어 모든 경우의 수를 조합한 조인 데이터를 출력
카테시안 곱 또는 카시안 조인이라고 한다.
CROSS JOIN 표기법 ANSI 표기법
SELECT a.empno,
a.ename,
a.job,
b.deptno,
b.dname
FROM EMP a CROSS JOIN dept b; -- 14X4 = 56개
-- 오라클 표기법
SELECT a.empno, a.ename, a.job, b.deptno, b.dname
FROM emp a, dept b;
교집합
SELECT a.empno, a.ename, d.deptno, d.dname
FROM emp a INNER JOIN dept d
ON a.deptno = d.deptno
WHERE a.job = 'MANAGER';
-- 오라클 버전
SELECT a.empno, a.ename, b.deptno, b.dname
FROM emp a, dept b
WHERE a.job = 'MANAGER' AND a.deptno = b.deptno;
-- INNER JOIN 메인테이블(emp)과 조인테이블(dept)에 조인컬럼(deptno)의 값이 동시에 존재해야 조회가 된다.
SELECT EMPNO, ENAME, e.DEPTNO, DNAME, LOC
FROM EMP e, DEPT b
WHERE e.deptno = b.deptno;
-- INNER JOIN 메인테이블(emp)과 조인테이블(dept)에 조인컬럼(deptno)의 값이 동시에 존재해야 조회가 된다.
SELECT EMPNO, ENAME, e.DEPTNO, DNAME, LOC
FROM EMP e, DEPT b
WHERE e.deptno = b.deptno
ORDER BY b.deptno, e.empno;
-- 8-6 사원번호, 이름, 급여, 근무부서명이 함께 출력, 출력조건 : 급여가 3000이상인 사원의 정보
SELECT e.EMPNO, e.ENAME, e.SAL, b.DNAME
FROM EMP e, DEPT b
WHERE sal >=3000 and e.deptno = b.deptno
ORDER BY e.ename;
CREATE TABLE orders(
ID INT NOT NULL,
PRODUCT VARCHAR(20),
PRIMARY KEY(ID)
);
CREATE TABLE point(
ID INT NOT NULL,
POINT INT,
PRIMARY KEY(ID)
);
INSERT INTO orders VALUES(1, 'APPLE');
INSERT INTO orders VALUES(2, 'BOOK');
INSERT INTO orders VALUES(3, 'CHICKEN');
INSERT INTO orders VALUES(4, 'PENCIL');
INSERT INTO orders VALUES(5, 'DOOR');
COMMIT;
INSERT INTO point VALUES(3, 100);
INSERT INTO point VALUES(4, 200);
INSERT INTO point VALUES(5, 300);
INSERT INTO point VALUES(6, 400);
INSERT INTO point VALUES(7, 500);
COMMIT;
-- 안시
SELECT o.id, o.product, p.id, p.point
FROM orders o INNER JOIN point p
ON o.id=p.id;
-- 오라클
SELECT o.id, o.product, p.id, p.point
FROM orders o, point p
WHERE o.id=p.id;
CREATE TABLE POINT(
ID INT NOT NULL,
PRODUCT VARCHAR(20) NOT NULL,
PRIMARY KEY(ID)
);
CREATE TABLE orders(
id INT NOT NULL,
product VARCHAR2(20) NOT NULL,
CONSTRAINT fk_id FOREIGN KEY (id) REFERENCES point(id)
);
DROP TABLE ORDERS;
INSERT INTO orders VALUES(1, 'APPLE');
INSERT INTO orders VALUES(2, 'BOOK');
INSERT INTO orders VALUES(3, 'CHICKEN');
INSERT INTO orders VALUES(4, 'PENCIL');
INSERT INTO orders VALUES(5, 'DOOR');
COMMIT;
INSERT INTO point VALUES(3, 100);
INSERT INTO point VALUES(4, 200);
INSERT INTO point VALUES(5, 300);
INSERT INTO point VALUES(6, 400);
INSERT INTO point VALUES(7, 500);
COMMIT;
SELECT *
FROM orders p INNER JOIN point o ON P.ID = o.ID;
--------------------------------------------------------------
-- 1. INNER JOIN - 기본 사용법
--------------------------------------------------------------
WITH emp AS (
SELECT 7839 empno, 'KING' ename, 'PRESIDENT' job, NULL mgr, 90 deptno, 13 locno FROM dual UNION ALL
SELECT 7698 empno, 'BLAKE' ename, 'MANAGER' job, 7839 mgr, 30 deptno, 11 locno FROM dual UNION ALL
SELECT 7782 empno, 'CLARK' ename, 'MANAGER' job, 7839 mgr, 10 deptno, 13 locno FROM dual UNION ALL
SELECT 7566 empno, 'JONES' ename, 'MANAGER' job, 7839 mgr, 20 deptno, 12 locno FROM dual
), dept AS (
SELECT 10 deptno, 'ACCOUNTING' dname, 13 locno FROM dual UNION ALL
SELECT 20 deptno, 'RESEARCH' dname, 11 locno FROM dual UNION ALL
SELECT 30 deptno, 'SALES' dname, 11 locno FROM dual UNION ALL
SELECT 40 deptno, 'OPERATIONS' dname, 12 locno FROM dual
), loc AS (
SELECT 11 locno, 'TEXAS' lname FROM dual UNION ALL
SELECT 12 locno, 'CALIFORNIA' lname FROM dual UNION ALL
SELECT 13 locno, 'WASHINGTON' lname FROM dual
)
-- INNER JOIN은 교집합, 그렇기 때문에 90이 있는 KING은 나오지 않음
SELECT a.empno
, a.ename
, a.job
, a.deptno
, b.dname
FROM emp a
INNER JOIN dept b
ON a.deptno = b.deptno
WHERE job IN ('PRESIDENT', 'MANAGER');
SELECT *
FROM EMP
WHERE ename='KING';
--------------------------------------------------------------
-- 2. INNER JOIN - 조인 칼럼이 여러개인 경우
--------------------------------------------------------------
WITH emp AS (
SELECT 7839 empno, 'KING' ename, 'PRESIDENT' job, NULL mgr, 90 deptno, 13 locno FROM dual UNION ALL
SELECT 7698 empno, 'BLAKE' ename, 'MANAGER' job, 7839 mgr, 30 deptno, 11 locno FROM dual UNION ALL
SELECT 7782 empno, 'CLARK' ename, 'MANAGER' job, 7839 mgr, 10 deptno, 13 locno FROM dual UNION ALL
SELECT 7566 empno, 'JONES' ename, 'MANAGER' job, 7839 mgr, 20 deptno, 12 locno FROM dual
), dept AS (
SELECT 10 deptno, 'ACCOUNTING' dname, 13 locno FROM dual UNION ALL
SELECT 20 deptno, 'RESEARCH' dname, 11 locno FROM dual UNION ALL
SELECT 30 deptno, 'SALES' dname, 11 locno FROM dual UNION ALL
SELECT 40 deptno, 'OPERATIONS' dname, 12 locno FROM dual
), loc AS (
SELECT 11 locno, 'TEXAS' lname FROM dual UNION ALL
SELECT 12 locno, 'CALIFORNIA' lname FROM dual UNION ALL
SELECT 13 locno, 'WASHINGTON' lname FROM dual
)
SELECT a.empno
, a.ename
, a.job
, a.deptno
, b.dname
, a.locno
FROM emp a
INNER JOIN dept b
ON a.deptno = b.deptno
AND a.locno = b.locno
WHERE job IN ('PRESIDENT', 'MANAGER');
--------------------------------------------------------------
-- 3. INNER JOIN - 조인 테이블이 여러개인 경우
--------------------------------------------------------------
WITH emp AS (
SELECT 7839 empno, 'KING' ename, 'PRESIDENT' job, NULL mgr, 90 deptno, 13 locno FROM dual UNION ALL
SELECT 7698 empno, 'BLAKE' ename, 'MANAGER' job, 7839 mgr, 30 deptno, 11 locno FROM dual UNION ALL
SELECT 7782 empno, 'CLARK' ename, 'MANAGER' job, 7839 mgr, 10 deptno, 13 locno FROM dual UNION ALL
SELECT 7566 empno, 'JONES' ename, 'MANAGER' job, 7839 mgr, 20 deptno, 12 locno FROM dual
), dept AS (
SELECT 10 deptno, 'ACCOUNTING' dname, 13 locno FROM dual UNION ALL
SELECT 20 deptno, 'RESEARCH' dname, 11 locno FROM dual UNION ALL
SELECT 30 deptno, 'SALES' dname, 11 locno FROM dual UNION ALL
SELECT 40 deptno, 'OPERATIONS' dname, 12 locno FROM dual
), loc AS (
SELECT 11 locno, 'TEXAS' lname FROM dual UNION ALL
SELECT 12 locno, 'CALIFORNIA' lname FROM dual UNION ALL
SELECT 13 locno, 'WASHINGTON' lname FROM dual
)
SELECT a.empno
, a.ename
, a.job
, a.deptno
, b.dname
, a.locno
, c.lname
FROM emp a
INNER JOIN dept b
ON a.deptno = b.deptno
INNER JOIN loc c
ON a.locno = c.locno
WHERE job IN ('PRESIDENT', 'MANAGER');
SELECT * FROM salgrade; -- 급여 등급 기준이 저장되어 있는 테이블(최소금액과 최대금액)
SELECT * FROM emp;
SELECT e.ename, s.grade, s.losal, s.hisal
FROM emp e, salgrade s
WHERE e.sal BETWEEN s.losal AND s.hisal;
CREATE TABLE POINT(
ID INT NOT NULL,
POINT VARCHAR(20) NOT NULL,
PRIMARY KEY(ID)
);
CREATE TABLE orders(
id INT NOT NULL,
product VARCHAR2(20) NOT NULL,
-- CONSTRAINT fk_id FOREIGN KEY (id) REFERENCES point(id)
PRIMARY KEY (id)
);
DROP TABLE POINT;
DESC POINT;
SELECT *
FROM orders;
INSERT INTO point VALUES(3, 100);
INSERT INTO point VALUES(4, 200);
INSERT INTO point VALUES(5, 300);
INSERT INTO point VALUES(6, 400);
INSERT INTO point VALUES(7, 500);
COMMIT;
INSERT INTO orders VALUES(1, 'APPLE');
INSERT INTO orders VALUES(2, 'BOOK');
INSERT INTO orders VALUES(3, 'CHICKEN');
INSERT INTO orders VALUES(4, 'PENCIL');
INSERT INTO orders VALUES(5, 'DOOR');
COMMIT;
SELECT o.id, o.product, p.id, p.point
FROM orders o
LEFT OUTER JOIN POINT p ON o.id = p.id
ORDER BY o.ID ASC;
SELECT o.id, o.product, p.id, p.point
FROM ORDERS o, POINT p
WHERE o.id=p.id(+)
ORDER BY o.ID ASC;
-- 오른쪽 조인
SELECT o.id, o.product, p.id, p.point
FROM orders o
RIGHT OUTER JOIN POINT p ON o.id = p.id
ORDER BY o.ID ASC;
SELECT o.id, o.product, p.id, p.point
FROM ORDERS o, POINT p
WHERE o.id(+)=p.id
ORDER BY o.ID ASC;
SELECT e1.empno 사원번호, e1.ename 사원이름, e1.mgr 상관번호,
e2.empno e2상관번호, e2.ename e2상관이름
FROM emp e1, emp e2
WHERE e1.mgr = e2.empno;
+++ LEFT OUTER JOIN으로 변경 ⇒ KING이 생김(NULL 포함)
-- LEFT OUTER JOIN
SELECT e1.empno 사원번호, e1.ename 사원이름, e1.mgr 상관번호,
e2.empno e2상관번호, e2.ename e2상관이름
FROM emp e1, emp e2
WHERE e1.mgr = e2.empno(+);
-- 예제 1
SELECT d.DEPTNO, d.DNAME, e.EMPNO, e.ENAME, e.SAL
FROM EMP e INNER JOIN DEPT d
ON e.deptno=d.deptno
WHERE e.SAL>2000;
-- 다른 풀이
SELECT d.DEPTNO, d.DNAME, e.EMPNO, e.ENAME, e.SAL
FROM EMP e, DEPT d
WHERE e.deptno=d.deptno and e.SAL>2000;
-- 예제 2
SELECT d.DEPTNO, d.DNAME, ROUND(AVG(e.SAL)) AS AVG_SAL, MAX(e.SAL) AS MAX_SAL,
MIN(e.SAL) AS MIN_SAL, COUNT(d.DEPTNO) AS CNT
FROM EMP e INNER JOIN DEPT d
ON e.deptno=d.deptno -- 셀을 가져오기 위해 둘을 합쳐줌
GROUP BY d.DEPTNO, d.DNAME
ORDER BY d.DEPTNO;
-- 예제 3
SELECT d.DEPTNO, d.DNAME, e.EMPNO, e.ENAME, e.JOB, e.SAL
FROM DEPT d LEFT OUTER JOIN EMP e
ON e.deptno=d.deptno
ORDER BY d.DEPTNO, e.ename;
-- 예제 4
SELECT D.DEPTNO, D.DNAME, E.EMPNO, E.ENAME, E.MGR, E.SAL, E.DEPTNO, S.LOSAL, S.HISAL, S.GRADE, E2.EMPNO AS MGR_EMPNO, E2.ENAME AS MGR_ENAME
FROM EMP E RIGHT OUTER JOIN DEPT D ON (E.DEPTNO = D.DEPTNO)
LEFT OUTER JOIN SALGRADE S ON (E.SAL BETWEEN S.LOSAL AND S.HISAL)
LEFT OUTER JOIN EMP E2 ON (E.MGR = E2.EMPNO)
ORDER BY D.DEPTNO, E.EMPNO;
--
오늘 하루도 열심히 들었다... 요즘 체력이 좀.. 주말 알바 하루를 빼야 하나 고민중