1) first
// 1. ํ
์ด๋ธ์์ ๋ชจ๋ ์ง์์ ์ด๋ฆ(name)๊ณผ ์ง๊ธ(position)์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์.
// ์ ๋ต
select name,
position
from sparta_employees;
// 2. ํ
์ด๋ธ์์ ์ค๋ณต ์์ด ๋ชจ๋ ์ง๊ธ(position)์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์.
// ์ ๋ต
select distinct position
from sparta_employees;
// 3. ํ
์ด๋ธ์์ ์ฐ๋ด(salary)์ด 40000๊ณผ 60000 ์ฌ์ด์ธ ์ง์๋ค์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์.
// ์ ๋ต
select *
from sparta_employees
where selary between 40000 and 60000;
// 4. ํ
์ด๋ธ์์ ์
์ฌ์ผ(hire_date)์ด 2023๋
1์ 1์ผ ์ด์ ์ธ ๋ชจ๋ ์ง์๋ค์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์.
// ์ ๋ต
select *
from sparta_employees
where hire_date < '2023-01-01';
2)
// 5. products ํ
์ด๋ธ์์ ์ ํ ์ด๋ฆ(product_name)๊ณผ ๊ฐ๊ฒฉ(price)๋ง์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์.
// ์ ๋ต
select produst_name, price
from products;
// 6. products ํ
์ด๋ธ์์ ์ ํ ์ด๋ฆ์ 'ํ๋ก'๊ฐ ํฌํจ๋ ๋ชจ๋ ์ ํ์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์.
// ์ ๋ต
select *
from products
where product_name like '%ํ๋ก%';
// 7. products ํ
์ด๋ธ์์ ์ ํ ์ด๋ฆ์ด '๊ฐค'๋ก ์์ํ๋ ๋ชจ๋ ์ ํ์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์.
// ์ ๋ต
select *
from products
where product_name like '๊ฐค%';
// 8. products ํ
์ด๋ธ์์ ๋ชจ๋ ์ ํ์ ๊ตฌ๋งคํ๊ธฐ ์ํด ํ์ํ ๋์ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์.
// ์ ๋ต
select sum(price) as sum_price --๋ณ์นญ ์ฌ์ฉํด์ ๊ฒฐ๊ณผ ๊น๋ํ๊ฒ ์ ๋ฆฌํ๊ธฐ
from products;
3)
// 9. orders ํ
์ด๋ธ์์ ์ฃผ๋ฌธ ์๋(amount)์ด 2๊ฐ ์ด์์ธ ์ฃผ๋ฌธ์ ์งํํ ์๋น์์ ID(customer_id)๋ง ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
// ์ ๋ต
select customer_id
from orders
where amount >= 2;
// 10. orders ํ
์ด๋ธ์์ 2023๋
11์ 2์ผ ์ดํ์ ์ฃผ๋ฌธ๋ ์ฃผ๋ฌธ ์๋(amount)์ด 2๊ฐ ์ด์์ธ ์ฃผ๋ฌธ์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
// ์ ๋ต
select *
from orders
where order_date > '2023-11-02' and amount >= 2;
// 11. orders ํ
์ด๋ธ์์ ์ฃผ๋ฌธ ์๋์ด 3๊ฐ ๋ฏธ๋ง์ด๋ฉด์ ๋ฐฐ์ก๋น(shipping_fee)๊ฐ 15000์๋ณด๋ค ๋น์ผ ์ฃผ๋ฌธ์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
// ์ ๋ต
select *
from orders
where amount<3 and shipping_fee>15000;
// 12. orders ํ
์ด๋ธ์์ ๋ฐฐ์ก๋น๊ฐ ๋์ ๊ธ์ก ์์ผ๋ก ์ ๋ ฌํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
// ์ ๋ต
select *
from orders
order by shipping_fee desc;
4)
// 13. sparta_students ํ
์ด๋ธ์์ ๋ชจ๋ ํ์์ ์ด๋ฆ(name)๊ณผ ํธ๋(track)์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
// ์ ๋ต
select name, track
from sparta_students;
// 14. sparta_students ํ
์ด๋ธ์์ Unity ํธ๋ ์์์ด ์๋ ํ์๋ค์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
//๐์ค๋ต
select *
from sparta_students
where track <> 'Unity'; --๋ฌธ์์ด์ ''
// 15. sparta_students ํ
์ด๋ธ์์ ์
ํ๋
๋(enrollment_year)๊ฐ 2021๋
์ธ ํ์๊ณผ 2023๋
์ธ ํ์์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
// ์ ๋ต
select *
from sparta_students
where enrollment_year in (2021, 2023);
// 16. sparta_students ํ
์ด๋ธ์์ Node.js ํธ๋ ์์์ด๊ณ ํ์ ์ด ‘A’์ธ ํ์์ ์
ํ๋
๋๋ฅผ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
//๐์ค๋ต
select enrollment_year
from sparta_students
where track='Node.js' and grade='A'; --๋ฌธ์์ด์ ''
<์ค๋ต๋
ธํธ>
14, 16. ๋ฌธ์์ด ์ฌ์ฉ ์ ์์๋ฐ์ดํ('') ์ฌ์ฉํ๊ธฐ
5)
//17.team_projects ํ
์ด๋ธ์์ AWS ์์ฐ(aws_cost)์ด 40000 ์ด์ ๋ค์ด๊ฐ ํ๋ก์ ํธ๋ค์ ์ด๋ฆ์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
// ์ ๋ต
select name
from team_projects
where aws_cost >= 40000;
//18.team_projects ํ
์ด๋ธ์์ 2022๋
์ ์์๋ ํ๋ก์ ํธ๋ฅผ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์! ๋จ, start_date < ‘2023-01-01’ ์กฐ๊ฑด์ ์ฌ์ฉํ์ง ๋ง๊ณ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
//๐์ค๋ต
select *
from team_projects
where int(date(start_date, '%y')) = 2022;
//๐กํด์ค
select *
from team_projects
where year(start_date) = 2022; --year() ๊ฒฐ๊ณผ ํ์
: date(์ซ์๋ก ์ธ์)
//19.team_projects ํ
์ด๋ธ์์ ํ์ฌ ์งํ์ค์ธ ํ๋ก์ ํธ๋ฅผ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์. ๋จ, ์ง๊ธ ์์ ์ ๋ ์ง๋ฅผ ํ๋์ฝ๋ฉํด์ ์ฟผ๋ฆฌํ์ง ๋ง์์ฃผ์ธ์!
//๐์ค๋ต
//๐กํด์ค
select *
from team_projects
where curdate() between start_date and end_date; --curdate(): ํ์ฌ ๋ ์ง ๋ฐํ
//20.team_projects ํ
์ด๋ธ์์ ๊ฐ ํ๋ก์ ํธ์ ์ง์ ๊ธฐ๊ฐ์ ์ผ ์๋ก ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
//๐์ค๋ต
select date((int(end_date)-int(start_date)) + 1), '%d') as "ํ๋ก์ ํธ ์ง์ ๊ธฐ๊ฐ"
from team_projects
//๐กํด์ค
select name,
datediff(day, start_date, end_date) as working_days --๋ ์ง(์ผ) ์ฐจ์ด ๊ณ์ฐ ํจ์
from team_projects;
<์ค๋ต๋
ธํธ>
18. date_format('๋ ์ง๊ฐ(๋ฌธ์์ด)' or ์ปฌ๋ผ, '๋ ์ง ์ถ๋ ฅ ํฌ๋งท(๋ฌธ์์ด)') [๊ฒฐ๊ณผ ํ์
: ๋ฌธ์์ด]
*date(): ๋ ์ง/์๊ฐ ๊ฐ์์ ๋ ์ง ๋ฐ์ดํฐ๋ง ์ถ์ถ [๊ฒฐ๊ณผ ํ์
: date]
year('๋ ์ง๊ฐ(๋ฌธ์์ด)' or ์ปฌ๋ผ) [๊ฒฐ๊ณผํ์
: INT]
19. curdate(): ํ์ฌ ๋ ์ง ์ถ๋ ฅ [๊ฒฐ๊ณผ ํ์
: DATE]
now(): ํ์ฌ ๋ ์ง/์๊ฐ ์ถ๋ ฅ [๊ฒฐ๊ณผ ํ์
: DATETIME]
20. datediff(datepart, start date, end date) [๊ฒฐ๊ณผ ํ์
: INT]
: dateapart์ ๋ฐ๋ฅธ ์ผ์ ์ฐจ์ด๋ฅผ ๋ฐํ
6)
//21.lol_users ํ
์ด๋ธ์์ ๊ฐ ์ ์ ์ ๋ ์ดํ
(rating) ์์๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์! ์ ์ฒด ์ง์ญ(region) ๊ธฐ์ค์ด๊ณ ์์๋ ๋ ์ดํ
์ด ๋์์๋ก ๋์์ผํด์. (e.g. rating 1400 ์ ์ ์ ์์ > rating 1350 ์ ์ ์ ์์)
//๐์ค๋ต
select *
from lol_users
order by region asc, rating desc
//๐กํด์ค
select id,
name,
region,
rating,
rank() over(order by rating desc) as lol_ranking --rank() over(partition by(์ ํ) order by(ํ์))
from lol_users;
//22.lol_users ํ
์ด๋ธ์์ ๊ฐ์ฅ ๋ฆ๊ฒ ๊ฒ์์ ์์ํ(join_date) ์ ์ ์ ์ด๋ฆ์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
//์ ๋ต
select name,
join_date
from lol_users
where join_date is not null
order by join_date desc
limit 1;
//23.lol_users ํ
์ด๋ธ์์ ์ง์ญ๋ณ๋ก ๋ ์ดํ
์ด ๋์ ์์ผ๋ก ์ ์ ๋ค์ ์ ๋ ฌํด์ ๋์ดํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
//์ ๋ต
select *
from lol_users
order by region, rating desc;
//24.lol_users ํ
์ด๋ธ์์ ์ง์ญ๋ณ๋ก ํ๊ท ๋ ์ดํ
์ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
//์ ๋ต
select region,
avg(rating) as avg_rating
from lol_users
group by region;
<์ค๋ต๋
ธํธ>
21. ๋ญํน ์ง๊ณ ํจ์
rank() over(partition by ์์๋ฅผ ๋๋ ์ ๋งค๊ธธ ๊ธฐ์ค(์ ํ) order by ์์ ๊ธฐ์ค(ํ์))
- ๋์ ํ์ฉ / ๊ฐ์ ์์๋ก ํ๋จ
- partition by : ํด๋น ๊ธฐ์ค๋ง๋ค ์์ ๋ฆฌ์
(ex.๊ฐ ๊ตญ๊ฐ ๋ด์์ ๊ฒฝ์ํ ๋)
7)
//25. lol_feedbacks ํ
์ด๋ธ์์ ๋ง์กฑ๋ ์ ์(satisfaction_score)์ ๋ฐ๋ผ ํผ๋๋ฐฑ์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select id, user_name, satisfaction_score, feedback_date
from lol_feedbacks
order by satisfaction_score desc;
//26.lol_feedbacks ํ
์ด๋ธ์์ ๊ฐ ์ ์ ๋ณ๋ก ์ต์ ํผ๋๋ฐฑ์ ์ฐพ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select user_name,
max(feedback_date) as latest_feedback_date
from lol_feedbacks
group by user_name;
//27.lol_feedbacks ํ
์ด๋ธ์์ ๋ง์กฑ๋ ์ ์๊ฐ 5์ ์ธ ํผ๋๋ฐฑ์ ์๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select count(*) as feedback_cnt
from lol_feedbacks
where satisfaction_score=5;
//28.lol_feedbacks ํ
์ด๋ธ์์ ๊ฐ์ฅ ๋ง์ ํผ๋๋ฐฑ์ ๋จ๊ธด ์์ 3๋ช
์ ๊ณ ๊ฐ์ ์ฐพ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--๐์ค๋ต
select user_name,
count(user_name) as cnt_fb
from lol_feedbacks
order by count(user_name) desc
limit 3;
--๐กํด์ค
select user_name,
count(user_name) as cnt_fb
from lol_feedbacks
group by user_name --๋ถ๋ฅ ๊ธฐ์ค ๊ผญ ์ ๊ธฐ
order by count(user_name) desc --order by cnt_fb๊ณผ ๊ฐ์ด ๋ณ์นญ ์ฌ์ฉ ๊ฐ๋ฅ
--order by๊ฐ select ๋ณด๋ค ๋ค์ ์คํ๋๊ธฐ ๋๋ฌธ์ select์ ๋ณ์นญ์ ๋ถ๋ฌ์ฌ ์ ์์.
limit 3;
//29.lol_feedbacks ํ
์ด๋ธ์์ ํ๊ท ๋ง์กฑ๋ ์ ์๊ฐ ๊ฐ์ฅ ๋์ ๋ ์ง๋ฅผ ์ฐพ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select feedback_date,
avg(satisfaction_score) as avg_scor
from lol_feedbacks
group by feedback_date
order by avg_score desc
limit 1;
8)
// 30. doctors ํ
์ด๋ธ์์ ์ ๊ณต(major)๊ฐ ์ฑํ์ธ๊ณผ์ธ ์์ฌ์ ์ด๋ฆ์ ์์๋ด๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select name, major
from doctors
where major='์ฑํ์ธ๊ณผ';
// 31. doctors ํ
์ด๋ธ์์ ๊ฐ ์ ๊ณต ๋ณ ์์ฌ ์๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select major,
count(name) --null๊ฐ ์์ผ๋ count(*) ์ฌ์ฉ ๊ฐ๋ฅ
from doctors
group by major;
// 32. doctors ํ
์ด๋ธ์์ ํ์ฌ ๋ ์ง ๊ธฐ์ค์ผ๋ก 5๋
์ด์ ๊ทผ๋ฌด(hire_date)ํ ์์ฌ ์๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--๐์ค๋ต
select count(*)
from doctors
where year(curdate())-year(hire_date)>=5; --์ ํํ 5๋
์ ๊ณ์ฐํ๋ ๊ฒ ์๋๋ผ ๋
๋๋ฅผ ๊ธฐ์ค์ผ๋ก '5๋
์ฐจ'๋ฅผ ๊ณ์ฐํ๋ ํจ์. ์ ํ๋ ๋จ์ด์ง.
--๐กํด์ค
select count(*)
from doctors
where date_sub(curdate(), interval 5 year) >= hire_date --DATE_SUB(๊ธฐ์ค๋ ์ง, ํน์ ์๊ฐ ๊ฐ๊ฒฉ)
// 33.doctors ํ
์ด๋ธ์์ ๊ฐ ์์ฌ์ ๊ทผ๋ฌด ๊ธฐ๊ฐ์ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select name,
hire_date,
datediff(curdate(), hire_date) as working_dates --datediff(๋๋๋ ๋ ์ง, ์์ ๋ ์ง): ๋ ๋ ์ง์ ์ฐจ์ด๋ฅผ ๊ตฌํ๋ ํจ์
from doctors;
<ํจ์ ์ค๋ต๋
ธํธ>
32. year() ํจ์์ ์ฐจ์ด๋ '์ฐ์ฐจ'๋ฅผ ๊ณ์ฐํ๋ ํจ์๋ก, ์ ํํ ์๊ฐ์ ๊ณ์ฐํ ์ ์๋ค.
DATE_SUB(๊ธฐ์ค ๋ ์ง, INTERVAL ์๊ฐ ๊ฐ๊ฒฉ) : '๊ธฐ์ค ๋ ์ง'์์ '์๊ฐ ๊ฐ๊ฒฉ'๋งํผ ์ด์ (-)
DATE_ADD(๊ธฐ์ค ๋ ์ง, INTERVAL ์๊ฐ ๊ฐ๊ฒฉ) : '๊ธฐ์ค ๋ ์ง'์์ '์๊ฐ ๊ฐ๊ฒฉ'๋งํผ ์ดํ (+)
33. ๋ ๋ ์ง์ ์ฐจ์ด ๊ตฌํ๊ธฐ
DATEDIFF(๋ ๋ ์ง, ์์ ๋ ์ง)
9)
//34. patients ํ
์ด๋ธ์์ ๊ฐ ์ฑ๋ณ(gender)์ ๋ฐ๋ฅธ ํ์ ์๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select gender,
count(gender) as gender_cnt --count(*)์ฌ์ฉ ๊ฐ๋ฅ
from patients
group by gender;
//35. patients ํ
์ด๋ธ์์ ํ์ฌ ๋์ด๊ฐ 40์ธ ์ด์์ธ ํ์๋ค์ ์๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--๐์ค๋ต
select count(*) as "age_>=40"
from patients
where year(curdate())-year(birth_date)+1 >= 40
--๐กํด์ค
select count(*)
from patients
where birth_date <= date_sub(curdate(), interval 40 year);
//36. patients ํ
์ด๋ธ์์ ๋ง์ง๋ง ๋ฐฉ๋ฌธ ๋ ์ง(last_visit_date)๊ฐ 1๋
์ด์ ๋ ํ์๋ค์ ์ ํํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select *
from patients
where last_visit_date <= date_sub(curdate(), interval 1 year);
//37. patients ํ
์ด๋ธ์์ ์๋
์์ผ์ด 1980๋
๋์ธ ํ์๋ค์ ์๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select count(*)
from patients
where year(birth_date) between 1980 and 1989;
--๐ก๊ถ์ฅ
select count(*)
from patients
where birth_date between '1980-01-01' and '1989-12-31';
<์ค๋ต๋
ธํธ>
35. ์์ผ ๊ฐ์ ๊ตฌํ ๋ year() ํจ์๋ก ์ฐ๋๋ง ๊ณ์ฐํ๋ฉด ํ๊ตญ๋์ด๋ง ๊ณ์ฐ ๊ฐ๋ฅ.
๊ฐ๋ฅํ๋ฉด ๋ ์ง ๊ณ์ฐ์ date_sub() / date_add() ํจ์ ์ด์ฉํ๊ธฐ
37. ์ฐ๋๊ณ์ฐ์ year() ์ฌ์ฉ ๊ฐ๋ฅ
๋จ, ๊ทธ๋ฌ๋ฉด ์ปฌ๋ผ์ ๋ชจ๋ date ๊ฐ์ year๋ก ์ฌ๋ฐํํด์ผ ํจ. ๋นํจ์จ์ . date ํฌ๋งท ๊ทธ๋๋ก ์ฌ์ฉํ๋๋ก.
10)
//38. ํ์ฌ ์กด์ฌํ๊ณ ์๋ ์ด ๋ถ์์ ์๋ฅผ ๊ตฌํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select count(*) as cnt_departments
from departments;
//39. ๋ชจ๋ ์ง์๊ณผ ๊ทธ๋ค์ด ์ํ ๋ถ์์ ์ด๋ฆ์ ๋์ดํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select e.name,
d.name --join์ผ๋ก ์ฐ๊ฒฐํ ํ
์ด๋ธ์ด ๊ผญ select์ ํฌํจ๋์ง ์์๋ ๋จ.
FROM employees AS e INNER JOIN departments AS d ON e.department_id=d.id;
//40. '๊ธฐ์ ํ' ๋ถ์์ ์ํ ์ง์๋ค์ ์ด๋ฆ์ ๋์ดํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select e.name
from employees e inner join departments d on e.department_id=d.id
where d.name='๊ธฐ์ ํ';
//41. ๋ถ์๋ณ๋ก ์ง์ ์๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--๐์ค๋ต
select d.name as department,
count(*)
from employees e inner join departments d on e.department_id=d_id --inner join:์ง์์ด ์๋ ๋ถ์๋ง ๋ณด์ฌ์ค๋ค.(์ค๋ต/๋ชจ๋ ๋ถ์๋ฅผ ๋ณด์ฌ์ค์ผ ํจ.)
group by d.name;
--๐กํด์ค
select e.name as department,
count(e.id) --count ํ ๋ null์ผ ๊ฐ๋ฅ์ฑ(๋ณ๋๋ ๊ฐ๋ฅ์ฑ)์ด ์ ์ ์ปฌ๋ผ์ ์ ๋ ๊ฒ ์์ ํจ.
from departments d --๊ธฐ์ค ํ
์ด๋ธ๋ก ์ค์ (๋ฌธ์ ๊ฐ '๋ถ์๋ณ๋ก ์ง์ ์~)
left join employees e on d.id=e.department_id --๋ถ์์ ์ง์์ด 0๋ช
์ธ ๋ถ์๋ ํฌํจ์ด์ด์ 'left join'์ฌ์ฉ.
group by d.name;
//42. ์ง์์ด ์๋ ๋ถ์์ ์ด๋ฆ์ ์ฐพ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select d.name as department
count(e.id) as cnt_employee
from departments d
left join employees e
on d.id=e.department_id
group by d.name
having count(e.id)=0;
--๐ก๊ถ์ฅ
SELECT d.name
FROM departments d LEFT JOIN employees e ON d.id = e.department_id
WHERE e.id IS NULL; --๋จ์ null๊ฐ์ ์กฐํํ ๋๋ where๋ก ๊ฐ๋จํ๊ฒ.
//43. '๋ง์ผํ
ํ' ๋ถ์์๋ง ์ํ ์ง์๋ค์ ์ด๋ฆ์ ๋์ดํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select e.name
from employees e
inner join departments d
on e.department_id=d.id
where d.name='๋ง์ผํ
ํ';
<์ค๋ต๋
ธํธ>
41. ๋ฌธ์ ์ ๋ฐ๋ผ ๊ธฐ์คํ
์ด๋ธ๋ก ์ค์ ํ๋ ๊ฒ ๋ฌ๋ผ์ง๋ค.
ํผ ๊ฒ์ฒ๋ผ employees ํ
์ด๋ธ์ ๊ธฐ์คํ
์ด๋ธ๋ก ์ค์ ํ๋ฉด, ์ง์์ด ์๋ ๋ถ์๋ ์กฐํ๋์ง ์๋๋ค.
'๋ถ์๋ณ๋ก ์ง์ ์'๋ฅผ ๊ตฌํด์ผ ํ๊ธฐ ๋๋ฌธ์ (1)๋ชจ๋ ๋ถ์๊ฐ ํ์๋์ด์ผ ํ๋ฉฐ (2)๋ง์ฝ ์ง์์ด ์์ผ๋ฉด ๊ทธ ๋ถ์์ ์ง์์๋ 0์ผ๋ก ํ์.
42. ๋จ์ null๊ฐ์ ์กฐํํ๋ฉด ๋ ๋๋ where๋ก ๊ฐ๋ฒผ์ด ์ฟผ๋ฆฌ๋ฌธ ์์ฑ.
**LEFT JOIN์์ NULL ์กฐํ**
LEFT JOIN์์ ๋งค์นญ๋์ง ์์ ํ์ ์ฐพ๊ธฐ ์ํด์๋ ๊ธฐ์ค ํ
์ด๋ธ์ด ์๋ ์ค๋ฅธ์ชฝ ํ
์ด๋ธ์ ์ปฌ๋ผ์ ๊ฒ์ฌํด์ผ ํ๋ฉฐ,
๊ทธ์ค์์๋ NULL์ด ๋ ๊ฐ๋ฅ์ฑ์ด ์๋ ๊ณ ์ ์๋ณ์(PK)๋ฅผ ๊ธฐ์ค์ผ๋ก NULL ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ๊ฒ์ด ๊ฐ์ฅ ์์ ํ๋ค.
11) last
//44. ๋ชจ๋ ์ฃผ๋ฌธ์ ์ฃผ๋ฌธ ID์ ์ฃผ๋ฌธ๋ ์ํ์ ์ด๋ฆ์ ๋์ดํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select o.id, p.name
from orders o inner join products p on o.product_id=p.id;
//45. ์ด ๋งค์ถ(price * quantity์ ํฉ)์ด ๊ฐ์ฅ ๋์ ์ํ์ ID์ ํด๋น ์ํ์ ์ด ๋งค์ถ์ ๊ฐ์ ธ์ค๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select p.id,
sum(p.price * o.quantity) as sum_price
from products p
inner join orders o
on p.id=o.product.id
group by p.id
order by sum(p.price * o.quantity) desc
limit 1;
//46. ๊ฐ ์ํ ID๋ณ๋ก ํ๋งค๋ ์ด ์๋(quantity)์ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select p.id,
sum(o.quantity) as sum_qty --์ด๋ค ํ
์ด๋ธ์์ ๊ฐ์ ธ์จ ์ปฌ๋ผ์ธ์ง ๊ผญ ํ๊ธฐ!!
from products p
left join orders o
on p.id=o.product_id
group by p.id;
//47. 2023๋
3์ 3์ผ ์ดํ์ ์ฃผ๋ฌธ๋ ๋ชจ๋ ์ํ์ ์ด๋ฆ์ ๋์ดํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
-์ ๋ต
select p.name
from products p inner join orders o on p.id=o.product_id
where o.order_date > '2023-03-03';
//48. ๊ฐ์ฅ ๋ง์ด ํ๋งค๋ ์ํ์ ์ด๋ฆ์ ์ฐพ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select p.name,
sum(o.quantity) as total_qty
from products p inner join orders o on p.id=o.product_id
group by p.name
order by sum(quantity) desc
limit 1;
//49. ๊ฐ ์ํ ID๋ณ๋ก ํ๊ท ์ฃผ๋ฌธ ์๋์ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต
select p.id,
avg(o.quantity) as avg_qty
from products p inner join orders o on p.id=o.product_id
group by p.id;
//50. ํ๋งค๋์ง ์์ ์ํ์ ID์ ์ด๋ฆ์ ์ฐพ๋ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ฃผ์ธ์!
--์ ๋ต.
select p.id, p.name
from products p left join orders o on p.id=o.product_id
where o.quantity is null; --o.quantity๋ ๋จ์ ๊ธฐ์
๋๋ฝ์ผ ์ ์์ด์ ์ค๋ฌด์์๋ ์ค๋ฅ ๋ฐ์ ๊ฐ๋ฅ์ฑ ์์.
--์ฃผ๋ฌธ ์์ฒด์ ๊ณ ์ ๊ฐ์ธ o.id๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ์์ ํจ.
<์ค๋ต๋
ธํธ>
- select, group by, count() ๋ฑ ํด๋น ์ปฌ๋ผ์ ๊ณ ์ ๊ฐ์ผ๋ก ๋ณ๋ ๊ฐ๋ฅ์ฑ์ด ์๋ ์ปฌ๋ผ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์์ ํจ.
- group by์์ A์ปฌ๋ผ์ ์ฌ์ฉ, ์ปฌ๋ผA์ ์ปฌ๋ผB๊ฐ 'ํจ์ ์ข
์์ฑ(1:1 ๋งค์นญ)'์ด ์๋ค๋ฉด select์ ์ปฌ๋ผB๋ฅผ ์ฌ์ฉํด๋ ๋จ.(๊ณ ์ ๊ฐ์ธ ์ปฌ๋ผ์ผ๋ก ์ฌ์ฉ)
์ค๋ต๋
ธํธ ์ดํฉ
- ๋ฌธ์์ด ์ฌ์ฉ ์ ์์๋ฐ์ดํ('') ์ฌ์ฉ
- select, group by, count() ๋ฑ ํด๋น ์ปฌ๋ผ์ ๊ณ ์ ๊ฐ์ผ๋ก ๋ณ๋ ๊ฐ๋ฅ์ฑ์ด ์๋ ์ปฌ๋ผ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์์ ํจ.
- group by์์ A์ปฌ๋ผ์ ์ฌ์ฉ, ์ปฌ๋ผA์ ์ปฌ๋ผB๊ฐ 'ํจ์ ์ข
์์ฑ(1:1 ๋งค์นญ)'์ด ์๋ค๋ฉด select์ ์ปฌ๋ผB๋ฅผ ์ฌ์ฉํด๋ ๋จ.
(๊ณ ์ ๊ฐ์ธ ์ปฌ๋ผ์ผ๋ก ์ฌ์ฉ)
# ๋ ์ง ๊ด๋ จ ํจ์
- DATE_FORMAT('๋ ์ง๊ฐ(๋ฌธ์์ด)' or ์ปฌ๋ผ, '๋ ์ง ์ถ๋ ฅ ํฌ๋งท(๋ฌธ์์ด)')
: ๋ ์ง(Date/Datetime)๋ฅผ ์ฌ๋์ด ์ํ๋ “๋ฌธ์์ด ํ์”์ผ๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์
[Data Type: Str/Varchar]
| ํฌ๋งท |
์๋ฏธ |
์์ |
| %Y |
์ฐ๋(4์๋ฆฌ) |
2025 |
| %y |
์ฐ๋(2์๋ฆฌ) |
25 |
| %m |
์(01~12) |
12 |
| %d |
์ผ(01~31) |
27 |
| %Y-%m |
์ฐ-์ |
2025-12 |
| %Y-%m-%d |
์ฐ-์-์ผ |
2025-12-27 |
| %H:%i:%s |
์:๋ถ:์ด |
14:30:00 |
- DATE('๋ ์ง๊ฐ ์๊ฐ๊ฐ' or ์ปฌ๋ผ)
: ๋ ์ง/์๊ฐ ๊ฐ์์ ๋ ์ง ๋ฐ์ดํฐ๋ง ์ถ์ถ
[Data Type: DATE]
- YEAR('๋ ์ง๊ฐ(๋ฌธ์์ด)' or ์ปฌ๋ผ)
: ๋ ์ง(Date / Datetime)์์ ‘์ฐ๋(๋
๋)’๋ง ์ถ์ถํ๋ ํจ์
[Data Type: INT]
- CURDATE()
: ํ์ฌ ๋ ์ง ์ถ๋ ฅ
[Data Type: DATE]
- NOW()
: ํ์ฌ ๋ ์ง/์๊ฐ ์ถ๋ ฅ
[Data Type: DATETIME]
- DATEDIFF(date1, date2)
: ๋ ๋ ์ง ์ฌ์ด์ ์ฐจ์ด๋ฅผ ‘์ผ(day)’ ๋จ์๋ก ๊ณ์ฐํด์ ๋ฐํํ๋ ํจ์
๊ณ์ฐ: date1 - date2
**๋จ์ ์ผ์ฐจ ๊ณ์ฐ / ์ ํํ ๋ ์ง ๊ธฐ์ค์ ๊ณ์ฐ์ด ํ์ํ ๋๋ 'DATE_SUB() ํจ์ ์ฌ์ฉ'
[Data Type: INT]
- DATE_SUB(๊ธฐ์ค ๋ ์ง, INTERVAL ์๊ฐ ๊ฐ๊ฒฉ)
: '๊ธฐ์ค ๋ ์ง'์์ '์๊ฐ ๊ฐ๊ฒฉ'๋งํผ ์ด์ (-)
** ์ฐ์ฐจ / ๊ทผ์ / ๋์ด ๊ณ์ฐ ๋ฑ ์ ํํ ์ผ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ณ์ฐํ ๋๋ ์ด ํจ์ ์ฌ์ฉ.
[Data Type: DATE]
- DATE_ADD(๊ธฐ์ค ๋ ์ง, INTERVAL ์๊ฐ ๊ฐ๊ฒฉ)
: '๊ธฐ์ค ๋ ์ง'์์ '์๊ฐ ๊ฐ๊ฒฉ'๋งํผ ์ดํ (+)
** ์ฐ์ฐจ / ๊ทผ์ / ๋์ด ๊ณ์ฐ ๋ฑ ์ ํํ ์ผ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ณ์ฐํ ๋๋ ์ด ํจ์ ์ฌ์ฉ.
[Data Type: DATE]
# ๋ญํน ์ง๊ณ ํจ์
- rank() over(partition by ์์๋ฅผ ๋๋ ์ ๋งค๊ธธ ๊ธฐ์ค(์ ํ) order by ์์ ๊ธฐ์ค [ASC | DESC](ํ์))
:RANK()๋ ์ ๋ ฌ ๊ธฐ์ค์ ๋ฐ๋ผ ์์๋ฅผ ๋งค๊ธฐ๋, ๊ฐ์ ๊ฐ์ ๊ฐ์ ์์๋ฅผ ๋ถ์ฌํ๊ณ ๋ค์ ์์๋ ๊ฑด๋๋ด๋ค.
- partition by : ํด๋น ๊ธฐ์ค๋ง๋ค ์์ ๋ฆฌ์
(ex.๊ฐ ๊ตญ๊ฐ ๋ด์์ ๊ฒฝ์ํ ๋)
- DENSE_RANK()
: ๋์ฒจํ์ฉ, ๋ค์์์ ์ ๊ฑด๋๋.
- ROW_NUMBER()
: ๋์ ๋ฌด์, ๋ฌด์กฐ๊ฑด 1์ฉ ์ฆ๊ฐ
# JOIN
- ๋ฌธ์ ์ ๋ฐ๋ผ ๊ธฐ์คํ
์ด๋ธ๋ก ์ค์ ํ๋ ๊ฒ ๋ฌ๋ผ์ง๋ค.
ํผ ๊ฒ์ฒ๋ผ employees ํ
์ด๋ธ์ ๊ธฐ์คํ
์ด๋ธ๋ก ์ค์ ํ๋ฉด, ์ง์์ด ์๋ ๋ถ์๋ ์กฐํ๋์ง ์๋๋ค.
'๋ถ์๋ณ๋ก ์ง์ ์'๋ฅผ ๊ตฌํด์ผ ํ๊ธฐ ๋๋ฌธ์ (1)๋ชจ๋ ๋ถ์๊ฐ ํ์๋์ด์ผ ํ๋ฉฐ (2)๋ง์ฝ ์ง์์ด ์์ผ๋ฉด ๊ทธ ๋ถ์์ ์ง์์๋ 0์ผ๋ก ํ์.
- ๋จ์ null๊ฐ์ ์กฐํํ๋ฉด ๋ ๋๋ where๋ก ๊ฐ๋ฒผ์ด ์ฟผ๋ฆฌ๋ฌธ ์์ฑ.
**LEFT JOIN์์ NULL ์กฐํ**
LEFT JOIN์์ ๋งค์นญ๋์ง ์์ ํ์ ์ฐพ๊ธฐ ์ํด์๋ ๊ธฐ์ค ํ
์ด๋ธ์ด ์๋ ์ค๋ฅธ์ชฝ ํ
์ด๋ธ์ ์ปฌ๋ผ์ ๊ฒ์ฌํด์ผ ํ๋ฉฐ,
๊ทธ์ค์์๋ NULL์ด ๋ ๊ฐ๋ฅ์ฑ์ด ์๋ ๊ณ ์ ์๋ณ์(PK)๋ฅผ ๊ธฐ์ค์ผ๋ก NULL ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ๊ฒ์ด ๊ฐ์ฅ ์์ ํ๋ค.