Notice
Recent Posts
Recent Comments
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
관리 메뉴

SYDev

[데이터베이스] LAB4-sql_6 본문

3학년 1학기 전공/데이터베이스

[데이터베이스] LAB4-sql_6

시데브 2024. 4. 14. 12:53
경희대학교 이영구 교수님의 데이터베이스 수업 복습용 게시물입니다.

 

-- 1
SELECT last_name, hire_date
FROM employees
WHERE department_id = (
    SELECT department_id
    FROM employees
    WHERE last_name = 'Zlotkey'
) 
AND last_name <> 'Zlotkey'

 

-- 2 
SELECT employee_id, last_name, salary
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
)
ORDER BY salary

 

-- 3
SELECT employee_id, last_name
FROM employees
WHERE department_id IN (
    SELECT department_id
    FROM employees
    WHERE last_name LIKE '%u%'
)

 

-- 4
SELECT last_name, department_id, job_id
FROM employees
WHERE department_id IN (
    SELECT department_id
    FROM departments
    WHERE location_id = 1700
)

 

 

-- 5
SELECT last_name, salary
FROM employees
WHERE manager_id IN (
    SELECT employee_id
    FROM employees
    WHERE last_name = 'King'
)

SELECT last_name, salary
FROM employees
WHERE manager_id = (
    SELECT employee_id
    FROM employees
    WHERE last_name = 'King' AND employee_id = 100
)

 

-- 6
SELECT department_id, last_name, job_id
FROM employees
WHERE department_id = (
	SELECT department_id
    FROM departments
    WHERE department_name = 'Executive'
)

 

-- 7
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id IN (
    SELECT department_id
    FROM employees
    WHERE salary > (
        SELECT AVG(salary)
        FROM employees
    )
    AND last_name LIKE '%u%'
)

 

-- 8
SELECT employee_id, last_name, salary
FROM employees
WHERE salary > (
    SELECT AVG(e.salary)
    FROM employees e JOIN departments d ON (e.department_id = d.department_id)
    WHERE d.location_id IN (
    	SELECT location_id
    	FROM locations
    	WHERE country_id = 'US'
    )
)

SELECT employee_id, last_name, salary
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees e JOIN departments d 
    	ON (e.department_id = d.department_id)
    JOIN locations l
    	ON (d.location_id = l.location_id)
    GROUP BY l.country_id
    HAVING l.country_id = 'US'
)

 

-- 9
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE (department_id, salary) IN (
    	SELECT department_id , MAX(salary)
        FROM employees
        GROUP BY department_id
)
ORDER BY department_id

 

-- 10
SELECT e.employee_id, e.last_name, e.salary, e.department_id
FROM employees e, (
    	SELECT department_id , MAX(salary) salary
        FROM employees
        GROUP BY department_id
) ee
WHERE e.department_id = ee.department_id 
    AND e.salary = ee.salary
ORDER BY e.department_id

 

-- 11
SELECT d.department_id, d.department_name
FROM departments d
WHERE EXISTS (
    SELECT e.department_id
    FROM employees e
    WHERE d.department_id = e.department_id
)

 

-- 12
SELECT rownum, employee_id, last_name, salary
FROM (
    SELECT *
    FROM employees
    ORDER BY salary
)
WHERE rownum <= 5

 

 

-- 1
SELECT last_name, hire_date
FROM employees
WHERE department_id = (
    SELECT department_id
    FROM employees
    WHERE last_name = 'Zlotkey'
) 
AND last_name <> 'Zlotkey'

-- 2 
SELECT employee_id, last_name, salary
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
)
ORDER BY salary

-- 3
SELECT employee_id, last_name
FROM employees
WHERE department_id IN (
    SELECT department_id
    FROM employees
    WHERE last_name LIKE '%u%'
)

-- 4
SELECT last_name, department_id, job_id
FROM employees
WHERE department_id IN (
    SELECT department_id
    FROM departments
    WHERE location_id = 1700
)

-- 5
SELECT last_name, salary
FROM employees
WHERE manager_id IN (
    SELECT employee_id
    FROM employees
    WHERE last_name = 'King'
)

SELECT last_name, salary
FROM employees
WHERE manager_id = (
    SELECT employee_id
    FROM employees
    WHERE last_name = 'King' AND employee_id = 100
)

-- 6
SELECT department_id, last_name, job_id
FROM employees
WHERE department_id = (
	SELECT department_id
    FROM departments
    WHERE department_name = 'Executive'
)

-- 7
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id IN (
    SELECT department_id
    FROM employees
    WHERE salary > (
        SELECT AVG(salary)
        FROM employees
    )
    AND last_name LIKE '%u%'
)

-- 8
SELECT employee_id, last_name, salary
FROM employees
WHERE salary > (
    SELECT AVG(e.salary)
    FROM employees e JOIN departments d ON (e.department_id = d.department_id)
    WHERE d.location_id IN (
    	SELECT location_id
    	FROM locations
    	WHERE country_id = 'US'
    )
)

SELECT employee_id, last_name, salary
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees e JOIN departments d 
    	ON (e.department_id = d.department_id)
    JOIN locations l
    	ON (d.location_id = l.location_id)
    GROUP BY l.country_id
    HAVING l.country_id = 'US'
)

-- 9
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE (department_id, salary) IN (
    	SELECT department_id , MAX(salary)
        FROM employees
        GROUP BY department_id
)
ORDER BY department_id

-- 10
SELECT e.employee_id, e.last_name, e.salary, e.department_id
FROM employees e, (
    	SELECT department_id , MAX(salary) salary
        FROM employees
        GROUP BY department_id
) ee
WHERE e.department_id = ee.department_id 
    AND e.salary = ee.salary
ORDER BY e.department_id

-- 11
SELECT d.department_id, d.department_name
FROM departments d
WHERE EXISTS (
    SELECT e.department_id
    FROM employees e
    WHERE d.department_id = e.department_id
)

-- 12
SELECT rownum, employee_id, last_name, salary
FROM (
    SELECT *
    FROM employees
    ORDER BY salary
)
WHERE rownum <= 5

 

-- 부질의 없이 평균 급여보다 많은 급여를 받는 사원의 번호, 이름, 급여를 표시
SELECT e1.employee_id, e1.last_name, e1.salary
FROM employees e1, employees e2
GROUP BY e1.employee_id, e1.last_name, e1.salary	-- 이미 employee_id로 group by를 진행했기 때문에 다른 그룹 attribute를 추가해도 그루핑에 문제가 발생하지 않음
HAVING e1.salary > AVG(e2.salary)