- order by 구문
: 정렬 하기. 반드시 select 구문의 마지막에 작성해야 함. : order by 칼럼명 [asc] | desc
- asc : 오름차순, 생략가능.
- desc : 내림차순.
문제 :
1. 1학년 학생의 이름,키를 조회하기. 단, 키가 작은 순으로 정렬하기.
select name,height from student where grade = 1;
select name,height from student where grade = 1 order by asc;
문제 :
2. 1학년 학생의 이름,키를 조회하기. 단,키가 큰 순으로 정렬하기.
select name,height from student where grade = 1 order by desc;
문제 :
3. 학생의 이름,키,몸무게,학년을 조회하기. 단 학년순, 키가큰 순,몸무게가 작은 순으로 조회하기.
select name,height,weight,grade from student order by grade acs,height desc,weight asc;
select name,height,weight,grade from student order by 4,2 desc,3;
💡
order by 조건문에서 칼럼 명 대신 쓸 수 있는 연산을 쓸 수도 있고, 숫자 혹은 별명을 쓸 수 있다. 숫자는 조회 되는 칼럼의 순서이다.
ORACLE의 order by 구문에서만 쓸 수 있다.
문제 :
4. 교수의 급여를 10% 인상예정임. 교수번호,교수이름,학과코드,급여,예상급여(10%인상) 조회하기.
단 학과코드 역순으로 조회하기.
select profno,name,deptno,pay,pay*1.1 예상급여 from professor order by 3,5 desc;
select profno,name,deptno,pay,pay*1.1 예상급여 from professor order by deptno,예상급여 desc;
문제 :
5. 학생테이블에서 지도교수가 배정되지 않은 학생의 학번,이름,지도교수번호,학과코드 출력하기.
단 학과코드 순으로 정렬하기.
select studno,name,profno,deptno1 from student where profno is null order by 4;
- 지도교수가 배정되지 않은 학생을 찾는 것 이므로 is null을 사용.
'수업(국비지원) > Oracle' 카테고리의 다른 글
[Oracle] SQL 단일행 함수 - 문자 함수(대소문자 변환 함수) initcap, upper, lower (0) | 2023.04.14 |
---|---|
[Oracle] 합집합 union (0) | 2023.04.13 |
[Oracle] Null (0) | 2023.04.13 |
[Oracle] like, not like 연산자 (0) | 2023.04.13 |
[Oracle] in, not in 연산자 (0) | 2023.04.13 |