Subquery - 실습문제
Oracle/SQL & PL/SQL / 2010. 2. 12. 10:16
- 학생테이블에서 이광훈 학생과 같은 학과 학생들의 이름과 학과 이름을 출력하세요.
SQL> select s.name, d.dname
from student s, department d
where s.deptno=d.deptno
and s.deptno=(select deptno from student where name='이광훈');
※ where d.deptno=(select deptno from student where name='이광훈')으로 조건을 입력할 경우
학과 이름이 이광훈 학생의 학과 이름으로 변경되어 "모든 학생들이 검색" 됨
- 101번 학과 학생들의 평균몸무게보다 적은 학생의 이름, 학과번호, 몸무게를 출력하세요.
SQL> select name, deptno, weight
from student
where weight < (select avg(weight) from student where deptno=101);
- 이광훈 학생의 학과의 평균몸무게보다 작은 학생들의 학생이름과 학생의 몸무게,
각 학생들의 학과이름과 지도교수 이름을 출력하세요.
SQL> select s.name, s.weight, d.dname, p.name
from student s, department d, professor p
where s.deptno=d.deptno and s.profno=p.profno(+) and
s.weight < (select avg(weight) from student
where deptno = (select deptno from student where name='이광훈'));
※ (+)가 빠질 경우, 지도교수가 없는 학생들은 검색에서 제외 됨
- temp, tdept 테이블을 사용하여 인천에 근무하는 직원의 사번과 성명을 구하세요.
SQL> select emp_id, emp_name
from temp
where dept_code in (select dept_code from tdept where area='인천');
- temp, tdept, tcom 테이블을 참고하여 부서별로 commission을 받는 인원수를 세는 쿼리를 작성하세요.
SQL> select d.dept_name, count(*)
from tdept d, temp e
where d.dept_code=e.dept_code
and emp_id in (select emp_id from tcom)
group by d.dept_name;
- temp 테이블에서 과장 중 가장 급여를 적게 받는 사람보다 많이 받는 사원들의
사번, 이름 급여를 출력하세요.
SQL> select emp_id, emp_name, salary
from tem
where salary > any (select salary from temp where lev='과장');
- temp 테이블에서 부서별로 최대 연봉을 구해서 연봉을 받는 사원의 부서코드, 사번, 이름, 급여를 출력하세요.
SQL> select dept_code, emp_id, emp_name, salary
from temp
where (dept_code,salary) in (select dept_code, max(salary) from temp group by dept_code);
'Oracle > SQL & PL/SQL' 카테고리의 다른 글
DDL (0) | 2010.02.17 |
---|---|
DML (0) | 2010.02.12 |
실습파일 내용 (0) | 2010.02.11 |
Subquery (0) | 2010.02.11 |
JOIN (ANSI JOIN) (0) | 2010.02.11 |