백엔드/MySQL

DB 관리 및 쿼리 실습(2)

짱뚱짱 2024. 9. 3. 14:37

https://koop.tistory.com/23

 

DB 관리 및 쿼리 실습(1)

배운것들을 활용한 쿼리 실습.college라는 DB를 root에서 생성하고 mysqlUser@localhost에게 권한을 부여한다.테이블 구조는 다음과 같이 만들 것.  먼저 ERDCloud 사이트에서 ERD를 그렸다. 그 후, 테이블

koop.tistory.com

지난번에 이어, 실습을 진행

 

--생성한 테이블 구조 및 데이터 확인

mysql> select * from student;
+------------+---------+---------+----------+
| st_num     | st_name | st_term | st_point |
+------------+---------+---------+----------+
| 2020123001 | 김영철  |       1 |        0 |
| 2020123002 | 나영희  |       1 |        0 |
| 2020160001 | 강철수  |       2 |        0 |
| 2020160002 | 박철수  |       2 |        0 |
| 2020456001 | 강군    |       2 |        0 |
+------------+---------+---------+----------+
5 rows in set (0.01 sec)

mysql> select * from professor;
+------------+---------+--------+----------+----------+-------------+
| pr_num     | pr_name | pr_age | pr_room  | pr_state | pr_position |
+------------+---------+--------+----------+----------+-------------+
| 2005789001 | 홍길동  |     60 | B동302호 | 재직     | 정교수      |
| 2006456001 | 박영실  |     65 | B동301호 | 안식년   | 정교수      |
| 2010160001 | 강길동  |     55 | A동202호 | 재직     | 조교수      |
| 2010160002 | 김길순  |     50 | A동301호 | 재직     | 조교수      |
| 2011123001 | 이순신  |     55 | A동203호 | 재직     | 정교수      |
+------------+---------+--------+----------+----------+-------------+
5 rows in set (0.00 sec)

mysql> select * from course;
+--------+------------+-----------+---------+---------+---------------------+
| co_num | co_pr_num  | co_su_num | co_term | co_year | co_timetable        |
+--------+------------+-----------+---------+---------+---------------------+
|      1 | 2005789001 |         1 |       1 |    2002 | 월2a/2b/3a/3b/4a/4b |
|      2 | 2010160002 |         1 |       2 |    2002 | 월2a/2b/3a/3b/4a/4b |
|      3 | 2010160001 |         2 |       1 |    2002 | 화1a/1b/2a/2b       |
|      4 | 2010160001 |         3 |       1 |    2002 | 목2a/2b/3a/3b/4a/4b |
|      5 | 2011123001 |         4 |       2 |    2002 | 화1a/1b/2a/2b       |
|      6 | 2011123001 |         5 |       2 |    2002 | 수1a/1b/2a/2b       |
+--------+------------+-----------+---------+---------+---------------------+
6 rows in set (0.00 sec)

mysql> select * from guide;
+--------+------------+------------+---------+
| gu_num | gu_pr_num  | gu_st_num  | gu_year |
+--------+------------+------------+---------+
|      1 | 2010160001 | 2020160001 |    2023 |
|      2 | 2010160001 | 2020160002 |    2023 |
|      3 | 2011123001 | 2020123001 |    2023 |
|      4 | 2011123001 | 2020123002 |    2023 |
|      5 | 2010160002 | 2020456001 |    2023 |
+--------+------------+------------+---------+
5 rows in set (0.00 sec)

mysql> select * from attend;
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
| at_num | at_st_num  | at_co_num | at_mid | at_final | at_hw | at_attend | at_score | at_pass | at_repetition |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
|      1 | 2020123001 |         1 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|      2 | 2020123001 |         2 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|      3 | 2020123001 |         3 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|      4 | 2020123001 |         4 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|      5 | 2020123001 |         5 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|      6 | 2020123001 |         6 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|      7 | 2020123002 |         1 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|      8 | 2020123002 |         2 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|      9 | 2020123002 |         3 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     10 | 2020123002 |         4 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     11 | 2020123002 |         5 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     12 | 2020160001 |         1 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     13 | 2020160001 |         2 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     14 | 2020160001 |         3 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     15 | 2020160001 |         4 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     16 | 2020160001 |         5 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     17 | 2020160001 |         6 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     18 | 2020160002 |         1 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     19 | 2020160002 |         2 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     20 | 2020160002 |         3 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     21 | 2020160002 |         4 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     22 | 2020160002 |         5 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     23 | 2020160002 |         6 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     24 | 2020456001 |         1 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     25 | 2020456001 |         2 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     26 | 2020456001 |         3 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     27 | 2020456001 |         4 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
|     28 | 2020456001 |         5 |      0 |        0 |     0 |         0 | NULL     | f       | n             |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
28 rows in set (0.00 sec)

mysql> select * from subject;
+--------+---------+------------+----------+---------+
| su_num | su_code | su_title   | su_point | su_time |
+--------+---------+------------+----------+---------+
|      1 | msc001  | 대학수학   |        3 |       3 |
|      2 | com001  | 컴퓨터개론 |        2 |       2 |
|      3 | com002  | 운영체제   |        3 |       3 |
|      4 | abc001  | 디자인이론 |        2 |       2 |
|      5 | abc002  | 생활영어   |        2 |       3 |
+--------+---------+------------+----------+---------+
5 rows in set (0.00 sec)

 

--1. 김영철이 수강하는 과목명을 출력
--st_name, su_title => 이름(student st), 과목명(subject su)
select st.st_name, su.su_title from student st
join attend a on a.at_st_num = st_num
join course c on c.co_num = a.at_co_num
join subject su on su.su_num = c.co_su_num
where st.st_name = '김영철';
---- 서브쿼리 형태
SELECT
st_name, 
(
select su_title from subject s
where s.su_num = c.co_su_num
) as su_title
FROM
student st
join attend a on a.at_st_num = st.st_num
join course c on c.co_num = a.at_co_num
WHERE
st.st_name = '김영철';
+---------+------------+
| st_name | su_title   |
+---------+------------+
| 김영철  | 대학수학   |
| 김영철  | 컴퓨터개론 |
| 김영철  | 운영체제   |
| 김영철  | 디자인이론 |
| 김영철  | 생활영어   |
+---------+------------+


--2. 강길동 교수가 지도하는 학생명 출력
select st.st_name as '강길동교수 지도학생'
from guide g
join student st on st.st_num = g.gu_st_num
join professor p on p.pr_num = g.gu_pr_num
where p.pr_name='강길동';
----서브쿼리 형태
select
(
    select st_name from student st
    where st_num = g.gu_st_num
) as '강길동교수 지도학생'
from
  guide g
  join professor p on p.pr_num = g.gu_pr_num
where
  p.pr_name='강길동';
+---------------------+
| 강길동교수 지도학생 |
+---------------------+
| 강철수              |
| 박철수              |
+---------------------+


--3. 대학수학 과목을 수강하는 수강자 명단 출력(중복제거)
select distinct st_name as '대학수학 수강자명단'
from student st
join attend a on a.at_st_num = st.st_num
join course c on c.co_num = a.at_co_num
join subject s on s.su_num = c.co_su_num
where s.su_title ='대학수학';
----서브쿼리 형태
select distinct 
(
select st_name from student st
where st.st_num = a.at_st_num
) as '대학수학 수강자명단'
from 
  attend a
  join course c on c.co_num = a.at_co_num
  join subject s on s.su_num = c.co_su_num
where s.su_title = '대학수학';
+---------------------+
| 대학수학 수강자명단 |
+---------------------+
| 김영철              |
| 나영희              |
| 강철수              |
| 박철수              |
| 강군                |
+---------------------+
--1. at_mid, at_final, at_attend, at_hw 값 업데이트
--40 40 10 10
update attend set at_mid=40, at_final=40, at_attend=10, at_hw=9
where at_num in (1,9,15);
update attend set at_mid=38, at_final=35, at_attend=8, at_hw=7
where at_num in (2,4,10,5,6);
update attend set at_mid=30, at_final=28, at_attend=5, at_hw=7
where at_num in (3,11,20, 25, 27);
update attend set at_mid=28, at_final=25, at_attend=6, at_hw=10
where at_num in (12,21,19);
update attend set at_mid=12, at_final=20, at_attend=2, at_hw=6
where at_num in (7,16,24);
update attend set at_mid=25, at_final=19, at_attend=7, at_hw=5
where at_num in (8,13,22);
update attend set at_mid=38, at_final=35, at_attend=7, at_hw=6
where at_num in(23, 26, 14, 17, 18);
update attend set at_mid=12, at_final=20, at_attend=5, at_hw=6
where at_num=28;
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
| at_num | at_st_num  | at_co_num | at_mid | at_final | at_hw | at_attend | at_score | at_pass | at_repetition |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
|      1 | 2020123001 |         1 |     40 |       40 |     9 |        10 | NULL     | f       | n             |
|      2 | 2020123001 |         2 |     38 |       35 |     7 |         8 | NULL     | f       | n             |
|      3 | 2020123001 |         3 |     30 |       28 |     7 |         5 | NULL     | f       | n             |
|      4 | 2020123001 |         4 |     38 |       35 |     7 |         8 | NULL     | f       | n             |
|      5 | 2020123001 |         5 |     38 |       35 |     7 |         8 | NULL     | f       | n             |
|      6 | 2020123001 |         6 |     38 |       35 |     7 |         8 | NULL     | f       | n             |
|      7 | 2020123002 |         1 |     12 |       20 |     6 |         2 | NULL     | f       | n             |
|      8 | 2020123002 |         2 |     25 |       19 |     5 |         7 | NULL     | f       | n             |
|      9 | 2020123002 |         3 |     40 |       40 |     9 |        10 | NULL     | f       | n             |
|     10 | 2020123002 |         4 |     38 |       35 |     7 |         8 | NULL     | f       | n             |
|     11 | 2020123002 |         5 |     30 |       28 |     7 |         5 | NULL     | f       | n             |
|     12 | 2020160001 |         1 |     28 |       25 |    10 |         6 | NULL     | f       | n             |
|     13 | 2020160001 |         2 |     25 |       19 |     5 |         7 | NULL     | f       | n             |
|     14 | 2020160001 |         3 |     38 |       35 |     6 |         7 | NULL     | f       | n             |
|     15 | 2020160001 |         4 |     40 |       40 |     9 |        10 | NULL     | f       | n             |
|     16 | 2020160001 |         5 |     12 |       20 |     6 |         2 | NULL     | f       | n             |
|     17 | 2020160001 |         6 |     38 |       35 |     6 |         7 | NULL     | f       | n             |
|     18 | 2020160002 |         1 |     38 |       35 |     6 |         7 | NULL     | f       | n             |
|     19 | 2020160002 |         2 |     28 |       25 |    10 |         6 | NULL     | f       | n             |
|     20 | 2020160002 |         3 |     30 |       28 |     7 |         5 | NULL     | f       | n             |
|     21 | 2020160002 |         4 |     28 |       25 |    10 |         6 | NULL     | f       | n             |
|     22 | 2020160002 |         5 |     25 |       19 |     5 |         7 | NULL     | f       | n             |
|     23 | 2020160002 |         6 |     38 |       35 |     6 |         7 | NULL     | f       | n             |
|     24 | 2020456001 |         1 |     12 |       20 |     6 |         2 | NULL     | f       | n             |
|     25 | 2020456001 |         2 |     30 |       28 |     7 |         5 | NULL     | f       | n             |
|     26 | 2020456001 |         3 |     38 |       35 |     6 |         7 | NULL     | f       | n             |
|     27 | 2020456001 |         4 |     30 |       28 |     7 |         5 | NULL     | f       | n             |
|     28 | 2020456001 |         5 |     12 |       20 |     6 |         5 | NULL     | f       | n             |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+

-- 2. at_mid + at_final + at_attend + at_hw 합계 값을 이용하여
-- at_score 업데이트
-- >= 90A / >=80B / >=70C / >=60 D / F
update attend set at_score=
case 
when at_mid + at_final + at_attend + at_hw >= 90 then 'A'
when at_mid + at_final + at_attend + at_hw >= 80 then 'B'
when at_mid + at_final + at_attend + at_hw >= 70 then 'C'
when at_mid + at_final + at_attend + at_hw >= 60 then 'D'
else 'F'
end;
mysql> select * from attend;
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
| at_num | at_st_num  | at_co_num | at_mid | at_final | at_hw | at_attend | at_score | at_pass | at_repetition |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
|      1 | 2020123001 |         1 |     40 |       40 |     9 |        10 | A        | f       | n             |
|      2 | 2020123001 |         2 |     38 |       35 |     7 |         8 | B        | f       | n             |
|      3 | 2020123001 |         3 |     30 |       28 |     7 |         5 | C        | f       | n             |
|      4 | 2020123001 |         4 |     38 |       35 |     7 |         8 | B        | f       | n             |
|      5 | 2020123001 |         5 |     38 |       35 |     7 |         8 | B        | f       | n             |
|      6 | 2020123001 |         6 |     38 |       35 |     7 |         8 | B        | f       | n             |
|      7 | 2020123002 |         1 |     12 |       20 |     6 |         2 | F        | f       | n             |
|      8 | 2020123002 |         2 |     25 |       19 |     5 |         7 | F        | f       | n             |
|      9 | 2020123002 |         3 |     40 |       40 |     9 |        10 | A        | f       | n             |
|     10 | 2020123002 |         4 |     38 |       35 |     7 |         8 | B        | f       | n             |
|     11 | 2020123002 |         5 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     12 | 2020160001 |         1 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     13 | 2020160001 |         2 |     25 |       19 |     5 |         7 | F        | f       | n             |
|     14 | 2020160001 |         3 |     38 |       35 |     6 |         7 | B        | f       | n             |
|     15 | 2020160001 |         4 |     40 |       40 |     9 |        10 | A        | f       | n             |
|     16 | 2020160001 |         5 |     12 |       20 |     6 |         2 | F        | f       | n             |
|     17 | 2020160001 |         6 |     38 |       35 |     6 |         7 | B        | f       | n             |
|     18 | 2020160002 |         1 |     38 |       35 |     6 |         7 | B        | f       | n             |
|     19 | 2020160002 |         2 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     20 | 2020160002 |         3 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     21 | 2020160002 |         4 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     22 | 2020160002 |         5 |     25 |       19 |     5 |         7 | F        | f       | n             |
|     23 | 2020160002 |         6 |     38 |       35 |     6 |         7 | B        | f       | n             |
|     24 | 2020456001 |         1 |     12 |       20 |     6 |         2 | F        | f       | n             |
|     25 | 2020456001 |         2 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     26 | 2020456001 |         3 |     38 |       35 |     6 |         7 | B        | f       | n             |
|     27 | 2020456001 |         4 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     28 | 2020456001 |         5 |     12 |       20 |     6 |         5 | F        | f       | n             |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+

-- 3. at_pass 값 업데이트 (p/f)
-- at_score가 A 또는 B이면 p(pass) 아니면 f(fail)
update attend set at_pass=
case 
when at_score = 'A' or at_score='B' then 'p'
else 'f'
end;
mysql> select * from attend;
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
| at_num | at_st_num  | at_co_num | at_mid | at_final | at_hw | at_attend | at_score | at_pass | at_repetition |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
|      1 | 2020123001 |         1 |     40 |       40 |     9 |        10 | A        | p       | n             |
|      2 | 2020123001 |         2 |     38 |       35 |     7 |         8 | B        | p       | n             |
|      3 | 2020123001 |         3 |     30 |       28 |     7 |         5 | C        | f       | n             |
|      4 | 2020123001 |         4 |     38 |       35 |     7 |         8 | B        | p       | n             |
|      5 | 2020123001 |         5 |     38 |       35 |     7 |         8 | B        | p       | n             |
|      6 | 2020123001 |         6 |     38 |       35 |     7 |         8 | B        | p       | n             |
|      7 | 2020123002 |         1 |     12 |       20 |     6 |         2 | F        | f       | n             |
|      8 | 2020123002 |         2 |     25 |       19 |     5 |         7 | F        | f       | n             |
|      9 | 2020123002 |         3 |     40 |       40 |     9 |        10 | A        | p       | n             |
|     10 | 2020123002 |         4 |     38 |       35 |     7 |         8 | B        | p       | n             |
|     11 | 2020123002 |         5 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     12 | 2020160001 |         1 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     13 | 2020160001 |         2 |     25 |       19 |     5 |         7 | F        | f       | n             |
|     14 | 2020160001 |         3 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     15 | 2020160001 |         4 |     40 |       40 |     9 |        10 | A        | p       | n             |
|     16 | 2020160001 |         5 |     12 |       20 |     6 |         2 | F        | f       | n             |
|     17 | 2020160001 |         6 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     18 | 2020160002 |         1 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     19 | 2020160002 |         2 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     20 | 2020160002 |         3 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     21 | 2020160002 |         4 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     22 | 2020160002 |         5 |     25 |       19 |     5 |         7 | F        | f       | n             |
|     23 | 2020160002 |         6 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     24 | 2020456001 |         1 |     12 |       20 |     6 |         2 | F        | f       | n             |
|     25 | 2020456001 |         2 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     26 | 2020456001 |         3 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     27 | 2020456001 |         4 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     28 | 2020456001 |         5 |     12 |       20 |     6 |         5 | F        | f       | n             |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+

-- 4. at_repetition 값 업데이트(y/n)
-- at_score가 F이거나, at_attend가 3이하인 자료는 y 아니면 n'
update attend set at_repetition =
case when at_score='F' or at_attend<=3 then 'y'
else 'n'
end;
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
| at_num | at_st_num  | at_co_num | at_mid | at_final | at_hw | at_attend | at_score | at_pass | at_repetition |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
|      1 | 2020123001 |         1 |     40 |       40 |     9 |        10 | A        | p       | n             |
|      2 | 2020123001 |         2 |     38 |       35 |     7 |         8 | B        | p       | n             |
|      3 | 2020123001 |         3 |     30 |       28 |     7 |         5 | C        | f       | n             |
|      4 | 2020123001 |         4 |     38 |       35 |     7 |         8 | B        | p       | n             |
|      5 | 2020123001 |         5 |     38 |       35 |     7 |         8 | B        | p       | n             |
|      6 | 2020123001 |         6 |     38 |       35 |     7 |         8 | B        | p       | n             |
|      7 | 2020123002 |         1 |     12 |       20 |     6 |         2 | F        | f       | y             |
|      8 | 2020123002 |         2 |     25 |       19 |     5 |         7 | F        | f       | y             |
|      9 | 2020123002 |         3 |     40 |       40 |     9 |        10 | A        | p       | n             |
|     10 | 2020123002 |         4 |     38 |       35 |     7 |         8 | B        | p       | n             |
|     11 | 2020123002 |         5 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     12 | 2020160001 |         1 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     13 | 2020160001 |         2 |     25 |       19 |     5 |         7 | F        | f       | y             |
|     14 | 2020160001 |         3 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     15 | 2020160001 |         4 |     40 |       40 |     9 |        10 | A        | p       | n             |
|     16 | 2020160001 |         5 |     12 |       20 |     6 |         2 | F        | f       | y             |
|     17 | 2020160001 |         6 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     18 | 2020160002 |         1 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     19 | 2020160002 |         2 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     20 | 2020160002 |         3 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     21 | 2020160002 |         4 |     28 |       25 |    10 |         6 | D        | f       | n             |
|     22 | 2020160002 |         5 |     25 |       19 |     5 |         7 | F        | f       | y             |
|     23 | 2020160002 |         6 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     24 | 2020456001 |         1 |     12 |       20 |     6 |         2 | F        | f       | y             |
|     25 | 2020456001 |         2 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     26 | 2020456001 |         3 |     38 |       35 |     6 |         7 | B        | p       | n             |
|     27 | 2020456001 |         4 |     30 |       28 |     7 |         5 | C        | f       | n             |
|     28 | 2020456001 |         5 |     12 |       20 |     6 |         5 | F        | f       | y             |
+--------+------------+-----------+--------+----------+-------+-----------+----------+---------+---------------+
--1. 학점별 학생 수 출력 (2002 1학기 자료만 사용)
--A B C D E F G 의 학생수
--at_score, count(st_score)
--조건 co_year = 2002, co_term = 1
SELECT a.at_score as '학점', count(a.at_score) as '인원수'
FROM attend a
join course c
on c.co_num = a.at_co_num
where c.co_year = 2002 and c.co_term = 1
group by a.at_score
order by a.at_score;
----서브쿼리 사용
SELECT a.at_score as '학점', count(a.at_score) as '인원수'
FROM attend a
join 
(select co_num from course where co_year=2002 and co_term=1) as c
on c.co_num = a.at_co_num
group by a.at_score
order by a.at_score;
+------+--------+
| 학점 | 인원수 |
+------+--------+
| A    |      3 |
| B    |      5 |
| C    |      3 |
| D    |      2 |
| F    |      2 |
+------+--------+


--2.김영철이 수강하는 과목 출력
--1학기 => 대학수학(1) => su_title(co_term)
--2학기 => 대학수학(2)
--attend, student, subject, course
SELECT concat(su.su_title, '(', c.co_term, ')') as 수강과목
FROM subject su
JOIN course c on c.co_su_num = su.su_num
JOIN attend a on a.at_co_num = c.co_num
JOIN student st on st.st_num = a.at_st_num
WHERE st.st_name = '김영철';
+---------------+
| 수강과목      |
+---------------+
| 대학수학(1)   |
| 대학수학(2)   |
| 컴퓨터개론(1) |
| 운영체제(1)   |
| 디자인이론(2) |
| 생활영어(2)   |
+---------------+

 

'백엔드 > MySQL' 카테고리의 다른 글

MySQL VIEW 생성  (0) 2024.09.03
MySQL INDEX  (0) 2024.09.03
MySQL 외래키(FK, foreign key) 연결 테이블 삭제 순서  (0) 2024.09.03
DB 관리 및 쿼리 실습(1)  (0) 2024.09.02
ROLL UP 및 RANK  (2) 2024.09.02