<JOIN>
1. F를 받은 학생 명단 (학번, 이름, 학과)
select s.std_num,s.std_name,s.std_major,a.at_score
from attend a
inner join student s
on a.at_std_num=s.std_num
where a.at_score='F';
2. A학점을 받은 학생 명단(학번, 이름 , 과목, 교수명)
select s.std_num,s.std_name,c.co_name,c.co_professor
from attend a
inner join student s
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
where a.at_score='A';
3. A학점 학생들의 학번, 이름, 과목, 성적(중간+기말+출석+과제)
select s.std_num,s.std_name,c.co_name, (a.at_mid+a.at_final+a.at_attend+a.at_hw) as '성적'
from attend a
inner join student s
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
where a.at_score='A';
4. 과목별(co_name) 별 중간, 기말 출석 과제 합계(sum)
컴퓨터 개론 30,50,10,30
디자인 50,60,20,30
select c.co_name,sum(a.at_mid+a.at_final+a.at_attend+a.at_hw) as '성적'
from attend a
inner join student s
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
group by co_name;
///
select c.co_name,sum(a.at_mid) as mid, sum(a.at_final) as final, sum(a.at_attend) as attend, sum(a.at_hw) as hw
from attend a
join course c
on a.at_co_code=c.co_code
where a.at_term=1
group by c.co_code
order by c.co_name;
5. 학점별 중간, 기말, 출석, 과제, 평균(null 은 제외, 학점별 오름차순)
select a.at_score ,
avg(a.at_mid+a.at_final+a.at_attend+a.at_hw) as '성적'
from attend a
inner join student s
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
group by a.at_score
having a.at_score is not null
order by a.at_score asc;
//
select a.at_score ,
round(avg(a.at_mid),1) as mid,
round(avg((a.at_final),1) as final,
round(avg(a.at_attend),1) as attend,
round(avg(a.at_hw),1) as hw
from attend a
group by a.at_score
having a.at_score is not null
order by a.at_score;
6. 강철수가 수강하고 있는 과목명과 교수명 출력
=> attend에 칼럼이 필요하진 않지만 student, course를 연결하려면 필요
select s.std_name,c.co_name,c.co_professor
from attend a
inner join student s
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
where s.std_name='강철수';
<JOIN>
1. 프로그래밍 일반 과목을 듣는 학생 명단 출력
(학번 , 이름, 학과)
select s.std_num,s.std_name,c.co_name
from student s
inner join attend a
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
where c.co_name='프로그래밍일반';
2. 홍길동 교수가 강의하는 과목을 듣는 학생 명단
(학번, 이름, 학과, 수강과목, 담당교수)
select s.std_num,s.std_name,s.std_major,c.co_name,c.co_professor
from student s
inner join attend a
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
where c.co_professor='홍길동';
3. 전봉준(3)이 획득한 학점(co_point) 합계 => 강나래(5)
(2학기는 미포함, 미이수학점도 제외)
이름, 학과, 학점합계
select s.std_num, s.std_major,sum(c.co_point)
from student s
inner join attend a
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
where a.at_term=1
and
a.at_score='n'
group by s.std_name
having s.std_name='전봉준';
//
select s.std_name,sum(c.co_point)
from student s
inner join attend a
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
where a.at_term=1
and
a.at_repetition='n'
and
s.std_name='강철수'
group by s.std_name;
//
select s.std_name,sum(c.co_point),s.std_major
from student s
inner join attend a
on a.at_std_num=s.std_num
inner join course c
on a.at_co_code=c.co_code
where a.at_term=1
and
a.at_repetition='n'
and
s.std_name='강철수'
group by s.std_name,s.std_major;
4. insert into student values
('2024160003','김순이','컴퓨터공학',1,25),
('2024160004','홍순길','디자인',1,20);
-학생 명단에는 있지만 수강하고 있지 않은 학생
=> left join
student 0 attend x
left join
select s.* from student s
left join attend a
on s.std_num= a.at_std_num
where a.at_num is null;
'SQL' 카테고리의 다른 글
| SQL 강의 - day3: MYSQL (0) | 2025.03.09 |
|---|---|
| SQL 강의 - day2 : MYSQL (1) | 2025.03.09 |
| MYSQL 2일 - 테이블만들기 (0) | 2025.03.09 |
| MYSQL 1일 -테이블만들기 (0) | 2025.03.09 |
| SQL 강의 - day1 : MYSQL (0) | 2025.02.24 |