๐Ÿ“‚๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค/๐Ÿ“ŠDBMS

DBMS ์˜ค๋ฆ„์ฐจ์ˆœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์กฐํšŒํ•˜๊ธฐ limit( ์ถœ๋ ฅ ๊ฐœ์ˆ˜ ์ œํ•œ) ์ค‘๋ณต ์ œ๊ฑฐ ํ‰๊ท (avg) ์„ธ๊ธฐ(count) ์กฐ๊ฑด์‹(having) - group by์—์„œ ์‚ฌ์šฉ

๐Ÿ‘ฉ‍๐ŸŽ“์ธํ…”๋ฆฌ๊ฐ์ž๐Ÿฅ” 2023. 5. 12. 17:49

DBMS ์กฐํšŒํ•˜๊ธฐ ์—ฐ์Šต๋ฌธ์ œ

-- ์—ฐ์Šต ๋ฌธ์ œ

-- 1. buy ํ…Œ์ด๋ธ”์—์„œ amount๊ฐ€ 4 ์ด์ƒ์ธ ๋ชจ๋“  ์—ด ์กฐํšŒํ•˜๊ธฐ

-- 2. buy ํ…Œ์ด๋ธ”์—์„œ prod_name์ด ์ง€๊ฐ‘ ๋˜๋Š” ์ฒญ๋ฐ”์ง€์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์—ด ๋ฐ์ดํ„ฐ ์กฐํšŒํ•˜๊ธฐ

 

select * from buy where amount >= 4;

select * from buy where prod_name in ('์ง€๊ฐ‘','์ฒญ๋ฐ”์ง€');

 

DBMS ์˜ค๋ฆ„์ฐจ์ˆœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์กฐํšŒํ•˜๊ธฐ

order by

-- ์ •๋ ฌ(๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์ˆœ์„œ๋ฅผ ์กฐ์ ˆ)

select mem_id, mem_name, debut_date from member order by debut_date; # ์˜ค๋ฆ„์ฐจ์ˆœ

-- ์ •๋ ฌ ์˜ต์…˜

-- ASC : ์˜ค๋ฆ„์ฐจ์ˆœ

-- DESC : ๋‚ด๋ฆผ์ฐจ์ˆœ

-- : ์ƒ๋žตํ•˜๋ฉด ASC๋กœ ๋ฐ›์•„๋“ค์ž„

 

select mem_id, mem_name, debut_date from member order by debut_date desc; # ๋‚ด๋ฆผ์ฐจ์ˆœ

 

-ํ‰๊ท ํ‚ค๊ฐ€ 164 ์ด์ƒ์ธ ํšŒ์›๋“ค(์กฐ๊ฑด)์„ ํ‚ค์˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒํ•  ๋•Œ

#์—๋Ÿฌ

select mem_id, mem_name, debut_date, height from member order by heigth desc where height >= 164; 

 

# ์ œ๋Œ€๋กœ ์ถœ๋ ฅ

select mem_id, mem_name, debut_date, height

from `member`

where height >= 164 #์—ฌ๊ธฐ์— from

order by height desc; 

 

 

์ •๋ ฌ ๊ธฐ์ค€์„ ์—ฌ๋Ÿฌ ์—ด๋กœ ์ง€์ •ํ•˜๊ธฐ

select mem_id, mem_name, debut_date, height

from member

where height >= 164

order by height desc, debut_date asc;

 

limit( ์ถœ๋ ฅ ๊ฐœ์ˆ˜ ์ œํ•œ)

select * from member limit 3;

 

*์‘์šฉ

-- ๋ฐ๋ท” ์ผ์ž๊ฐ€ ๊ฐ€์žฅ ๋น ๋ฅธ 3๊ฑด๋งŒ ์กฐํšŒ

select * from member order by debut_date asc limit 3;

 

 

์ค‘๋ณต ์ œ๊ฑฐ 

select addr from member;

select addr from member order by addr asc;

select distinct addr from member order by addr asc; #distinct๋Š” ๋ฌด๋ฆฌ๊ฐ€ ๋งŽ์ด ๊ฐ€๋Š” ๋ช…๋ น์–ด

 

๊ทธ๋ฃนํ™”(group by)

-- ๊ฐ ํšŒ์›์ด ๊ตฌ๋งคํ•œ ๋ฌผํ’ˆ์˜ ์ด ๊ฐœ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

select mem_id, amount from buy order by mem_id;

select mem_id, sum(amount) from buygroupbymem_id;

select mem_id "ํšŒ์› ์•„์ด๋””", sum(amount) "์ด ๊ตฌ๋งค ๊ฐœ์ˆ˜" from buy group by mem_id;

 

-- ๊ฐ ํšŒ์›์ด ๊ตฌ๋งคํ•œ ๊ธˆ์•ก์˜ ์ดํ•ฉ์„ ์•Œ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

select mem_id "ํšŒ์› ์•„์ด๋””", sum(price*amount) "์ด ๊ตฌ๋งค ๊ธˆ์•ก" from buy group by mem_id;

 

ํ‰๊ท (avg)

-- ํ•œ ๊ฑฐ๋ž˜ ๋‹น ๊ตฌ๋งคํ•˜๋Š” ๋ฌผํ’ˆ ์ˆ˜ ํ‰๊ท 

select mem_id, avg(amount) "ํ‰๊ท  ๊ตฌ๋งค ๊ฐœ์ˆ˜" from buy group by mem_id;

 

-- ์ „์ฒด ํšŒ์›์˜ ๊ตฌ๋งคํ•˜๋Š” ๋ฌผํ’ˆ ์ˆ˜ ํ‰๊ท 

select avg(amount) from buy;

 

์ „์ฒด ์ˆ˜ ์„ธ๊ธฐ(count)

-- ์ „์ฒด ํšŒ์› ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

select count(*) from member;

 

-- ์—ฐ๋ฝ์ฒ˜๊ฐ€ ์žˆ๋Š” ํšŒ์› ์ˆ˜๋งŒ ์•Œ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

select count(phone1) "์—ฐ๋ฝ์ฒ˜๊ฐ€ ์žˆ๋Š” ํšŒ์› ์ˆ˜ " from member;

 

์กฐ๊ฑด์‹(having) - group by์—์„œ ์‚ฌ์šฉ

-- ์œ„ ๋ฐ์ดํ„ฐ์—์„œ ์ด ๊ตฌ๋งค์•ก์ด 1000 ์ด์ƒ์ธ ํšŒ์›์—๊ฒŒ๋งŒ ์‚ฌ์€ํ’ˆ์„ ์ฆ์ •ํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด

#์—๋Ÿฌ --์ง‘๊ณ„ํ•จ์ˆ˜๋Š” where ์ ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ

select mem_id "ํšŒ์› ์•„์ด๋””", sum(price *amount) "์ด ๊ตฌ๋งค ๊ธˆ์•ก"

from buy

where sum(price * amount) >= 1000

group by mem_id;

 

#์ •์ƒ ์ถœ๋ ฅ --having

select mem_id "ํšŒ์› ์•„์ด๋””", sum(price * amount) "์ด ๊ตฌ๋งค ๊ธˆ์•ก"

from buy

group by mem_id

 

-- ์œ„ ๋ฐ์ดํ„ฐ์—์„œ ์ด ๊ตฌ๋งค์•ก์ด ํฐ ์‚ฌ์šฉ์ž๋ถ€ํ„ฐ ๋‚˜ํƒ€๋‚ด๋ ค๋ฉด

select mem_id "ํšŒ์› ์•„์ด๋””", sum(price * amount) "์ด ๊ตฌ๋งค ๊ธˆ์•ก"

from buy

group by mem_id

having sum(price * amount) >= 1000

order by sum(price * amount) desc;

 

์—ฐ์Šต๋ฌธ์ œ

-- 1. member ํ…Œ์ด๋ธ”์—์„œ ํšŒ์›๋“ค์„ height์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ

-- 2. member ํ…Œ์ด๋ธ”์˜ phone1์„ ์ค‘๋ณต์—†์ด ์กฐํšŒํ•˜๊ธฐ

select * from member order by height asc;

select distinct phone1 from member;

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ€์ ธ์˜ค๊ธฐ

๋„ˆ๋ฌด ์•„๋ฆ„ ๋‹ค์šด๋‹ค์šด๋‹ค์šด๋‹ค์šด view 

๋‹ค์Œ๋‹ค์Œ๋‹ค์Œ๋‹ค์Œ์„ ๋ˆŒ๋Ÿฌ์ค€๋‹ค....

 

 

์„ฑ๊ณต์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์™”๋‹ค.

 

์—ฐ์Šต๋ฌธ์ œ

-- 3. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋ชจ๋“  ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ animal_id ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” sql๋ฌธ ์ž‘์„ฑํ•˜๊ธฐ

-- ์กฐํšŒํ•  ์—ด : animal_id, animal_type, datetime, intake_condition, name, sex_upon_intake

 

select animal_id, animal_type, datetime, intake_condition, name, sex_upon_intake

from aac_intakes

order by animal_id asc;

 

-- 4. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ค‘ intake_condition์ด sick์ธ ๋™๋ฌผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ animal_id์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ

-- ์กฐํšŒํ•  ์—ด : animal_id, intake_condition, name

 

select animal_id, intake_condition, name

from aac_intakes

where intake_condition = "sick"

order by animal_id;

 

-- 5. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ค‘ intake_condition์ด Aged๊ฐ€ ์•„๋‹Œ ๋™๋ฌผ๋“ค์˜ ์ •๋ณด๋ฅผ animal_id ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ

-- ์กฐํšŒํ•  ์—ด : animal_id, intake_condition, name

 

select animal_id, intake_condition, name

from aac_intakes

where intake_condition != "Aged"

order by animal_id;

 

select animal_id, intake_condition, name

from aac_intakes

where intake_condition <> "Aged"

order by animal_id;

 

select animal_id, intake_condition, name

from aac_intakes

where not (intake_condition = "Aged")

order by animal_id;

 

-- 6. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋ชจ๋“  ๋™๋ฌผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋ฆ„์˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ

-- ๋‹จ, ์ด๋ฆ„์ด ๊ฐ™์€ ๋™๋ฌผ ์ค‘์—์„œ๋Š” ์ตœ๊ทผ์— ๋ณดํ˜ธ๋ฅผ ์‹œ์ž‘ํ•œ ๋™๋ฌผ์„ ๋จผ์ € ์กฐํšŒํ•˜๊ธฐ

-- ์กฐํšŒํ•  ์—ด : animal_id, datetime, name

 

select animal_id, datetime, name

from aac_intakes

order by name desc, datetime desc;

 

-- 7. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ๋ฐ์ดํ„ฐ ์กฐํšŒํ•˜๊ธฐ

-- ์กฐํšŒํ•  ์—ด : name, datetime

 

select name, datetime

from aac_intakes

order by datetime asc limit 1;

 

-- 8. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ด๋ฆ„์ด ์ด ๋ช‡ ์ข…๋ฅ˜ ์žˆ๋Š”์ง€ ์กฐํšŒํ•˜๊ธฐ

-- ์ด๋•Œ, ์ด๋ฆ„์ด ''์ธ ๊ฒฝ์šฐ๋Š” ์ง‘๊ณ„ํ•˜์ง€ ์•Š์Œ

select count(distinct name) from aac_intakes where name != "";

 

-- 9. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ข…(aniaml_type) ๋ณ„๋กœ ๊ฐ๊ฐ ๋ช‡ ๋งˆ๋ฆฌ์ธ์ง€ ์กฐํšŒํ•˜๊ธฐ

-- ์ด ๋•Œ, ๊ณ ์–‘์ด๊ฐ€ ๊ฐœ๋ณด๋‹ค ๋จผ์ € ๋“ฑ์žฅํ•˜๋„๋ก ์ •๋ ฌํ•˜๊ธฐ

-- ์กฐํšŒํ•  ์—ด : animal_type, cnt

 

select animal_type, count(*)"cnt"

from aac_intakes

group by animal_type

order by animal_type asc;

 

-- 10. ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ด๋ฆ„ ์ค‘ ๋‘ ๋ฒˆ ์ด์ƒ ์“ฐ์ธ ์ด๋ฆ„๊ณผ ํ•ด๋‹น ์ด๋ฆ„์ด ์“ฐ์ธ ํšŸ์ˆ˜๋ฅผ ์กฐํšŒํ•˜๊ธฐ.

-- ์ด ๋•Œ, ์ด๋ฆ„์ด ''์ธ ๋™๋ฌผ์€ ์ง‘๊ณ„์—์„œ ์ œ์™ธํ•˜๋ฉฐ, ์ด๋ฆ„์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ

-- ์กฐํšŒํ•  ์—ด: name, cnt

 

select name, count(*) cnt

from aac_intakes where name != '' and name not like '*%'

group by name #์ด๋ฆ„์œผ๋กœ ๋ฌถ์–ด์ค€๋‹ค!!!

having count(*) >= 2

order by name;