[Database] SQL Assignment 4

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