2024. 4. 2. 22:25ใComputerScience/Database
#1.
SELECT last_name, department_id, department_name
FROM employees NATURAL JOIN departments
-- department_id๊ฐ ๋์ผํ๊ธฐ ๋๋ฌธ์ NATURAL JOIN (32๊ฐ) -> ํ๋๋ฅผ ์์ ๋ฒ๋ฆฐ๋ค.
-- But departments_id ๋ฟ๋ง ์๋๋ผ, manager_id๊ฐ ๊ฐ๊ธฐ ๋๋ฌธ์ (์๋ก ๋ค๋ฅธ ์๋ฏธ, ๋์ ์ฌ์ vs ๋ถ์์ฅ), natural join์ ํ๋ฉด ํ๋ฆผ
select e.last_name, e.department_id, d.department_name
from employees e INNER JOIN departments d
on (e.department_id = d.department_id)
-- 106๊ฐ ๊ฐ์ด ๋ค๋ฅด๋ค - departments์ manager๋ ๋ถ์์ฅ
-- ๊ผญ join์ ์กฐ๊ฑด์ ๋ถ์ฌ์ฃผ์
#2.
SELECT DISTINCT job_id, location_id
FROM employees JOIN departments USING (department_id)
WHERE department_id = 80;
#3.
SELECT last_name, department_name, location_id, city
FROM employees JOIN departments USING (department_id) NATURAL JOIN locations
WHERE commission_pct is not null
-- location id๋ง ๊ฒน์น๋ฏ๋ก, natural join ๊ฐ๋ฅ
#4.
SELECT last_name, department_name
FROM employees JOIN departments USING (department_id)
WHERE employees.last_name LIKE '%a%'
#5.
SELECT last_name, job_id, e.department_id, department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id
WHERE l.city = 'Toronto';
#6.
SELECT worker.last_name "Employee", worker.employee_id "Emp#", manager.last_name "Manager", worker.manager_id "Mgr#"
FROM employees worker, employees manager
WHERE worker.manager_id = manager.employee_id
-- ์ ๊ฐ์ table์ธ๋ฐ join ์ฌ์ฉ?
-- manager ์ด๋ฆ์ด ์ถ๊ฐ๋๋๋ก, self join์ ํด์ผ ํ๋ค.
* NULL ๊ฐ์ ์กฐ์ธ์ ํฌํจ์ด ์๋๋๋ฐ, ๋งค๋์ ๊ฐ ์๋ ์ฌ์๋ ํฌํจํ๊ธฐ ์ํด์ left outer join์ ํด์ค์ผ ํจ
SELECT worker.last_name "Employee", worker.employee_id "Emp#", manager.last_name "Manager", worker.manager_id "Mgr#"
FROM employees worker LEFT OUTER JOIN employees manager ON worker.manager_id = manager.employee_id
#7.
SELECT worker.last_name "Employee", department_name "Department", collegue.last_name "Colleague"
FROM employees worker, employees collegue, departments
WHERE worker.department_id = departments.department_id
AND worker.department_id = collegue.department_id
AND worker.last_name != collegue.last_name
ORDER BY departments.department_id, worker.last_name, collegue.last_name
* ํด๋น ์ฌ์๊ณผ ๋์ผํ ๋ถ์์์ ๊ทผ๋ฌดํ๋ ๋ชจ๋ ์ฌ์์ ํ์ํ๋ผ
* ์๊ธฐ ์์ ๋ ๊ฐ์ ๋ถ์์ ๊ทผ๋ฌดํ๊ธฐ ๋๋ฌธ์, ๋ถํ์ํ ํฌํ์ด ๋ง๋ค์ด์ง
-- Department ํ์ X
SELECT worker.last_name "Employee", collegue.last_name "Colleague"
FROM employees worker, employees collegue
WHERE worker.department_id = collegue.department_id
AND worker.last_name != collegue.last_name
ORDER BY worker.department_id, worker.last_name, collegue.last_name
* (a, b)์ (b,a) ์ฌ์ ํ ๋ ๋ฒ ์ถ๋ ฅ๋จ -> how?
SELECT worker.last_name "Employee", collegue.last_name "Colleague"
FROM employees worker, employees collegue
WHERE worker.department_id = collegue.department_id
AND worker.last_name > collegue.last_name
ORDER BY worker.department_id, worker.last_name, collegue.last_name
* ๋ถ๋ฑํธ๋ฅผ ํตํด์ ํด๊ฒฐ ๊ฐ๋ฅ
#8.
SELECT e.last_name, e.hire_date
FROM employees e, employees d
WHERE d.last_name = 'Davies' AND d.hire_date <= e.hire_date AND e.last_name <> 'Davies'
* where ์ ์์ ์กฐ๊ฑด์ ์ ๊ฒ์ํ๋๋ก big table์ ์ ๊ตฌ์ฑํด์ผ ํ๋ค.
* Self-join ํ๋ ๊ฒ์ ์์ฐ์ค๋ฝ์ง X -> ์ด์ค select
* ์ค์ฒฉ ์ง์
SELECT last_name, hire_date
FROM employees
WHERE hire_date >= (SELECT hire_date from employees where last_name = 'Davies')
-- ์์ชฝ์ ๋ค์ด๊ฐ ์ง์๋ฅผ sub query, ๋ถ์ง์๋ผ๊ณ ํ๋ค. ๊ดํธ ์์ ๋ฃ์ด์ผ ํจ
#9.
SELECT e.last_name "Employee NAME", e.hire_date, m.last_name "Manager NAME", m.hire_date
FROM employees e, employees m
WHERE e.manager_id = m.employee_id AND e.hire_date <= m.hire_date
* ๊ด๋ฆฌ์์ ์ ์ฌ์ผ๊น์ง ํ ๋ฒ์ ๋ํ๋ผ ์ ์์ผ๋, self-join์ด ํ์ํ ๊ฒ
'ComputerScience > Database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Database] SQL Assignment 6 (1) | 2024.04.19 |
---|---|
[Database] SQL Assignment 5 (2) | 2024.04.19 |
[Database] SQL Assignment 3 (0) | 2024.04.02 |
[Database] SQL Assignment 2 (0) | 2024.04.01 |