SLECT 칼럼명 [ALIAS명]
FROM 테이블명
[WHERE 조건식]
[GROUP BY 칼럼(Column)이나 표현식]
[HAVING 그룹조건식 ]
[ORDER BY 칼럼(Column)이나 표현식 [ASC 또는 DESC] ] ;
SELECT PLAYER_NAME 선수명
, POSITION 포지션
, BACK_NO 백넘버
FROM PLAYER
ORDER BY 포지션 ASC; --(이름으로 내림차순정렬)
--ORDER BY 2
--ORDER BY POSITION
SELECT DNAME
, LOC
, DEPTNO
FROM DEPT
ORDER BY DNAME, LOC, DEPTNO DESC;
SELECT DNAME AS DEPT
, LOC AS AREA
, DEPTNO
FROM DEPT
ORDER BY DNAME, AREA, DEPTNO DESC;
SELECT DNAME
, LOC AREA
, DEPTNO
FROM DEPT
ORDER BY 1, AREA, 3 DESC;
5. SELECT 칼럼명 [ALIAS명]
1. FROM 테이블명
2. WHERE 조건식
3. GROUP BY 칼럼(Column)이나 표현식
4. HAVING 그룹조건식
6. ORDER BY 칼럼(Column)이나 표현식;
SELECT EMPNO
, ENAME
FROM EMP
ORDER BY MGR;
결과:정상출력
(SELECT 절에서 정의하지 않은 칼럼을 사용해도 문제없음)
SELECT EMPNO
FROM
(
SELECT EMPNO
, ENAME
FROM EMP
ORDER BY MGR
)
;
결과:14개의 행이 선택되었다.
(인라인 뷰의 SELECT 절에서 정의한 칼럼은 메인쿼리에서도 사용할 수 있음)
SELECT MGR
FROM
(
SELECT EMPNO
, ENAME
FROM EMP
ORDER BY MGR
)
;
결과: SELECT MGR FROM ; * ERROR: "MGR": 부적합한 식별자
SELECT JOB
, SAL
FROM EMP
GROUP BY JOB
HAVING COUNT(*) > 0
ORDER BY SAL ;
--결과:SELECT JOB, SAL ; * ERROR: GROUP BY 표현식이 아니다.
SELECT JOB
FROM EMP
GROUP BY JOB
HAVING COUNT(*) > 0
ORDER BY SAL;
--결과:ORDER BY SAL; * ERROR: GROUP BY 표현식이 아니다.
SELECT JOB
FROM EMP
GROUP BY JOB
HAVING COUNT(*) > 0
ORDER BY MAX(EMPNO), MAX(MGR), SUM(SAL)
, COUNT(DEPTNO), MAX(HIREDATE);
결과: 정상출력 ||
SELECT ENAME
, SAL
FROM EMP
WHERE ROWNUM < 4
ORDER BY SAL DESC;
SELECT ENAME
, SAL
FROM
(
SELECT ENAME
, SAL
FROM EMP
ORDER BY SAL DESC
)
WHERE ROWNUM < 4
SELECT TOP(4)
ENAME
, SAL
FROM EMP
WHERE ROWNUM < 4
ORDER BY SAL DESC