[DB] MySQL 문제 풀이

최재원's avatar
Mar 06, 2025
[DB] MySQL 문제 풀이

1.

문제 : 부서번호가 10번인 부서의 사람 중 사원번호, 이름, 월급을 출력하라

notion image

해답 :

select empno, ename, sal from emp where deptno = 10;
notion image

2.

문제 : 사원 번호가 7369인 사람 중 이름, 입사일, 부서번호를 출력하라

notion image

해답 :

select ename, hiredate, deptno from emp where empno = 7369;
notion image

3.

문제 : 이름이 ALLEN인 사람의 모든 정보를 출력하라

notion image

해답 :

select * from emp where ename = 'ALLEN';
notion image

4.

문제 : 입사일이 80/12/17인 사원의 이름, 부서번호, 월급을 출력하라

  • ex) hiredate = “80/12/17”
notion image

해답 :

select ename, deptno, sal from emp where hiredate = '80/12/17';
notion image

5.

문제 : 직업이 MANAGER가 아닌 사람의 모든 정보를 출력하라

  • ex) ! =, <>, not in (’’,’’,’’)
notion image

해답 :

select * from emp where job != 'MANAGER';
notion image

6.

문제 : 입사일이 81/04/02 이후에 입사한 사원의 정보를 출력하라

notion image

해답 :

select * from emp where hiredate > '81/04/02';
notion image

7.

문제 : 급여가 $800 이상인 사람의 이름, 급여, 부서번호를 출력하라

notion image

해답 :

select ename, sal, deptno from emp where sal >= 800;
notion image

8.

문제 : 부서번호가 20번 이상인 사원의 모든 정보를 출력하라

notion image

해답 :

select * from emp where deptno >= 20;
notion image

9.

문제 : 입사일이 81/12/09 보다 먼저 입사한 사람들의 모든 정보를 출력하라

notion image

해답 :

select * from emp where hiredate < '81/12/09';
notion image

10.

문제 : 입사번호가 7698보다 작거나 같은 사람들의 입사번호와 이름을 출력하라

notion image

해답 :

select empno, ename from emp where empno <= 7698;
notion image

11.

문제 : 입사일이 81/04/02 보다 늦고 82/12/09보다 빠른 사원의 이름, 월급, 부서번호를 출력하라

notion image

해답 :

select ename, sal, deptno, hiredate from emp where '81/04/02' < hiredate and hiredate < '82/12/09';
notion image

12.

문제 : 급여가 $1,600보다 크고 $3,000보다 작은 사람의 이름, 직업, 급여를 출력하라

notion image

해답 :

select ename, job, sal from emp where 1600 < sal and sal < 3000;
notion image

13.

문제 : 사원번호가 7654와 7782 사이 이외의 사원의 모든 정보를 출력하라.

notion image

해답 :

SELECT * FROM emp WHERE empno <= 7654 OR 7782 <= empno;
notion image

14.

문제 : 이름이 B와 J사이의 모든 사원의 정보를 출력하라.

notion image

해답 :

select * from emp where 'B' < left(ename, 1) and left(ename, 1) < 'J';
notion image

15.

문제 : 입사일이 81년 이외의 입사한 사람의 모든 정보를 출력하라.

  • ex) substr(hiredate, 3, 2) <> ‘81’;
notion image

해답 :

select * from emp where substr(hiredate, 3, 2) < '81' or '81' < substr(hiredate, 3, 2)
notion image

16.

문제 : 직업이 MANAGER와 SALESMAN인 사람의 모든 정보를 출력하라.

notion image

해답 :

select * from emp where job = 'MANAGER' or job = 'SALESMAN';
notion image

17.

문제 : 부서번호가 20, 30번을 제외한 모든 사람의 이름, 사원번호, 부서번호를 출력하라.

notion image

해답 :

SELECT ename, empno, deptno FROM emp WHERE deptno NOT IN (20, 30);
notion image

18.

문제 : S로 시작하는 사원의 사원번호, 이름, 입사일, 부서번호를 출력하라.

  • ex) like
notion image

해답 :

select empno, ename, hiredate, deptno from emp where left(ename, 1) = 'S';
notion image

19.

문제 : 입사일이 81년도인 사람의 모든 정보를 출력하라.

  • ex) substr(hiredate, 3, 2) = ‘81’
notion image

해답 :

select * from emp where substr(HIREDATE, 3, 2) = '81';
notion image

20.

문제 : 이름 중 S자가 들어가 있는 사람만 모든 정보를 출력하라.

  • ex) like
notion image

해답 :

select * from emp where ename like '%S%';
notion image

21.

문제 : 이름이 S로 시작하고 마지막 글자가 T인 사람의 모든 정보를 출력하라(단, 이름은 전체 5자리이다.)

  • ex) like
notion image

해답 :

select * from emp where left(ename, 1) = 'S' and right(ename, 1) = 'T' and length(ename) = 5;
notion image

22.

문제 : 첫 번쨰 문자는 관계없고, 두 번쨰 문자가 A인 사람의 정보를 출력하라.

  • ex) like ‘_A%’
notion image

해답 :

select * from emp where ename like '_A%';
notion image

23.

문제 : 커미션이 NULL인 사람의 정보를 출력하라.

  • ex) where comm is null, where com is not null
notion image

해답 :

select * from emp where comm is null;
notion image

24.

문제 : 커미션이 NULL이 아닌 사람의 정보를 출력하라.

  • ex) where comm is null, where com is not null
notion image

해답 :

select * from emp where comm is not null;
notion image

25.

문제 : 부서가 30번 부서이고 급여가 $1,500 이상인 사람의 이름, 부서, 월급을 출력하라.

notion image

해답 :

select ename, deptno, sal from emp where deptno = 30 and sal >= 1500;
notion image

26.

문제 : 이름의 첫 글자가 K로 시작하거나 부서번호가 30인 사람의 사원번호, 이름, 부서번호를 출력하라.

notion image

해답 :

select empno, ename, deptno from emp where left(ename, 1) = 'K%' or deptno = 30;
notion image

27.

문제 : 급여가 $1500 이상이고 부서번호가 30번인 사원 중 직업이 MANAGER인 사람의 정보를 추력하라.

notion image

해답 :

select * from emp where sal >= 1500 and deptno = 30 and job = 'MANAGER';
notion image

28.

문제 : 부서번호가 30인 사람 중 사원번호가 SORT.

ex) order by
notion image

해답 :

select * from emp where deptno = 30 order by empno;
notion image

29.

문제 : 급여가 많은 순으로 SORT하라.

  • ex) order by
notion image

해답 :

select * from emp order by sal desc;
notion image

30.

문제 : 부서번호로 ASCENDING SORT 한 후 급여가 많은 사람 순으로 출력하라.

  • order by 칼럼 asc, 칼럼 desc
notion image

해답 :

select * from emp order by deptno asc, sal desc;
notion image

31.

문제 : 부서 번호가 DESCENDING SORT하고, 이름 순으로 ASCENDING SORT, 급여순으로 DESCENDING SORT 하라.

notion image

해답 :

select * from emp order by deptno desc, ename asc, sal desc;
notion image

32.

문제 : EMP TABLE 에서 이름, 급여, 커미션 금액, 총액 (SAL + COMM) 을 구하여 총액이 많은 순서로 출력하라.

  • 단 커미션이 null인 사람은 제외한다
notion image

해답 :

select ename, sal, comm, sal + comm '총액' from emp where comm is not null order by '총액';
notion image

33.

문제 : 10번 부서의 모든 사람들에게 급여의 13%를 보너스로 지불하기로 하였다. 이름, 급여, 보너스 금액, 부서번호를 출력하라.

notion image

해답 :

select ename, sal, (sal * 0.13) 'Bunus', deptno from emp where deptno = 10;
notion image

34.

문제 : 30번 부서의 연봉을 계산하여 이름, 부서번호, 급여, 연봉을 출력하라.

  • 단 연말에 급여의 150%를 보너스로 지급한다
notion image

해답 :

select ename, deptno, sal, (sal*12 + sal*1.5) 'Income' from emp where deptno = 30;
notion image

35.

문제 : 급여가 $2,000 이상인 모든 사람은 급여가 15%를 경조비로 내기로 하였다.

  • 이름, 급여, 경조비를 출력하라
notion image

해답 :

select ename, sal, (sal*0.15) '경조비' from emp where sal >= 2000;
notion image

36.

문제 : 모든 사원의 실수령액을 계산하여 출력하라.

  • 단 급여가 많은 순으로 이름, 급여, 실수령액을 출력하라
  • 실수령액은 급여에 대해 10%의 세금을 뺀 금액
notion image

해답 :

select ename, sal, (sal*0.9) '실수령액' from emp order by sal desc;
notion image
Share article

jjack1