sql์—์„œ group by, having, join

LeeKyoungChangยท2022๋…„ 12์›” 6์ผ
0

database

๋ชฉ๋ก ๋ณด๊ธฐ
3/7
post-thumbnail

๐Ÿ“š 3. sql์—์„œ group by, having, join

๐Ÿ“– A. group by, having

๐Ÿ“ ์‹œ์ž‘ํ•˜๊ธฐ์ „, group by์™€ having ์ ˆ

select [distinct] [์ปฌ๋Ÿผ1,์ปฌ๋Ÿผ2,.....][as ๋ณ„๋ช…][ || ์—ฐ์‚ฐ์ž][*]  --- 6
from ํ…Œ์ด๋ธ”๋ช…     --- 1
 [where ์กฐ๊ฑด์ ˆ]    --- 2
 [group by์ปฌ๋Ÿผ๋ช…]  --- 3
  [having ์กฐ๊ฑด์ ˆ]   --- 4
  [order by ์ปฌ๋Ÿผ๋ช… asc|desc ]  --- 5
  • group by : ๊ทธ๋ฃนํ•จ์ˆ˜(max,min,sum,avg,count..)์™€ ๊ฐ™์ด ์‚ฌ์šฉ
  • having : ๋ฌถ์–ด๋†“์€ ๊ทธ๋ฃน์˜ ์กฐ๊ฑด์ ˆ

๐Ÿ’ก ์ฐธ๊ณ 

  • ์—…๋ฌดID : job id
  • ๋ถ€์„œID : department_id
  • ์‚ฌ์›๋ช… : last_name

ย 

โœ”๏ธ ํ‰๊ท  ๊ตฌํ•˜๊ธฐ

์‚ฌ์›๊ธ‰์—ฌํ‰๊ท 

  • format : ์†Œ์ˆ˜์  ํ‘œ์‹œ, ์ฒœ๋‹จ์œ„ ์ฝค๋งˆ ์ฐ๊ธฐ, ๋‚ ์งœ ๋ฌธ์ž์—ด ๋ณ€ํ™˜
  • floor : ์†Œ์ˆ˜์  ์ฒซ์งธ ์ž๋ฆฌ์—์„œ ๋ฒ„๋ฆผํ•˜๋Š” ํ•จ์ˆ˜
select format(avg(salary),0) as "์‚ฌ์›๊ธ‰์—ฌํ‰๊ท " from employees;
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-12-06 แ„‹แ…ฉแ„’แ…ฎ 5 19 34

ย 

โœ”๏ธ ๋ถ€์„œ๋ณ„๋กœ ๊ธ‰์—ฌ ํ‰๊ท ์„ ๊ตฌํ•ด์„œ ๋ถ€์„œ ID, ๊ธ‰์—ฌ ํ‰๊ท ์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

select department_id as "๋ถ€์„œID", format(avg(salary),0)as "๊ธ‰์—ฌํ‰๊ท "
from employees
group by department_id
order by department_id desc;
  • ๋ถ€์„œ๋ฅผ ๊ทธ๋ฃน ์ง€์ •ํ•œ๋‹ค.
  • ๋ถ€์„œ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.

ย 

โœ”๏ธ ์—…๋ฌดID๋ณ„ ๊ธ‰์—ฌ์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•ด์„œ ์—…๋ฌดID, ๊ธ‰์—ฌํ•ฉ๊ณ„๋ฅผ ์ถœ๋ ฅ

select job_id as "์—…๋ฌดID", format(sum(salary),0) as "๊ธ‰์—ฌํ•ฉ๊ณ„"
from employees
group by job_id
order by job_id;
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-12-06 แ„‹แ…ฉแ„’แ…ฎ 5 32 10

ย 

โœ”๏ธ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌํ‰๊ท ์„ ๊ตฌํ•ด์„œ ํ‰๊ท ๊ธ‰์—ฌ๊ฐ€ 6000์ด์ƒ์ธ ๋ถ€์„œ๋งŒ ์ถœ๋ ฅ (8๊ฑด)

  • (ํ‰๊ท ๊ธ‰์—ฌ๋Š” ์†Œ์ˆ˜์  ์ดํ•˜ ์ ˆ์‚ญ)
--      ๋ถ€์„œID   ํ‰๊ท ๊ธ‰์—ฌ
--     -----------------------
--        NULL    7000
--        20      9500
select department_id as "๋ถ€์„œID", floor(avg(salary)) as "ํ‰๊ท ๊ธ‰์—ฌ"
from employees
group by department_id
having avg(salary) >= 6000;

ย 

โœ”๏ธ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌํ‰๊ท ์„ ๊ตฌํ•˜์‹œ์˜ค (9๊ฑด)

  • ์กฐ๊ฑด1) ์†Œ์ˆ˜์ดํ•˜๋Š” ๋ฐ˜์˜ฌ๋ฆผ
  • ์กฐ๊ฑด2) ์„ธ์ž๋ฆฌ๋งˆ๋‹ค์ฝค๋งˆ, ํ™”ํŽ˜๋‹จ์œ„ ๏ฟฆ๋ฅผ ํ‘œ์‹œ
  • ์กฐ๊ฑด3) ๋ถ€์„œ์ฝ”๋“œ ํ‰๊ท ๊ธ‰์—ฌ
--            NULL    ๏ฟฆ7,000
--            20      ๏ฟฆ9,500
  • ์กฐ๊ฑด4) ๋ถ€์„œ๋ณ„๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์ •๋ ฌํ•˜์‹œ์˜ค
  • ์กฐ๊ฑด5) ํ‰๊ท ๊ธ‰์—ฌ๊ฐ€ 5000์ด์ƒ์ธ ๋ถ€์„œ๋งŒ ํ‘œ์‹œํ•˜์‹œ์˜ค

select department_id as "๋ถ€์„œ์ฝ”๋“œ", concat('โ‚ฉ',format(round(avg(salary),0),0)) as "ํ‰๊ท ๊ธ‰์—ฌ"
from employees
group by department_id
having avg(salary) >= 5000
order by department_id;
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-12-06 แ„‹แ…ฉแ„’แ…ฎ 5 48 20

ย 

โœ”๏ธ 10๊ณผ 20 ๋ถ€์„œ์—์„œ ์ตœ๋Œ€๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š”์‚ฌ๋žŒ์˜ ๊ธ‰์—ฌ๋ฅผ ๊ตฌํ•˜์‹œ์˜ค, ๋ถ€์„œ๋ณ„๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์‹œ์˜ค

-- department_id     max_salary
-----------------------------
-- 10                    4400
-- 20                    13000 
select department_id, round(max(salary),0) as "max_salary"
from employees
where department_id in (10,20)
group by department_id
order by 2;
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-12-06 แ„‹แ…ฉแ„’แ…ฎ 5 53 21

having์ ˆ (where + group by + having)

  • 10๊ณผ 20 ๋ถ€์„œ์—์„œ ์ตœ๋Œ€๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š”์‚ฌ๋žŒ์˜ ๊ธ‰์—ฌ๋ฅผ ๊ตฌํ•˜์‹œ์˜ค. --1๊ฑด
  • ๋ถ€์„œ๋ณ„๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์‹œ์˜ค
  • ์ตœ๋Œ€๊ธ‰์—ฌ๊ฐ€ 5000์ด์ƒ์ธ ๋ถ€์„œ๋งŒ ์ถœ๋ ฅํ•˜์‹œ์˜ค
--         department_id     max_salary
--         -----------------------------------
--           20                    13000 

ย 

select department_id, round(max(salary), 0)
from employees
where department_id in (10, 20)
group by department_id
having max(salary) >= 5000
order by department_id;
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-12-06 แ„‹แ…ฉแ„’แ…ฎ 5 57 58

ย 

ย 

๐Ÿ“– B. join

๐Ÿ“ ์‹œ์ž‘ํ•˜๊ธฐ์ „, join์ ˆ

    1. natural join(์ž์—ฐ์กฐ์ธ) : ๊ฐ™์€์ปฌ๋Ÿผ์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์„๋•Œ ๊ฐ™์€ ์ปฌ๋Ÿผ ๋ชจ๋‘๋ฅผ and์—ฐ์‚ฐํ•ด์„œ ์—ฐ๊ฒฐ
    1. Inner join(๋‚ด๋ถ€์กฐ์ธ) : ๊ฐ™์€๊ฒƒ ๋ผ๋ฆฌ๋งŒ ์—ฐ๊ฒฐ
    1. Outer join(์™ธ๋ถ€์กฐ์ธ) : ํ•œ์ชฝ์„ ๊ธฐ์ค€(๋ชจ๋‘ํฌํ•จ)ํ•ด์„œ ์—ฐ๊ฒฐ
    • left join : ์™ผ์ชฝ์ปฌ๋Ÿผ ๋ชจ๋‘ํฌํ•จ
    • right join : ์˜ค๋ฅธ์ชฝ์ปฌ๋Ÿผ ๋ชจ๋‘ํฌํ•จ
  • self join : ์ž๊ธฐ์ž์‹  ํ…Œ์ด๋ธ”๊ณผ ์—ฐ๊ฒฐ
  • cross join : ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ ์—ฐ๊ฒฐ
  • non equijoin : ๋ฒ”์œ„์— ์†ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ
  • n๊ฐœ ํ…Œ์ด๋ธ” ์กฐ์ธ : ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ…Œ์ด๋ธ” ์กฐ์ธ

๐Ÿ’ก ์ฐธ๊ณ 
1. MySQL๊ตฌ๋ฌธ
2. Ansiํ‘œ์ค€๊ตฌ๋ฌธ

ย 

โœ”๏ธ ์‚ฌ์›ํ…Œ์ด๋ธ”๊ณผ ๋ถ€์„œํ…Œ์ด๋ธ”์—์„œ ๋ถ€์„œ๊ฐ€ ๊ฐ™์„๊ฒฝ์šฐ ์‚ฌ์›๋ฒˆํ˜ธ,๋ถ€์„œ๋ฒˆํ˜ธ,๋ถ€์„œ์ด๋ฆ„์„ ์ถœ๋ ฅ

mysql ๊ตฌ๋ฌธ

select employee_id, e.department_id, department_name
from employees e, departments d
where e.department_id = d.department_id;

ย 

โœ”๏ธ ๋ถ€์„œํ…Œ์ด๋ธ”๊ณผ ์œ„์น˜ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ถ€์„œ๊ฐ€ ์œ„์น˜ํ•œ ๋„์‹œ๋ฅผ ์•Œ์•„๋‚ด์‹œ์˜ค(inner join)

select d.department_id, l.city 
from departments d, locations l
where d.location_id = l.location_id;

ย 

โœ”๏ธ ์‚ฌ์›ํ…Œ์ด๋ธ”๊ณผ ๋ถ€์„œํ…Œ์ด๋ธ”์—์„œ ๋ถ€์„œ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์€ ์‚ฌ์›์„ ์กฐ์ธํ•˜์‹œ์˜ค ==> 107๊ฑด

  • ์กฐ๊ฑด 1) ์‚ฌ์›์ด๋ฆ„, ๋ถ€์„œID, ๋ถ€์„œ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค
  • ์กฐ๊ฑด 2) ์‚ฌ์›ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์‚ฌ์›์„ ํฌํ•จํ•˜์‹œ์˜ค
select last_name, department_id, department_name
from employees 
left join departments using(department_id);

ย 

โœ”๏ธ ์‚ฌ์›ํ…Œ์ด๋ธ”๊ณผ ๋ถ€์„œํ…Œ์ด๋ธ”์—์„œ ๋ถ€์„œ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์€ ์‚ฌ์›์„ ์กฐ์ธํ•˜์‹œ์˜ค

  • ์กฐ๊ฑด 1) ์‚ฌ์›์ด๋ฆ„, ๋ถ€์„œID, ๋ถ€์„œ์ด๋ฆ„
  • ์กฐ๊ฑด 2) ๋ถ€์„œํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ถ€์„œ๋ฅผ ํฌํ•จ
select last_name, department_id, department_name
from employees
right join departments using(department_id);

ย 

โœ”๏ธ departments ์™€ locations ์ž์—ฐ์กฐ์ธ์˜ ๋น„๊ต(๊ฐ™์€์ปฌ๋Ÿผ : location_id), ๋‘๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•ด์„œ ๋ถ€์„œ์œ„์น˜(location_id), ๋„์‹œ(city), ๋ถ€์„œ์ด๋ฆ„(department_name)์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค

natural join

select location_id, city, department_name
from departments
natural join locations;

inner join

select location_id, city, department_name
from departments
inner join locations using(location_id);

ย 

โœ”๏ธ ๋ถ€์„œID์™€ ๋งค๋‹ˆ์ €ID๊ฐ€ ๊ฐ™์€ ์‚ฌ์›์„ ์—ฐ๊ฒฐํ•˜์‹œ์˜ค

  • last_name, department_id, manager_id
select last_name, department_id, manager_id
from employees e
inner join departments d using(department_id, manager_id);
select last_name, department_id, manager_id
from employees
natural join departments;

ย 

โœ”๏ธ ์‚ฌ์›๊ณผ ๊ด€๋ฆฌ์ž๋ฅผ ์—ฐ๊ฒฐํ•˜์‹œ์˜ค. ๋ชจ๋“  ์‚ฌ์›์„ ํ‘œ์‹œ

  • ์‚ฌ์›๋ฒˆํ˜ธ ์‚ฌ์›์ด๋ฆ„ ๊ด€๋ฆฌ์ž
  • 101 Kochhar King
select e.employee_id as "์‚ฌ์›๋ฒˆํ˜ธ", e.last_name as "์‚ฌ์›์ด๋ฆ„", m.last_name as "๊ด€๋ฆฌ์ž"
from employees e, employees m
where e.manager_id = m.manager_id

ย 

profile
"์•ผ, (์˜ค๋ฅ˜ ๋งŒ๋‚ฌ์–ด?) ๋„ˆ๋‘ (ํ•ด๊ฒฐ) ํ•  ์ˆ˜ ์žˆ์–ด"

0๊ฐœ์˜ ๋Œ“๊ธ€