INNER JOIN, OUTER JOIN
SELECT s.name, p.name
FROM student s INNER JOIN professor p
ON s.profno = p.profno;
SELECT *
FROM professor p LEFT OUTER JOIN student s
ON s.profno = p.profno;
SELECT s.name, p.name
FROM student s FULL OUTER JOIN professor p
ON s.profno = p.profno;
SELECT s.name 학생, p.name 지도교수
FROM student s, professor p
WHERE p.profno = s.profno(+);
SELECT s.name, p.name
FROM student s, professor p
WHERE s.profno = p.profno(+)
UNION
SELECT s.name, p.name
FROM student s, professor p
WHERE s.profno(+) = p.profno;
SELF JOIN
SELECT e1.ename, e1.mgr, e2.empno, e2.ename
FROM emp e1, emp e2
WHERE e1.mgr = e2.empno(+);
Sub Query, 서브쿼리(subquery)
- SQL문을 실행하는 데 필요한 데이터를 추가로 조회하기 위해 SQL문 내부에서 사용하는 SELECT문을 의미
JOIN 연습문제
SELECT s.name, s.deptno1, d.dname
FROM student s, department d
WHERE s.deptno1 = d.deptno;
SELECT e.name
, e.position
, TO_CHAR(e.pay, '999,999,999') PAY
, TO_CHAR(p.s_pay, '999,999,999') "Low PAY"
, TO_CHAR(p.e_pay, '999,999,999') "High PAY"
FROM emp2 e, p_grade p
WHERE e.position = p.position(+)
AND e.position IS NOT NULL;
SELECT e.name
, TRUNC(MONTHS_BETWEEN(TO_DATE(20050530), e.birthday) / 12) AGE
, e.position 현재직급
, p.position 예상직급
FROM emp2 e, p_grade p
WHERE TRUNC((MONTHS_BETWEEN(TO_DATE(20050530), e.birthday) / 12))
BETWEEN p.s_age AND p.e_age
ORDER BY 2;
SELECT c.gname CUST_NAME, c.point, g.gname
FROM customer c, gift g
WHERE c.point > g.g_start
AND g.gno = 7
ORDER BY c.point DESC;
SELECT profno, name, hiredate
, RANK() OVER(ORDER BY hiredate)-1
FROM professor;
SELECT p1.profno, p1.name, p1.hiredate, COUNT(p2.profno) COUNT
FROM professor p1, professor p2
WHERE p1.hiredate > p2.hiredate(+)
GROUP BY p1.profno, p1.name, p1.hiredate
ORDER BY p1.hiredate;
SELECT e1.empno, e1.ename, e1.hiredate, COUNT(e2.empno)
FROM emp e1, emp e2
WHERE e1.hiredate > e2.hiredate(+)
GROUP BY e1.empno, e1.ename, e1.hiredate
ORDER BY e1.hiredate;
Sub Query 연습문제
SELECT s.name, s.deptno1, d.dname
FROM student s, department d
WHERE s.deptno1 = d.deptno
AND s.deptno1 = (SELECT deptno1
FROM student
WHERE name = 'Anthony Hopkins');
SELECT p.name, p.hiredate, d.dname
FROM professor p, department d
WHERE p.deptno = d.deptno
AND p.hiredate > (SELECT hiredate
FROM professor
WHERE name = 'Meg Ryan');
SELECT name, weight
FROM student
WHERE weight >
(SELECT AVG(weight)
FROM student
WHERE deptno1 = 201)
ORDER BY weight DESC;