3. Select와 Order By

김해준's avatar
Dec 26, 2025
3. Select와 Order By
Contents
예제풀이

1. 기본 문법

-- 1. SELECT 기본 문법(select는 order by 바로 직전에 실행) SELECT EMPNO, ENAME FROM emp; SELECT * FROM emp;
notion image
notion image

2. 특정 행만 골라보기

-- 2. 특정 행만 골라보기 SELECT * FROM emp WHERE EMPNO = 7369;
notion image

3. 테이블 스키마 보기

-- 3. 테이블 스키마 보기 desc emp;
notion image

4. 별칭

-- 4. 별칭 SELECT empno as '사원번호' from emp; SELECT empno 사원번호 from emp;
notion image

5. 중복 제거

-- 5. 서로다른 : 뜻 -> 중복 제거 SELECT distinct job FROM emp;
notion image

6. 연결연산자

-- 6. 연결연산자 SELECT concat(ename, '의 직업은 ', job) 소개 from emp;
notion image

7. ifnull 함수

-- 7. ifnull 함수 select ename, ifnull(comm, 0) + sal from emp;
notion image

8. 이름으로 찾기

-- 8. 이름으로 찾기 select * from emp where ename = 'SMITH';
notion image

9. 날짜로 찾기 (date)

-- 9. 날짜로 찾기 (date) select * from emp where hiredate = '1980-12-17'; select * from emp where hiredate = '1980/12/17';
notion image

10. null값 찾기

-- 10. null값 찾기 select ename, comm from emp where comm is null; select ename, comm from emp where comm is not null;
notion image
notion image

11. 복잡한 where (or, and, in, between)

-- or select * from emp where sal = 800 or sal = 1600; select * from emp where sal = 800 or job = 'manager'; -- in select * from emp where sal in (800, 1600); -- and select * from emp where sal = 800 and deptno = 20; -- between SELECT * FROM emp WHERE sal BETWEEN 800 AND 3000;
notion image
notion image
notion image
notion image
notion image

12. like 연산자

-- 12. like 연산자 select ename from emp where ename like 'S%'; select ename from emp where ename like '%T'; select ename from emp where ename like '%M%'; select ename from emp where ename like '%M%'; select ename from emp where ename like '__N%';
notion image
notion image
notion image
notion image
notion image

13. 정렬

-- 13. 정렬(order by는 제일 마지막에 실행) -- 오름차순 select * from emp order by sal asc; select * from emp order by sal; -- 내림차순 select * from emp order by sal desc; -- 정렬 연속 사용 select * from emp order by job asc, sal desc;
notion image
notion image
notion image
 
select from where orderby 의 동작 순서
from → where → select → orderby

예제풀이

-- 1. 부서번호가 10번인 부서의 사람 중 사원번호, 이름, 월급을 출력하라 select empno, ename, sal from emp where deptno = 10; -- 2. 사원번호가 7369인 사람 중 이름, 입사일, 부서번호를 출력하가 select ename, hiredate, deptno from emp where empno = 7369; -- 3. 이름이 ALLEN 인 사람의 모든 정보를 출력하라 select * from emp where ename = 'ALLEN'; -- 4. 입사일이 80/12/17인 사원의 이름, 부서번호, 월급을 출력하라 select ename, deptno, sal from emp where hiredate = '80/12/17'; -- 5. 직업이 Manager가 아닌사람의 모든 정보를 출력하라 select * from emp where job != 'MANAGER'; -- 6. 입사일이 81/04/02 이후에 입사한 사원의 정보를 출력하라 select* from emp where hiredate > '81/04/02'; -- 7. 급여가 $800 이상인 사람의 이름, 급여, 부서번호를 출력하라 select ename, sal, deptno from emp where sal >= 800; -- 8. 부서번호가 20 이상인 사원의 모든 정보를 출력하라 select * from emp where deptno >= 20; -- 9. 입사일이 81/12/09보다 먼저 입사한 사람들의 모든 정보를 출력하라 select* from emp where hiredate<'81/12/09'; -- 10. 입사번호가 7698보다 작거나 같은 사람들의 입사번호와 이름을 출력하라 select empno,ename from emp where empno <= 7698; -- 11. 입사일이 81/04/02보다 늦고 82/12/09보다 빠른 사원의 이름, 월급, 부서번호를 출력하라 select ename, sal, deptno from emp where hiredate > '81/04/02' and hiredate < '82/12/09'; -- 12. 급여가 $1600보다 크고 $3000보다 작은 사람의 이름, 직업, 급여를 출력하라 select ename, job, sal from emp where sal > 1600 and sal < 3000; -- 13. 사원번호가 7654와 7782사이 이외의 사원의 모든 정보를 출력하라 select * from emp where empno < 7654 or empno >7782; -- 14. 이름이 B와 J사이의 모든 사원의 정보를 출력하라 select * from emp where ename between 'B%' and 'j%'; -- 15. 입사일이 81년 이외에 입사한 사람의 모든 정보를 출력하라 select * from emp where substr(hiredate, 3, 2)<> '81'; -- 16. 직업이 MANAGER와 SALESMAN인 사람의 모든 정보를 출력하라 select * from emp where job = 'MANAGER' or job = 'SALESMAN'; -- 17. 부서번호가 20, 30번을 제외한 모든 사람의 이름, 사원번호, 부서번호를 출력하라 select ename, empno, deptno from emp where deptno != 20 and deptno != 30; -- 18. S로 시작하는 사원의 사원번호, 이름, 입사일, 부서번호를 출력하라 select empno, ename, hiredate, deptno from emp where ename like 'S%'; -- 19. 입사일이 81년도인 사람의 모든 정보를 출력하라 select * from emp where substr(hiredate,3,2) = '81'; -- 20. 이름 중 S자가 들어가 있는 사람만 모든 정보를 출력하라 select * from emp where ename like '%S%'; -- 21. 이름이 S로 시작하고 마지막 글자가 T인 사람의 모든 정보를 출력하라 select * from emp where ename like 'S___T'; -- 22. 첫 번째 문자는 관계없고, 두번째 문자가 A인 사람의 정보를 출력하라 select * from emp where ename like '_A%'; -- 23. 커미션이 NULL인 사람의 정보를 출력하라 select * from emp where comm is null; -- 24. 커미션이 NULL이 아닌사람의 정보를 출력하라 select * from emp where comm is not null; -- 25. 부서가 30번 부서이고 급여가 $1500이상인 사람의 이름 부서 월급을 출력하라 select ename, deptno, sal from emp where deptno = 30 and sal >= 1500; -- 26. 이름의 첫 글자가 K로 시작하거나 부서번호가 30인 사람의 사원번호, 이름, 부서번호를 출력하라 select empno, ename, deptno from emp where ename like 'K%' or sal >= 1500; -- 27. 급여가 $1500 이상이고 부서번호가 30번인 사원 중 직업이 MANAGER인 사람의 정보를 출력하라 select * from emp where sal >= 1500 and deptno = 30 and job = 'MANAGER'; -- 28. 부서번호가 30인 사람 중 사원번호가 SORT select * from emp order by empno; -- 29. 급여가 많은 순으로 SORT해라 select * from emp order by sal DESC; -- 30. 부서번호로 ASCENDING SORT 한 후 급여가 ㅏㄶ은 사람 순으로 출력하라 select * from emp order by deptno asc, sal desc; -- 31. 부서번호가 DESCENDING SORT하고 이름 순으로 ASCENDING SORT, 급여순으로 DESCENDING SORT하라 select * from emp order by deptno desc, ename asc, sal desc; -- 32. EMP TABLE에서 이름, 급여, 커미션 금액, 총액 (SAL + COMM)을 구하여 총액이 많은 순서로 출력하라 select ename, sal, comm, ROUND((sal + comm),2) as SUM from emp where comm is not null; -- 33. 10번 부서의 모든 사람들에게 급여의 13%를 보너스로 지불하기로 하였다 이름, 급여, 보너스금액, 부서번호를 출력하라 select ename, sal, ROUND((sal * 0.13),2) as 'BUNUS', deptno from emp where deptno = 10; -- 34. 30번 부서의 연봉을 계산하여 이름, 부서번호, 급여, 연봉을 출력하라 select ename, deptno, sal, ROUND(((sal * 12) + (sal * 1.5)),2) as 'INCOME' from emp where deptno = 30; -- 35. 급여가 $2000이상인 모든 사람은 급여가 15%를 경보비로 내기로 했다 이름, 급여, 경조비를 출력하라 select ename, sal, ROUND((sal * 0.15),2) from emp where sal >= 2000; -- 36. 모든 사원의 실수령액을 계산하여 출력하라 단, 급여가 많은 순으로 이름, 급여, 실수령액을 출력하라 -- 실수령액은 급여에 대해 10%의 세금을 뺀 금액 select ename, sal, ROUND((sal)-(sal*0.1),2) from emp order by sal desc;
 
Share article