λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Data Base/MySQL

MySQL 19λ²ˆμ§Έμˆ˜μ—…

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 8.
λ°˜μ‘ν˜•

β–Ά ν†΅κ³„ν•¨μˆ˜

​

1. λͺ¨λ“  μ—΄μ˜ 선택(*)​

mysql> select * from circle;​

​

​

 

2. SELECT절의 μˆ˜μ‹

μ§€κΈˆκΉŒμ§€ SELECT절의 λŒ€λΆ€λΆ„μ˜ μ˜ˆμ œλŠ” μ—΄μ˜ μ΄λ¦„λ§Œμ„ μ§€μ •ν–ˆμ§€λ§Œ μˆ˜μ‹μ—λŠ” λ¦¬ν„°λŸ΄, 계산 λ˜λŠ” 슀칼라 ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  μˆ˜λ„ μžˆλ‹€.

​

​

예제 10-1) 2019년에 λ“±λ‘ν•œ 학생에 λŒ€ν•œ ν•™λ²ˆ, 년도, ν•™κΈ°, μž₯ν•™κΈˆμ•‘, 납뢀총앑(λ“±λ‘κΈˆ-μž₯ν•™κΈˆ), λ‚©λΆ€κΈˆλΉ„μœ¨(납뢀총앑/λ“±λ‘κΈˆ*100), '%'을 좜λ ₯ν•˜μ‹œμ˜€.

A)

mysql> select stu_no, fee_year, fee_term, fee_total, jang_total,

-> fee_pay "납뢀총앑", fee_pay/fee_total*100 "λ‚©λΆ€κΈˆλΉ„μœ¨", '%'

-> from fee

-> where fee_year = 2019;

​

​

​

3. DISTINCTλ₯Ό μ‚¬μš©ν•œ μ€‘λ³΅λœ ν–‰ 제거

DISTINCTλ₯Ό μ‚¬μš©ν•˜λ©΄ SQL은 쀑간 κ²°κ³Όμ—μ„œ μ€‘λ³΅λœ 행을 μ‚­μ œν•œλ‹€.

​

​

예제 10-2) 등둝(FEE) ν…Œμ΄λΈ”μ—μ„œ 등둝년도가 2019년인 ν•™μƒμ˜ ν•™λ²ˆ, 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select distinct fe.stu_no, stu_name

-> from fee fe, student s

-> where fe.stu_no=s.stu_no

-> and fe.fee_year = 2019;

​

​

예제 10-3) FEE ν…Œμ΄λΈ”μ—μ„œ 2016λ…„κ³Ό 2018년에 λ“±λ‘ν•œ λͺ¨λ“  ν•™μƒμ˜ ν•™λ²ˆ, 이름, 등둝년도λ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select distinct fe.stu_no, stu_name, fee_year

-> from fee fe, student s

-> where fe.stu_no=s.stu_no

-> and (fe.fee_year = 2016 or fe.fee_year = 2018);

​

​

예제 10-4) ν•™μ ν…Œμ΄λΈ”μ—μ„œ νœ΄λŒ€ν°λ²ˆν˜Έ(MOBILE)κ°€ μ„œλ‘œ λ‹€λ₯Έ ν•™μƒμ˜ νœ΄λŒ€ν°λ²ˆν˜Έλ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select distinct mobile from student;

​

​

예제 10-5) ν•™μ ν…Œμ΄λΈ”μ—μ„œ νœ΄λŒ€ν°λ²ˆν˜Έ(MOBILE)κ°€ μ„œλ‘œ λ‹€λ₯Έ ν•™μƒμ˜ νœ΄λŒ€ν° 번호λ₯Ό 좜λ ₯ν•˜λΌ.(단, νœ΄λŒ€ν°μ΄ μ—†λŠ” 학생은 "νœ΄λŒ€ν° μ—†μŒ"으둜 좜λ ₯ν•˜λΌ)

A)

mysql> select distinct

-> ifnull(mobile, 'νœ΄λŒ€ν° μ—†μŒ')

-> from student;

​

​

​

4. μ–Έμ œ 2개의 행이 λ™λ“±ν•œκ°€?

열듀이 λ™μΌν•œ 값을 λ‚˜νƒ€λ‚΄λ©΄ 동등

​

​

예제 10-6) STUDENT ν…Œμ΄λΈ”μ— 전체 학생 μˆ˜λŠ”?

A)

mysql> select count(*) from student;

​

​

예제 10-7) 성별이 μ—¬μžμΈ 학생은 λͺ‡ λͺ…인가?​

A)

mysql> select count(*)

-> from student

-> where gender in (2,4,6);

​

​

​

5. COUNT ν•¨μˆ˜

COUNT ν•¨μˆ˜μ—μ„œλŠ” κ΄„ν˜Έ 내뢀에 * λ˜λŠ” μˆ˜μ‹μ„ 지정할 수 μžˆλ‹€.

COUNT ν•¨μˆ˜λŠ” NULL 값을 μ œμ™Έν•œ 수λ₯Ό κ³„μ‚°ν•œλ‹€.

​

​

예제 10-8) 2학년인 ν•™μƒμ˜ μˆ˜λŠ” μ–Όλ§ˆλ‚˜ λ˜λŠ”κ°€?

A)

mysql> select count(*)

-> from student

-> where grade = 2;

​

​

예제 10-9) νœ΄λŒ€ν°μ„ 가지고 μžˆλŠ” 학생은 λͺ‡ λͺ…인가?

A)

mysql> select count(mobile)

-> from student;

​

​

예제 10-10) BAN μ—΄μ—μ„œ μ„œλ‘œ λ‹€λ₯Έ 반이 μ–Όλ§ˆλ‚˜ λ˜λŠ”κ°€?

A)

mysql> select count(distinct class)

-> from student;

​

​

예제 10-11) μƒλ…„μ›”μΌμ˜ μ•ž 4자리의 값이 μ„œλ‘œ λ‹€λ₯Έ 숫자λ₯Ό κ°€μ§€λŠ” 것은 λͺ‡ λͺ…인가? (즉, νƒœμ–΄λ‚œ 연도가 μ„œλ‘œ λ‹€λ₯Έ 경우의 수λ₯Ό λ‚˜νƒ€λ‚΄μ–΄λΌ)

A)

mysql> select count(distinct substring(birthday,1,4))

-> from student;

​

​

예제 10-12) STUDENT ν…Œμ΄λΈ”μ— λ‚˜νƒ€λ‚œ μ„œλ‘œ λ‹€λ₯Έ ν•™κ³Ό μ½”λ“œμ˜ μˆ˜λŠ”?

A)

mysql> select count(distinct dept_code)

-> from student;

​

​

예제 10-13) μ„œλ‘œ λ‹€λ₯Έ μž…ν•™λ…„λ„μ˜ κ°œμˆ˜μ™€ ν•™λ…„μ˜ 수λ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select count(distinct substring(stu_no, 1, 4)),

-> count(distinct grade)

-> from student;

​

​

​

6. MAX와 MIN ν•¨μˆ˜

​

​

예제 10-14) λ‚©λΆ€ 총앑 쀑 κ°€μž₯ λ§Žμ€ λ“±λ‘κΈˆμ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select max(fee_pay)

-> from fee;

​

​

예제 10-15) 여학생 μ€‘μ—μ„œ λ“±λ‘κΈˆμ„ κ°€μž₯ 적게 λ‚©λΆ€ν•œ λ“±λ‘κΈˆμ€ μ–Όλ§ˆμΈκ°€?

A)

mysql> select min(fee_pay)

-> from fee

-> where stu_no in

-> (select stu_no

-> from student

-> where gender in (2, 4, 6));

​

​

예제 10-16) μ΅œλŒ€λ‘œ λ‚©λΆ€ν•œ λ“±λ‘κΈˆκ³Ό λ™μΌν•œ λ“±λ‘κΈˆμ„ λ‚©λΆ€ν•œ ν–‰μ˜ μˆ˜λŠ”?

A)

mysql> select fee_pay, count(*)

-> from fee

-> where fee_pay =

-> (select max(fee_pay)

-> from fee);

​

​

예제 10-17) μ΅œλŒ€λ‘œ λ‚©λΆ€ν•œ λ“±λ‘κΈˆκ³Ό λ™μΌν•œ λ“±λ‘κΈˆμ„ λ‚©λΆ€ν•œ ν•™μƒμ˜ μˆ˜λŠ”?

A)

mysql> select count(distinct stu_no)

-> from fee

-> where fee_pay =

-> (select max(fee_pay)

-> from fee);

​

​

예제 10-18) λ“±λ‘ν•œ 학생 쀑 ν•™μƒλ³„λ‘œ κ°€μž₯ λ§Žμ€ λ“±λ‘κΈˆμ„ λ‚©λΆ€ν–ˆμ„ λ•Œ ν•™μƒμ˜ ν•™λ²ˆ, 등둝년도, ν•™κΈ°, 납뢀총앑을 좜λ ₯ν•˜λΌ. 단, 좜λ ₯μˆœμ„œλŠ” ν•™λ²ˆ, 등둝년도, ν•™κΈ° μ˜€λ¦„μ°¨μˆœμ΄λ‹€.

A)

mysql> select stu_no, fee_year, fee_term, fee_pay

-> from fee f1

-> where fee_pay =

-> (select max(fee_pay)

-> from fee f2

-> where f1.stu_no = f2.stu_no)

-> order by stu_no, fee_year, fee_term;

​

​

예제 10-19) λ“±λ‘κΈˆμ„ λ‚©λΆ€ν•œ 학생 쀑 μ΅œλŒ€ λ“±λ‘κΈˆκ³Ό μ΅œμ†Œ λ“±λ‘κΈˆ, μ΅œλŒ€-μ΅œμ†Œκ°„ μ°¨μ΄λŠ” μ–Όλ§ˆμΈκ°€?

A)

mysql> select max(fee_pay), min(fee_pay), (max(fee_pay) - min(fee_pay))

-> from fee;

​

 

예제 10-20) μ˜λ¬Έμ΄λ¦„ 쀑 μ•ŒνŒŒλ²³ μˆœμ„œλ‘œ κ°€μž₯ 큰 κ°’(λ§ˆμ§€λ§‰)을 λ‚˜νƒ€λ‚΄λŠ” μ΄λ¦„μ˜ 첫 문자λ₯Ό μ°Ύμ•„ 좜λ ₯ν•˜λΌ.​

A)

mysql> select substring(max(stu_ename), 1, 1)

-> from student;

​

​

​

7. SUM ν•¨μˆ˜

​

​

예제 10-21) "박정인" 학생이 μž¬ν•™ 쀑 받은 전체 μž₯ν•™κΈˆμ˜ 총앑은 μ–Όλ§ˆμΈκ°€?

A)

mysql> select sum(jang_total)

-> from fee

-> where stu_no in

-> (select stu_no

-> from student

-> where stu_name = '박정인');

​

​

​

8. AVG ν•¨μˆ˜

AVG ν•¨μˆ˜λŠ” νŠΉλ³„ν•œ 열에 μžˆλŠ” κ°’μ˜ μ‚°μˆ  평균을 κ³„μ‚°ν•œλ‹€. μ—΄κ³Ό 수치 μžλ£Œν˜•μ˜ μˆ˜μ‹μ—λ§Œ 적용 κ°€λŠ₯.

​

​

예제 10-22) ν•™λ²ˆ 20161001("박정인") 학생이 받은 μž₯ν•™κΈˆμ˜ 평균을 κ³„μ‚°ν•˜λΌ.

A)

mysql> select avg(jang_total)

-> from fee

-> where stu_no = '20161001';

​

​

예제 10-23) λ™μΌν•œ λ“±λ‘κΈˆ 납뢀총앑을 μ œμ™Έν•œ λ“±λ‘κΈˆ λ‚©λΆ€μ΄μ•‘μ˜ 평균은 μ–Όλ§ˆμΈκ°€?

A)

mysql> select avg(distinct fee_pay)

-> from fee;

​

​

예제 10-24) 평균 μž₯ν•™κΈˆλ³΄λ‹€ 더 λ§Žμ€ μž₯ν•™κΈˆμ„ 받은 ν•™μƒμ˜ ν•™λ²ˆκ³Ό μž₯ν•™κΈˆμ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select distinct stu_no, jang_total

-> from fee

-> where jang_total >

-> (select sum(jang_total) / count(*)

-> from fee);

​

​

예제 10-25) μ˜λ¬Έμ΄λ¦„μ˜ 평균 길이(문자의 수)와 μ΄λ¦„μ˜ μ΅œλŒ€ κΈΈμ΄λŠ” μ–Όλ§ˆμΈκ°€?

A)

mysql> select avg(length(rtrim(stu_ename))), max(length(rtrim(stu_ename)))

-> from student;

​

​

예제 10-26) μž…ν•™κΈˆμ˜ 평균을 avg( ) ν•¨μˆ˜μ™€ μ‚°μˆ ν‰κ· (μž…ν•™κΈˆμ˜ 전체 ν•© / 전체 ν–‰μ˜ 수)을 κ΅¬ν•˜μ—¬λΌ.

A)

mysql> select avg(fee_enter), sum(fee_enter) / count(*)

-> from fee;

μ’€ 더 μ •ν™•ν•˜κ²Œ ν•˜λ €λ©΄ null 값을 0으둜 두고 계산.

​

mysql> select avg(ifnull(fee_enter, 0)), sum(fee_enter)/count(*) from fee;

​

​

​

9. STDDEV와 VARIANCE ν•¨μˆ˜

​

​

예제 10-27) μž₯ν•™κΈˆμ˜ ν‘œμ€€νŽΈμ°¨μ™€ 뢄산을 κ΅¬ν•˜λΌ.

A)

mysql> select stddev(ifnull(jang_total, 0)), variance((ifnull(jang_total, 0)))

-> from fee;

​

​

예제 10-28) μž₯ν•™κΈˆμ˜ 평균을 SUM( ) / COUNT(*)와 AVG( ) ν•¨μˆ˜λ₯Ό μ΄μš©ν•œ κ²°κ³Ό 값을 좜λ ₯ν•˜λΌ.

A)

mysql> select sum(jang_total) / count(*), avg(jang_total)

-> from fee;

​

​

예제 10-29) λ“±λ‘κΈˆ μ΄μ•‘μ˜ 평균을 SUM( ) / COUNT(*)와 AVG( ) ν•¨μˆ˜λ₯Ό μ΄μš©ν•œ κ²°κ³Ό 값을 좜λ ₯ν•˜λΌ.

A)

mysql> select sum(fee_total) / count(*), avg(fee_total)

-> from fee;

​

​

​​

10. μ—΄μ˜ ν‘œμ œμ–΄ μ‚¬μš©

​

​

예제 10-30) 동아리에 μ†Œμ†λœ ν•™μƒμ˜ ν•™λ²ˆ, 이름, μ†Œμ† 동아리 λͺ…을 좜λ ₯ν•˜λΌ(단, 동아리 λͺ…을 좜λ ₯ν•  λ•ŒλŠ” ν‘œμ œμ–΄λ₯Ό "동아리 λͺ…"이라고 κΈ°μž…ν•˜λΌ)

A)

mysql> select stu_no, stu_name, cir_name "동아리λͺ…"

-> from circle;

​​

​

예제 10-31) ꡐ수 ν…Œμ΄λΈ”μ—μ„œ κ΅μˆ˜μ½”λ“œ, ꡐ수λͺ…을 좜λ ₯ν•˜λΌ. 단, 좜λ ₯ν•  λ•ŒλŠ” ν‘œμ œμ–΄λ₯Ό "μ½”λ“œ", "ꡐ수λͺ…"이라고 κΈ°μž…ν•˜κ³ , κ΅μˆ˜μ½”λ“œλŠ” 4001, 4002, 4005, 5010만 좜λ ₯ν•œλ‹€.

A)

mysql> select prof_code "κ΅μˆ˜μ½”λ“œ", prof_name "ꡐ수λͺ…"

-> from professor

-> where prof_code in ('4001', '4002', '4005', '5010');

​​

​

​

<μ—°μŠ΅λ¬Έμ œ>

​

10-2) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ ν•™λ²ˆ, 등둝년도, ν•™κΈ°, μž…ν•™κΈˆ, λ“±λ‘κΈˆμ•‘, λ“±λ‘κΈˆν•©κ³„λ₯Ό 좜λ ₯ν•˜λΌ. (단, μž…ν•™κΈˆμ΄ null인 κ²½μš°μ—λŠ” 'NULL VALUE'λ₯Ό 좜λ ₯ν•˜λΌβ€‹

A)

mysql> select stu_no, fee_year, fee_term, ifnull(fee_enter, 'NULL VALUE'), fee_price, fee_total

-> from fee;

​

 

10-4) μž₯ν•™κΈˆμ˜ 평균 총앑은 μ–Όλ§ˆμΈκ°€?

A)

mysql> select avg(jang_total) from fee;

​

​

10-5) 20191006 학생보닀 μž₯ν•™κΈˆμ„ 더 많이 받은 ν•™μƒμ˜ ν•™λ²ˆκ³Ό μž₯ν•™κΈˆ 총앑을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, sum(jang_total)

-> from fee

-> where jang_total >

-> (select sum(jang_total)

-> from fee

-> where stu_no = '20191006')

-> group by stu_no;

​

​

10-6) λ“±λ‘κΈˆ μ΄μ•‘μ˜ μ΅œλŒ€κ°’κ³Ό μ΅œμ†Ÿκ°’μ˜ 차이와 λ‚©μž…ν•œ κΈˆμ•‘μ˜ μ΅œλŒ€κ°’κ³Ό μ΅œμ†Ÿκ°’μ˜ μ°¨μ΄λŠ” μ–Όλ§ˆμΈκ°€?

A)

mysql> select (max(fee_total) - min(fee_total)), (max(fee_pay) - min(fee_pay))

-> from fee;

​

​

10-7) μ˜λ¬Έμ΄λ¦„μ˜ 길이가 μ˜λ¬Έμ΄λ¦„μ˜ 평균 길이보닀 더 큰 ν•™μƒμ˜ λ²ˆν˜Έμ™€ 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, length(stu_ename)

-> from student

-> where length(stu_ename) >

-> (select avg(length(stu_ename))

-> from student)

λ°˜μ‘ν˜•

'Data Base > MySQL' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

MySQL 21λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 20λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 18λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08
MySQL 17λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08
MySQL 16λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08

λŒ“κΈ€