SQL 연습문제 풀이 1

Yona·2021년 10월 27일
0

🌙 CS_DataBase

목록 보기
10/10

find professor names and course IDs they taught

  • 방법1 : inner join 사용
select name, cID
from professor, teaches
where professor.pID=teaches.pID;

  • 방법2 : natural join 사용
select name, cID
from professor natural join teaches;


find the titles of courses offered by the ‘CS’ department, and names of professors who teach the courses

  • 방법1 : inner join 활용
Select title, name 
from teaches, course, professor 
where teaches.cID=course.cID and teaches.pID=professor.pID and course.deptName='CS';
  • 방법2 : natural join 활요
select title, name
from course, (professor natural join teaches)
where course.cID=teaches.cID and course.deptName='EE';


교수의 이름과 가르치는 과목을 리턴하시오

  • 잘못된 방법
Select name, title
from professor natural join teches natural join course;

course에도 deptName이 존재(이 수업을 제공하는 학과)
professor에도 deptName이 존재(이 교수의 소속 학과)

냅다 professor, teaches, course를 natural join해버리면
서로 다른 뜻을 가진 deptName들끼리 엉뚱히 equi연산을 한후, 같은 tuple들만 남게된다.

  • inner join + natural join
Select name, title
from course, (professor natural join teaches)
where course.cID=teaches.cID;
  • join using
select name, title
from course join (professor natural join teaches) using (cID);

using(cID)를 사용하여 where course.cID=teaches.cID 와 같은 효과(=cID를 기준으로 중복제거) 일으킴.

  • inner join
select name, title
from teaches, course, professor
where teaches.cID=course.cID and teaches.pID=professor.pID;

모두 카타시안 곱(inner join) 한 뒤, cID를 기준으로 각각 중복제거해줌.


이름에 a가 들어가는 교수의 이름을 출력하시오

select name
from professor
where name like '%a%';

like 키워드를 사용한다.


교수의 이름을 내림차순으로 정렬하여 출력하되, 중복을 제거하시오

select distinct name
from professor
order by name desc;

desc의 위치, dstinct의 위치에 유의!


To find the names of all professors with salary between 5000 and 6000 including boundaries

select name, salary
from professor
where salary between 5000 and 6000;

Software 소속의 교수가 가르치는 과목의 cID(course ID)를 출력하시오
주의사항) Software 소속의 교수 != Software 소속 과목을 가르치는 교수

Select name, cID
from professor, teaches
where (professor.pID, deptName) = (teaches.pID, 'Software');

pID를 기준으로 중복 삭제 + deptName이 'software'인것만 출력 을 한번에 튜플로 비교할 수 있다!


Find course IDs that ran in 2014 or in 2015

  • 잘못된 답) and, or 활용
select cID
from teaches
where (year=2014) or (year=2015)


중복을 고스란히 출력

왜냐하면 2014, 2015년 모두 강의된 강의들이 있기 때문이다!!
where은 where절 안이 참인 tuple을 반환하는데,
기본적으로 SQL이 중복을 허용하다보니
year=2014 허용하는 tuples + year=2015하는 tuples가 고스란히 결과 테이블로 가기 때문에 주의햐아한다.

  • union 활용하여 중복제거
(select cID 
 from teaches 
 where year = 2014) 
union 
(select cID 
 from teaches 
 where year = 2015);

2014에 운영된 cID, 2015에 운영된 cID 테이블을 따로 만들어 Union 시킨다.
⚠️ 꼭 한줄로만 짜려고 노력하다가 더 꼬일수도


Find course IDs that ran in Fall 2009 and in Fall 2010

  • and 를 활용한 오답
select cID
from teaches
where (year=2014) and (year=2015);

where절 안을 참으로 만드는 튜플들을 select절로 보내야하는데,
year이 2014이면서 동시에 2015일 수가 없다.
⚠️ 꼭 한줄로만 짜려고 노력하다가 더 꼬일수도

  • intersect 활용
(select cID from teaches where year=2014) intersect (select cID from teaches where year=2014);

Find course IDs taht ran in Fall 2009 but not in Fall 2010

(select cID from teaches where semester='Fall' and year=2009) except (select cID from teaches where semester='Fall' and year = 2010);

Find student names and department names of students whose GPA is below 2.0

select name, deptName
from student
where GPA < 2.0;

Find the student names who have taken at least one course of ‘Software’ department. Make sure that there are no duplicate names in the result

select distinct name
from student join (takes natural join course) using (sID)
where course.deptName='Software';

Retrieve the chairperson name, his/her salary, and the name of department he/she is in charge of

  • 오답
select name, salary, deptName.deptName
from professor, department
where department.chariman=professor.pID 
and department.deptName=professor.deptName;

List student names in a descending order of GPA (highest comes first)

select name, GPA
from student
order by GPA desc;
profile
Sometimes you win, sometimes you learn 🏃‍♀️

0개의 댓글