Programming/SQL

[SQL/실습] 데이터 검색 - 연습문제 풀어보기 1 (답안)

reeme 2020. 12. 16. 20:33

 

테이블 만들고 튜플 삽입하기

use example;



부서(department) 테이블 

create table department (
dno varchar(2) primary key,
dname varchar(15),
loc varchar(10)
);

 

 

부서(department) 테이블에 튜플 삽입
 -- 단축키 사용 (ctrl + shift + enter)

 insert into department values('10','Accounting','Seoul');
 insert into department values('20','Human','Incheon');
 insert into department values('30','Sales','Yungin');
 insert into department values('40','Computing','Suwon');

 


부서(department) 테이블 조회해서 insert 여부 확인

select * from department;



사원(employee) 테이블 

create table employee(
eno varchar(3) primary key,
ename varchar(5),
job varchar(15),
manager varchar(3),
hiredate datetime,
salary int,
commission int,
dno varchar(2)
);

 

 

사원(employee) 테이블에 튜플 삽입
-- 단축키 사용 (ctrl + shift + enter)

insert into employee values('101','e1','section','102','2020-01-01',100,100,'10');
insert into employee values('102','e2','chief','103','2020-01-02',200,Null,'10');
insert into employee values('103','e3','senior','105','2020-01-03',2500,200,'30');
insert into employee values('104','e4','section','107','2020-01-04',400,Null,'30');
insert into employee values('105','e5','chief','101','2020-01-05',450,100,'20');
insert into employee values('106','e6','senior','108','2020-01-06',500,Null,'10');
insert into employee values('107','e7','section','109','2020-01-07',520,100,'30');
insert into employee values('108','e8','chief','103','2020-01-08',670,Null,'20');
insert into employee values('109','e9','senior','103','2020-01-09',800,100,'10');
insert into employee values('110','e10','section','103','2020-01-10',120,Null,'20');
insert into employee values('111','e11','chief','104','2020-01-11',130,0,'10');
insert into employee values('112','e12','senior','101','2020-01-12',220,Null,'30');
insert into employee values('113','e13','section','102','2020-01-13',520,100,'10');

 

사원(employee) 테이블 조회해서 insert 여부 확인

select * from employee;

 

 

답안


-- 사원 테이블 (employee)에서 사원명(ename)과 봉급(salary) *12 검색

select ename, salary*12 as yearsal -- as뒤에 ''도 생략가능 
from employee;



-- 사원테이블(employee)에서 사원번호(eno) 110번의 이름 (ename)과 부서번호(dno) 검색

select ename, dno
from employee
where eno='110';



-- 사원 테이블에서 부서번호 (dno)가 20번이고 봉급(salary)이 400이상인 사원의 이름(ename)과 직책(job) 검색

select ename, job
from employee
where dno='20' and salary>='400'
;



-- 사원 테이블에서 입사일이 2020년 1월 10일에서 2020년 1월 12일 사이인 회원의 이름과 직책을 검색

select ename, job
from employee
-- where hiredate>='2020-01-10' and hiredate<='2020-01-12'
where hiredate between '2020-01-10' and '2020-01-12'
;



-- 사원 테이블에서 부서번호가 10 이거나 30인 회원의 이름과 직책을 검색

select ename, job
from employee
-- where dno = 10 or dno =30 -- or연산자
where dno in ('10','30') -- in 연산자
; 



-- 사원 테이블에서 보너스(commission)가 null값인 회원의 모든 속성을 검색

select *
from employee
where commission IS NULL -- null 값은 = 연산자로 비교를 못함 **중요**
;

 


-- 사원 테이블에서 보너스(commission)가 null이 아닌 값의 회원의 모든 속성을 검색

select *
from employee
where commission IS not NULL -- null 값은 = 연산자로 비교를 못함 **중요**
;



-- 사원테이블에서 직책(job)에 ie가 들어가있는 회원의 이름 , 직책 , 부서번호를 검색

select ename, job, dno 
from employee
where job like '%ie%'
;



-- 봉급 기준 내림차순으로 사원들의 이름 봉급 부서번호 출력
   단, 봉급이 같은 회원일 경우에는 부서번호가 낮은 순으로 ( 오름차순 ) 으로 재정렬

select ename, salary, dno
from employee
order by salary desc, dno asc;  
-- 봉급(salary)기준 내림차순으로 하고, 봉급이 같은 경우에는 부서번호가 낮은 순으로 재정렬



-- 사원 테이블의 모든 속성을 조회하시오

select *
from employee;



-- 사원명과 입사일을 조회하시오

select ename, hiredate
from employee;



-- 사원번호와 이름을 조회하시오

select eno, ename
from employee;



-- 사원테이블에 있는 직책의 목록을 조회하시오

select distinct job
from employee
;



-- 부서번호가 10인 사원을 조회하시오

select *
from employee
where dno='10'
;



-- 월급여가 2500이상 되는 사원을 조회하시오

select *
from employee
where salary>='2500'
;



-- 이름이 e10인 사원을 조회하시오

select *
from employee
where ename='e10'
;



-- 사원들 중 직책이 S로 시작하는 사원의 사원번호와 이름을 조회하시오.

select eno,ename
from employee
where job like 'S%'
;



-- 사원 직책에 e가 포함된 사원의 사원번호와 이름을 조회하시오.

select eno,ename
from employee
where job like '%e%'
;



-- 월급이 200,400,2500 인 사원의 사번, 이름, 커미션을 조회하시오

select eno, ename, commission
from employee
where salary in (200,400,2500)
;

 


-- 월급여가 400~500사이의 사원의 사번 이름 월급여를 조회하시오

select eno, ename, salary
from employee
where salary between 400 and 500
;

 


-- 직책이 chief이고 부서번호가 30번인 사원의 이름 사번 직급 부서번호를 조회하시오

select ename, eno, job, dno
from employee
where job='chief' and dno='30'
;



-- 부서번호가 30이 아닌 사원의 사번 이름 부서번호를 조회하시오

select eno, ename, dno
from employee
where dno!=30
;



-- 월급이 200 400 2500이 아닌 사원의 사번 이름 커미션을 조회하시오

select eno, ename, commission
from employee
-- select eno, ename, commission from employee where salary!=200 in salary!=400 in salary!=2500; 
where salary not in (200,400,2500)
;



-- 커미션이 NULL인 사원의 이름과 직급을 조회하시오.

select ename, job
from employee
where commission is null
;