-
SQL - JOIN, SUB QUERY, INLINE TABLE, IN VIEWDB/SQL 2018. 8. 3. 16:36반응형SELECT *FROM EMPLOYEESWHERE DEPARTMENT_ID IN (SELECT DEPARTMENT_IDFROM EMPLOYEESWHERE FIRST_NAME = 'Steven')AND FIRST_NAME != 'Steven' -- 여러개일때는 IN 사용;SELECT E.EMPLOYEE_ID, E.FIRST_NAME, E.LAST_NAME, E.SALARY, D.DEPARTMENT_NAMEFROM EMPLOYEES E,DEPARTMENTS DWHERE SALARY > (SELECT ROUND( AVG(SALARY), 0 )FROM EMPLOYEES)ORDER BY SALARY;SELECT E.*FROM DEPARTMENTS D, LOCATIONS L, EMPLOYEES EWHERE L.CITY = 'Seattle'AND E.DEPARTMENT_ID = D.DEPARTMENT_IDAND D.LOCATION_ID = L.LOCATION_ID;SELECT *FROM EMPLOYEESWHERE DEPARTMENT_ID IN (SELECT DEPARTMENT_IDFROM DEPARTMENTSWHERE LOCATION_ID = (SELECT LOCATION_IDFROM LOCATIONSWHERE CITY = 'Seattle'));-- 평균 급여가 많은 부서를 조회-- 집계함수 있으면 GROUP BY 사용-- INLINE-VIEW - TABLE의 결과를 TABLE로 쓴다.-- INLINE-TABLE - TABLE에 그대로 SUB QUERY쓰면 INLINE TABLESELECT * -- MAX(SALARY)FROM (SELECT *FROM ( -- INLINE VIEWSELECT DEPARTMENT_ID, AVG(SALARY) AVG_SALARYFROM EMPLOYEESGROUP BY DEPARTMENT_ID)ORDER BY AVG_SALARY DESC)WHERE ROWNUM = 1 -- 1번 불러오기, ORDER BY가 INLINE VIEW에 있어서 ROWNUM 사용 가능;-- PAGENATIONSELECT D.DEPARTMENT_ID, AVG(E.SALARY)FROM EMPLOYEES E,DEPARTMENTS DWHERE SALARY < (SELECT AVG(SALARY)FROM EMPLOYEES)GROUP BY DEPARTMENT_ID;-- 부서별 평균 연봉이 회사 전체 평균연봉보다-- 적은 부서명과 평균연봉을 조회하라.SELECT D.DEPARTMENT_NAME, AVG_D.AVG_SALARYFROM (SELECT DEPARTMENT_ID, AVG(SALARY) AVG_SALARYFROM EMPLOYEESGROUP BY DEPARTMENT_ID) AVG_D, DEPARTMENTS DWHERE AVG_D.DEPARTMENT_ID = D.DEPARTMENT_IDAND AVG_SALARY < (SELECT AVG(SALARY)FROM EMPLOYEES);-- 부서별 평균 연봉이 회사 전체 평균연봉보다-- 적은 부서명과 평균연봉을 조회하라.SELECT AVG_D.DEPARTMENT_NAME, AVG_D.AVG_SALARYFROM (SELECT D.DEPARTMENT_NAME, AVG(SALARY) AVG_SALARYFROM EMPLOYEES E, DEPARTMENTS DWHERE E.DEPARTMENT_ID = D.DEPARTMENT_IDGROUP BY D.DEPARTMENT_NAME) AVG_DWHERE AVG_SALARY >= (SELECT AVG(SALARY)FROM EMPLOYEES);-- 부서별 평균 연봉이 회사 전체 평균연봉보다-- 적은 부서명과 평균연봉을 조회하라.SELECT D.DEPARTMENT_NAME, AVG(SALARY) AVG_SALARYFROM EMPLOYEES E, DEPARTMENTS DWHERE E.DEPARTMENT_ID = D.DEPARTMENT_IDGROUP BY D.DEPARTMENT_NAMEHAVING AVG(SALARY) < (SELECT AVG(SALARY)FROM EMPLOYEES);-- 직무별 평균 연봉이 회사 전체 평균 연봉과 크거나 같은-- 직무를 가진 사원들의 모든 정보 출력SELECT *FROM EMPLOYEES E, (SELECT JOB_ID, AVG(SALARY) AVG_SALARYFROM EMPLOYEESGROUP BY JOB_IDHAVING AVG(SALARY) >= (SELECT AVG(SALARY)FROM EMPLOYEES)) JOB_AVGWHERE JOB_AVG.JOB_ID = E.JOB_ID;-- 단 한명의 사원도 없는 도시명 구하기SELECT CITYFROM LOCATIONSWHERE LOCATION_ID NOT IN (SELECT DISTINCT LOCATION_ID -- 중복제거FROM EMPLOYEES E, DEPARTMENTS DWHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID -- JOIN);-- 같은 조건, SUB QUERYSELECT CITYFROM LOCATIONSWHERE LOCATION_ID IN (SELECT LOCATION_IDFROM LOCATIONSMINUS -- 아래조건을 뺀다SELECT DISTINCT LOCATION_IDFROM EMPLOYEES E, DEPARTMENTS DWHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID);-- 부서의 사원수가 5명 미만인 부서의 부서별 평균 연봉을 구해-- 평균 연봉이 가장 높은 부서의 부서명을 조회SELECT DEPARTMENT_NAMEFROM DEPARTMENTS D, (SELECT DEPARTMENT_ID , AVG(SALARY) AVG_SALARYFROM EMPLOYEESGROUP BY DEPARTMENT_IDHAVING COUNT(DEPARTMENT_ID) < 5ORDER BY AVG_SALARY DESC) AVG_DWHERE D.DEPARTMENT_ID = AVG_D.DEPARTMENT_IDAND ROWNUM = 1;-- 동일조건FROM ( SELECT D.DEPARTMENT_NAME , AVG(SALARY) AVG FROM EMPLOYEES E , DEPARTMENTS D WHERE D.DEPARTMENT_ID = E.DEPARTMENT_ID GROUP BY D.DEPARTMENT_NAME HAVING COUNT(1) < 5 ORDER BY AVG DESC ) WHERE ROWNUM = 1 ;반응형
'DB > SQL' 카테고리의 다른 글
Eclipse - SQL Explorer 실행 (0) 2018.08.26 SQL Study3 - SELECT (0) 2018.08.07 SQL Study2 - SELECT (0) 2018.08.06 SQL SELECT Study (0) 2018.08.03 SQL - SELECT (0) 2018.08.01 댓글