Database/MySQL

MySQL) MySQL 기초부터 활용까지 75문제 풀기 (1)

pogun 2025. 2. 11. 00:38

SELECT 기초

문제1) EMPLOYEES Table의 모든 자료를 출력하여라.

desc employees;

 

문제2) EMPLOYEES Table의 컬럼들을 모두 출력하라.

select *
from employees;

 

문제3) EMPLOYEES Table에서 사원 번호, 이름, 급여, 담당업무를 출력하여라.

select employee_id, first_name, salary, job_id
from employees;

 

문제4) 모든 종업원의 급여를 $300증가 시키기 위해서 덧셈 연산자를 사용하고 결과에 SALARY+300을 디스플레이 합니다.

select employee_id, first_name, salary + 300 as 급여
from employees;

 

문제5) EMPLOYEES 테이블에서 사원번호, 이름, 급여, 보너스, 보너스 금액을 출력하여라. 
참고로 보너스는 월급 + (월급*커미션)

select employee_id, 
       first_name as 이름, 
       salary as 급여, 
       salary * commission_pct as 보너스 ,
       salary + (salary  * commission_pct) as "보너스 포합 급여"
from employees;

 

문제6) EMPLOYEES 테이블에서 LAST_NAME을 이름으로 SALARY을 급여로 출력하여라.

select last_name as 이름, salary as 급여
from employees;

 

문제7) EMPLOYEES 테이블에서 LAST_NAME을 Name으로 SALARY * 12 를 Annual Salary(연봉)로 출력하여라.

select last_name as Name, salary * 12 as "Annual Salary(연봉)"
from employees;

 

문제8) EMPLOYEES 테이블에서 이름과 업무를 연결하여 출력하여라.

select concat(last_name, ' ', job_id) as 사원정보
from employees;

 

문제9) EMPLOYEES 테이블에서 이름과 업무를 "KING is a PRESIDENT" 형식으로 출력하여라. 

select concat(last_name, ' ', job_id) as 사원정보
from employees;

 

문제11) EMPLOYEES 테이블에서 이름과 연봉을 "KING: 1 Year salary = 60000" 형식으로 출력하여라.

select concat(last_name, ' 1 Year salary = ', salary * 12) as "직원 연봉확인"
from employees;

 


SELECT : where절 활용

문제1) EMPLOYEES 테이블에서 급여가 6000이상인 사원의 사원번호, 이름, 담당업무, 급여를 출력하라.

select employee_id as 사원번호, 
       first_name as 이름, 
       job_id as 담당업무, 
       salary as 급여
from employees
where salary >= 6000;

 

문제2) EMPLOYEES 테이블에서 담당 업무가 ST_MAN인 사원의 사원번호, 성명, 담당업무, 급여, 부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       department_id as 부서번호
from employees
where job_id = 'ST_MAN';

 

문제3) EMPLOYEES 테이블에서 입사일자가 2019년 1월 1일 이후에 입사한 사원의 사원번호, 성명, 담당업무, 급여, 입사일자, 부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       department_id as 부서번호
from employees
where hire_date > '2019-01-01';

 

문제4) EMPLOYEES 테이블에서 급여가 3000에서 5000사이의 사원의 성명, 담당업무, 급여, 부서번호를 출력하라. 

select first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       department_id as 부서번호
from employees
where salary between 3000 and 5000;

select first_name as 성명, 
             job_id as 담당업무, 
             salary as 급여, 
             department_id as 부서번호
from employees
where salary >= 3000 and salary <= 5000;

 

문제5) EMPLOYEES 테이블에서 사원번호가 145,152,203인 사원의 사원번호, 성명, 담당업무, 급여, 입사일자를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       hire_date as 입사일자
from employees
where employee_id in(145, 152, 203);

 

문제6) EMPLOYEES 테이블에서 입사일자가 2020년도에 입사한 사원의 사원번호, 성명, 담당업무, 급여, 입사일자, 부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       department_id as 부서번호
from employees
where hire_date like '2020%';

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       department_id as 부서번호
from employees
where substr(hire_date, 1, 4) = 2020;

 

문제7) EMPLOYEES 테이블에서 보너스가 NULL인 사원의 사원번호, 성명, 담당업무, 급여, 입사일자, 보너스, 부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       ifnull(salary * commission_pct, 0) as 보너스, 
       department_id as 부서번호
from employees
where salary * commission_pct is null;

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       round(salary * commission_pct) as 보너스, 
       department_id as 부서번호
from employees
where salary * commission_pct is not null;

 

문제8) EMPLOYEES 테이블에서 급여가 7000이상이고 JOB이 ST_MAN인 사원의 사원번호, 성명, 담당업무, 급여, 입사일자, 부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       department_id as 부서번호
from employees
where salary >= 7000 and job_id = 'ST_MAN';

 

문제9) EMPLOYEES 테이블에서 급여가 10000이상이거나 JOB이 ST_MAN인 사원의 사원번호, 성명, 담당업무, 급여, 입사일자, 부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       department_id as 부서번호
from employees
where salary >= 10000 or job_id = 'ST_MAN';

 

문제10) EMPLOYEES 테이블에서 JOB_ID가 ST_MAN, SA_MAN, SA_REP가 아닌 사원의 사원번호, 성명, 담당업무, 급여, 부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 담당업무, 
       salary as 급여, 
       department_id as 부서번호
from employees
where job_id not in('ST_MAN', 'SA_MAN', 'SA_REP');

 

문제11) 업무가 AD_PRES 이고 급여가 12000이상이거나 업무가 SA_MAN인 사원의 사원번호, 성명, 업무, 급여를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 업무, 
       salary as 급여
from employees
where (job_id = 'AD_PRES' and salary >= 12000) or job_id = 'SA_MAN';

 

문제12) 업무가 AD_PRES 또는 SALESMAN(SA_)이고 급여가 12000이상의 사원의 사원번호, 이름, 업무, 급여를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 업무, 
       salary as 급여
from employees
where (job_id = 'AD_PRES' or job_id like 'SA_%') and salary >= 12000;

SELECT : ORDER BY_sorting(정렬)

문제1) EMPLOYEES 테이블에서 입사일자 순으로 정렬한 사원번호, 이름, 업무, 급여, 입사일자,부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       department_id as 부서번호
from employees
order by hire_date asc;

 

문제2) EMPLOYEES 테이블에서 가장 최근에 입사한 순으로 정렬한 사원번호, 이름, 업무, 급여, 입사일자,부서번호를 출력하라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 업무, 
       salary as 급여, 
       hire_date as 입사일자, 
       department_id as 부서번호
from employees
order by hire_date desc;

 

문제3) EMPLOYEES 테이블에서 부서번호로 정렬한 후 부서번호가 같을 경우 급여가 많은 순으로 정렬한 사원번호, 성명, 업무, 부서번호, 급여를 출력하여라.

select employee_id as 사원번호, 
       first_name as 성명, 
       job_id as 업무, 
       department_id as 부서번호, 
       salary as 급여
from employees
order by department_id asc, salary desc;

 

문제4) EMPLOYEES 테이블에서 첫번째 정렬은 부서번호로 두번째 정렬은 업무로 세번째 정렬은 급여가 많은 순으로 정렬한 사원번호, 성명, 입사일자, 부서번호, 업무, 급여를 출력하여라.

select employee_id as 사원번호, 
       first_name as 성명, 
       hire_date as 입사일자, 
       department_id as 부서번호, 
       job_id as 업무, 
       salary as 급여
from employees
order by department_id asc, job_id asc, salary desc;

 

문제5)  EMPLOYEES 테이블에서 각 사원의 이름과 근무 달 수(입사일로부터 현재까지의 달수)를 계산하여 컬럼을 MONTHS_WORKED로 지정하시오. 결과는 정수로 반올림하여 표시하고 근무달 수를 기준으로 오름차순으로 정렬하시오.

select concat(first_name, ' ', last_name) as 성명, 
       round(TIMESTAMPDIFF(MONTH, hire_date, CURDATE())) as MONTHS_WORKED
from employees
order by MONTHS_WORKED asc;
-- curdate() : 현재 날짜만 반환(시간 제외)
-- now() : 현재 날짜와 시간 모두 반환

SELECT_GROUP BY

문제1) EMPLOYEES 테이블에서 모든 SALESMAN(SA_)에 대하여 급여의 평균, 최고액, 최저액, 합계를 구하여 출력하여라.

select job_id, 
       avg(salary) as '평균', 
       max(salary) as '최고액', 
       min(salary) as '최저액', 
       sum(salary) as '합계'
from employees
where job_id like 'SA_%'
group by job_id;

 

문제2) EMPLOYEES 테이블에 등록되어 있는 인원수, 보너스가 NULL이 아닌 인원수, 보너스의 평균, 등록되어 있는 부서의 수를 구하여 출력하라.

select count(*) as '직원 수', 
       count(commission_pct) as '보너스 지급 직원수', 
       avg(commission_pct) as '보너스의 평균',
       count(distinct department_id) as '부서의 수'
from employees;

 

문제3) EMPLOYEES 테이블에서 부서별로 인원수, 평균 급여, 최저급여, 최고 급여, 급여의 합을 구하여 출력하라.

select department_id, 
       count(employee_id) as '인원수', 
       avg(salary) as '평균 급여', 
       min(salary) as '최저 급여', 
       max(salary) as '최고 급여', 
       sum(salary) as '급여의 합'
from employees
group by department_id;

 

문제4) EMPLOYEES 테이블에서 각 부서별로 인원수,급여의 평균, 최저 급여, 최고 급여, 급여의 합을 구하여 급여의 합이 많은 순으로 출력하여라.

select department_id, 
       count(employee_id) as '인원수', 
       avg(salary) as '평균 급여', 
       min(salary) as '최저 급여', 
       max(salary) as '최고 급여', 
       sum(salary) as '급여의 합'
from employees
group by department_id
order by sum(salary) desc;

 

문제5) EMPLOYEES 테이블에서 부서별, 업무별 그룹하여 결과를 부서번호, 업무, 인원수, 급여의 평균, 급여의 합을 구하여 출력하여라.

select department_id as '부서번호',
       job_id as '업무',
       count(employee_id) as '인원수', 
       avg(salary) as '평균 급여', 
       sum(salary) as '급여의 합'
from employees
group by department_id, job_id;

 

문제6) EMPLOYEES 테이블에서 부서 인원이 4명보다 많은 부서의 부서번호, 인원수, 급여의 합을 구하여 출력하여라.

select department_id as '부서번호', 
       count(employee_id) as '인원수',  
       sum(salary) as '급여의 합'
from employees
group by department_id
having count(employee_id) > 4;

 

문제7) EMPLOYEES 테이블에서 급여가 최대 10000이상인 부서에 대해서 부서번호, 평균 급여, 급여의 합을 구하여 출력하여라.

select department_id as '부서 번호',  
       avg(salary) as '평균 급여',
	   max(salary) as '최고 급여',
       sum(salary) as '급여의 합'
from employees
group by department_id
having max(salary) >= 10000;

 

문제8) EMPLOYEES 테이블에서 업무별 급여의 평균이 10000 이상인 업무에 대해서 업무명, 평균 급여, 급여의 합을 구하여 출력하라.

select job_id as '업무명',  
       avg(salary) as '평균 급여',
       sum(salary) as '급여의 합'
from employees
group by job_id
having avg(salary) >= 10000;

 

문제9) EMPLOYEES 테이블에서 전체 월급이 10000을 초과하는 각 업무에 대해서 업무와 월급여 합계를 출력하라. 
단, 판매원(SA_)은 제외하고 급여 합계로 정렬(내림차순)하라.

select job_id as '업무',
       sum(salary) as '월급여 합계'
from employees
where job_id not like 'SA_%'
group by job_id
having sum(salary) > 10000
order by sum(salary) desc;

 

MySQL) MySQL 기초부터 활용까지 75문제 풀기 (2)