Oacle 하위 조회 (난이도 있 음)

27312 단어 Oacle
Oacle 기본 Oacle 단일 줄 함수 Oacle 다 중 표 조회 Oacle 그룹 함수 Oacle 서브 조회
--    
--                 
--             
--       ,                             
--       ,             in / any/ all

--     Abel 
-- 10 
SELECT LAST_NAME,SALARY
FROM EMPLOYEES
WHERE SALARY > ( SELECT SALARY 
								 FROM EMPLOYEES
								 WHERE LAST_NAME = 'Abel')

--      'Chen' manage  

SELECT LAST_NAME,EMPLOYEE_ID
FROM EMPLOYEES
WHERE EMPLOYEE_ID = ( SELECT MANAGER_ID
											FROM EMPLOYEES
											WHERE LAST_NAME = 'Chen')

--   job_id  141     ,salary 143         ,job_id   

SELECT LAST_NAME,JOB_ID,SALARY
FROM EMPLOYEES
WHERE JOB_ID = ( SELECT JOB_ID 
								 FROM EMPLOYEES
								 WHERE EMPLOYEE_ID = 141)
AND
SALARY > (  SELECT SALARY 
						FROM EMPLOYEES
						WHERE EMPLOYEE_ID = 143 )

--           last_name,job_id salary

SELECT LAST_NAME,JOB_ID,SALARY
FROM EMPLOYEES
WHERE SALARY = (SELECT MIN(SALARY)
								FROM EMPLOYEES)


--         50          id      

SELECT DEPARTMENT_ID,MIN(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING MIN(SALARY) > ( SELECT MIN(SALARY) 
											 FROM EMPLOYEES
											 WHERE DEPARTMENT_ID = 50 )

--         job_id 'IT_PROG'               ,  ,job_id  salary
-- any         
--                  9000  9000   

SELECT EMPLOYEE_ID,LAST_NAME,JOB_ID,SALARY
FROM EMPLOYEES
WHERE JOB_ID <> 'IT_PROG' AND SALARY < ANY ( SELECT SALARY   -- 9000 6000 4800 4200
																						 FROM EMPLOYEES 
																						 WHERE JOB_ID = 'IT_PROG' )

--         job_id 'IT_PROG'               ,  ,job_id  salary
--             4200   4200   


SELECT EMPLOYEE_ID,LAST_NAME,JOB_ID,SALARY
FROM EMPLOYEES
WHERE JOB_ID <> 'IT_PROG' AND SALARY < ALL ( SELECT SALARY   -- 9000 6000 4800 4200
																						 FROM EMPLOYEES 
																						 WHERE JOB_ID = 'IT_PROG' )
																						 
 -- in        																						 
------------------------------------------------------------
--         !

-- 41.            : last_name, salary	

SELECT LAST_NAME,SALARY
FROM EMPLOYEES
WHERE SALARY = (
								SELECT MIN(SALARY)
								FROM EMPLOYEES
								)


--42.              
SELECT *
FROM DEPARTMENTS
WHERE DEPARTMENT_ID = (
												SELECT DEPARTMENT_ID 
												FROM EMPLOYEES
												HAVING AVG(SALARY) = (
																							SELECT MIN(AVG(SALARY))
																							FROM EMPLOYEES
																							GROUP BY DEPARTMENT_ID
																							)
												GROUP BY DEPARTMENT_ID
												)

-- 43*.                       
--   1
SELECT d.DEPARTMENT_ID "d_id",d.DEPARTMENT_NAME "d_name",Round(AVG(e.SALARY),2) "avg_sala"
FROM DEPARTMENTS d,EMPLOYEES e
WHERE d.DEPARTMENT_ID = (
											SELECT  DEPARTMENT_ID
											FROM EMPLOYEES
											GROUP BY DEPARTMENT_ID
											HAVING AVG(SALARY) = (
																						SELECT MIN(AVG(SALARY)) 
																						FROM EMPLOYEES 
																						GROUP BY DEPARTMENT_ID
																						)
											)
AND e.DEPARTMENT_ID = d.DEPARTMENT_ID(+)
GROUP BY d.DEPARTMENT_ID,d.DEPARTMENT_NAME
--   2
--             ,                 
SELECT  d.*,(SELECT AVG(SALARY) FROM EMPLOYEES WHERE DEPARTMENT_ID = d.DEPARTMENT_ID)
FROM DEPARTMENTS d
WHERE d.DEPARTMENT_ID = (
											SELECT  DEPARTMENT_ID
											FROM EMPLOYEES
											GROUP BY DEPARTMENT_ID
											HAVING AVG(SALARY) = (
																						SELECT MIN(AVG(SALARY)) 
																						FROM EMPLOYEES 
																						GROUP BY DEPARTMENT_ID
																						)
											)

44.           job   
SELECT *
FROM JOBS
WHERE JOB_ID in  (
									SELECT JOB_ID
									FROM EMPLOYEES
									HAVING AVG(SALARY) = (
																				SELECT MAX(AVG(SALARY))
																				FROM EMPLOYEES
																				GROUP BY JOB_ID
																			 )
									GROUP BY JOB_ID
								)

--45.                     ?
-- 8  
SELECT DEPARTMENT_ID,AVG(SALARY)
FROM EMPLOYEES
HAVING AVG(SALARY) > (
											SELECT AVG(SALARY)
											FROM EMPLOYEES
											)
GROUP BY DEPARTMENT_ID



-- 46.          manager      .
-- 18 
SELECT *
FROM EMPLOYEES
WHERE EMPLOYEE_ID in (
											SELECT DISTINCT MANAGER_ID
											FROM EMPLOYEES
											WHERE MANAGER_ID is not null 
											)

-- 47.                            
SELECT MIN(SALARY)
FROM EMPLOYEES
WHERE DEPARTMENT_ID = (
												SELECT DEPARTMENT_ID 
												FROM EMPLOYEES 
												HAVING MAX(SALARY) = (
																							SELECT MIN(MAX(SALARY))  
																							FROM EMPLOYEES
																							GROUP BY DEPARTMENT_ID
																							)
												GROUP BY DEPARTMENT_ID
												)
GROUP BY DEPARTMENT_ID

--48.              manager      : last_name, department_id, email, salary

SELECT LAST_NAME,DEPARTMENT_ID,EMAIL,SALARY
FROM EMPLOYEES
WHERE EMPLOYEE_ID in (
											SELECT DISTINCT MANAGER_ID
											FROM EMPLOYEES
											WHERE DEPARTMENT_ID = (
																							SELECT DEPARTMENT_ID
																							FROM EMPLOYEES 
																							HAVING  AVG(SALARY)  = (
																																	SELECT MAX(AVG(SALARY)) 
																																	FROM EMPLOYEES
																																	GROUP BY DEPARTMENT_ID
																																 )
																							GROUP BY DEPARTMENT_ID
																							)
										  AND MANAGER_ID is not null
											)





-- 49.    1999                        .

SELECT *
FROM EMPLOYEES
WHERE SALARY =  (
								SELECT MAX(SALARY) 
								FROM EMPLOYEES 
								WHERE TO_CHAR(HIRE_DATE,'yyyy') = '1999'
								)
AND TO_CHAR(HIRE_DATE,'yyyy') = '1999'



-- 1.	   Zlotkey              
-- 33 
SELECT LAST_NAME,HIRE_DATE
FROM EMPLOYEES 
WHERE DEPARTMENT_ID = (
												SELECT DEPARTMENT_ID
												FROM EMPLOYEES
												WHERE LAST_NAME = 'Zlotkey'
												)

AND LAST_NAME <> 'Zlotkey'


-- 2.	                   ,     。
--51 
SELECT EMPLOYEE_ID,LAST_NAME,SALARY
FROM EMPLOYEES
WHERE SALARY > (
								SELECT AVG(SALARY)
								FROM EMPLOYEES
								)

-- 3.	                        ,      
--38 
SELECT EMPLOYEE_ID,LAST_NAME,SALARY
FROM EMPLOYEES e1
WHERE SALARY > (
								SELECT AVG(SALARY)
								FROM EMPLOYEES e2
								WHERE e1.DEPARTMENT_ID = e2.DEPARTMENT_ID
								GROUP BY e2.DEPARTMENT_ID
								)
-- 4.	          u                  
--72 
SELECT EMPLOYEE_ID,LAST_NAME
FROM EMPLOYEES
WHERE DEPARTMENT_ID in(
												SELECT DEPARTMENT_ID
												FROM EMPLOYEES
												WHERE LAST_NAME LIKE '%u%'
												)
AND LAST_NAME NOT LIKE '%u%'


--  5.      location_id 1700            
-- 18 
SELECT EMPLOYEE_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (
												SELECT DEPARTMENT_ID
												FROM DEPARTMENTS
												WHERE LOCATION_ID = '1700'
												)


-- 6.      King        
--14 
SELECT LAST_NAME,SALARY
FROM EMPLOYEES
WHERE MANAGER_ID IN (
											SELECT EMPLOYEE_ID
											FROM EMPLOYEES 
											WHERE LAST_NAME = 'King'
											)


	
	
																						 

좋은 웹페이지 즐겨찾기