βΆ ν΅κ³ν¨μ
β
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 |
λκΈ