[강의 정리]D-50 DB - select 활용
show tables;
-- 1. 데이터 조회
-- 1) 특정 컬럼 조회
-- SELECT [컬럼 ] FROM [테이블];
select * from employees;
select first_name, family_name, salary from employees;
-- 산술 표현
-- 숫자컬럼의 경우 산술표현이 된다.
select first_name, family_name, salary/10000 from employees;
-- as를 사용하면 컬럼에 별칭을 부여 할 수 있다.
select first_name, family_name, salary*12 as 연봉 from employees;
select concat(family_name,first_name)as name, salary*12 as 연봉 from employees;
-- name , salary를 출력 해 보자
-- salary는 450만원 형태로 출력 해야 한다.
select concat(family_name,first_name)as name,
concat(truncate(salary/10000,0),'만원')as 월급 from employees;
-- 2) 특정 조건을 만족하는 데이터 조회
-- select [컬럼] from [테이블] where[조건];
select * from employees where family_name = '김';
-- 급여가 300만원 이상인 사람을 찾자
select * from employees where salary >= 3000000;
-- 2-1) and 조건
-- 급여가 100만원 이상이고, 500 만원 이하인 사원 찾기
select * from employees where salary >= 1000000 and salary <=5000000;
-- 2-2) or 조건
-- family_name 이 김이거나 급여가 200000인 사람 찾기
select * from employees where salary >= 2000000 or family_name = '김' ;
-- 2-3) BETWEEN AND
-- salary 가 50 만원 이상 400만원 이하인 사람의 first_name 과 family_name 구하기
select first_name, family_name, salary from employees where salary >= 500000 and salary <= 4000000;
select first_name, family_name, salary from employees where salary between 500000 and 4000000 ;
-- 3) 중복 제거
-- SELECT distinct [중복제거 컬럼, ...] FROM [테이블]
-- 메뉴얼 상에는 1개이상 가능 하다고 하지만 1개 이상 넣지 않는게 좋다. (두 개를 합쳐버림)
-- 중복제거는 1개 컬럼을 대상으로 하는것이 가장 정확하다 . (2개 할려고 하면 다시 distinct 걸어야 함.)
select distinct family_name from employees;
select distinct family_name , salary from employees;
-- 4) IN : 공통 컬럼으로 여러 값을 or조건을 사용할 때 간략화 시킨다.(속도도 우월하다.)
-- family_name 이 '김' 이거나, '박' 이거나, '이' 인사람의 모든 컬럼을 가져와라
select * from employees where family_name = '김' or family_name = '박' or family_name = '이' ;
select * from employees where family_name in('김','이','박') ;
-- 5) IS NULL \ IS NOT NULL
select * from employees where commission is null;
select * from employees where commission is not null;
-- 6) LIKE 검색
-- 일부가 비슷한 내용을 검색(속도가 느리다.)
-- where [컬럼명] like '%[문자열]%'
-- ze% -> ze로 시작하는...
-- %com -> com으로 끝나는...
-- %se% -> se를 포함하는
-- %s%e% -> s 또는 e 를 포함하는...
select * from employees where email like 'ze%';
select * from employees where email like '%com';
select * from employees where email like '%se%';
select * from employees where email like '%s%e%';
-- 7) order by
-- 특정 칼럼을 기준으로 정렬 ASC(오름차순) | DESC(내림차순) / 올라가는 사다리 - 작은게 위로
-- salary 가 많은 순으로...
select *from employees order by salary desc;
-- family_name을 가나다 순으로 ...
select * from employees order by family_name asc;
--연봉이 높은 순으로 정렬
select *from employees order by salary desc;
select emp_no ,first_name ,family_name ,salary*12 as annsal from employees order by annsal desc;
-- 다중정렬 (1차 정렬 후 ㄷ오률의 데이터에서 2차 정렬 진행.)
select * from employees order by family_name, salary desc ;-- (1. 가나다 순 + 2. 샐러리 //아까 distint랑은 다르네)
-- 급여(salary)가 200만원 이상인 사람을 family_name 순으로 오름 차순.
-- order by 의 위치 (맨뒤로)-> 정렬은 알고리즘이 많이 필요함.
-- 데이터를 뽑고나서 정렬하는 것이 정렬하고 데이터를 추리는 것보다 편하다.
select * from employees where salary >= 2000000 order by family_name asc;
-- 8) GROUP BY
-- 데이터를 특정한 컬럼을 중심으로 묶어서 가져오는 것
-- SELECT [컬럼, 컬럼, ...]FROM [테이블]GROUP BY [묶어줄 기준 컬럼]
select * from employees;
-- dev_001 -> dev001
update employees set depart_no = 'dev001' where depart_no = 'dev_001';
-- depart_no 을 기준으로 묶어서 가져오기
-- 묶인 대상 컬럼, 연산(합계, 평균, 집계,...)된 내용
-- 부서번호, 부서 급여 합계
select depart_no,sum(salary) from employees group by depart_no ;
--부서번호, 부서 급여 평균
select depart_no,avg(salary) from employees group by depart_no ;
-- 부서 번호, 급여 합계sum(), 팀원 수count()
select depart_no,
sum(salary) as 급여합계,
count(depart_no) as 사원수
from employees group by depart_no ;
-- 9)HAVING
-- group by로 부터 얻어온 결과를 특정 조건으로 추출하는 것
-- HAVING에는 별칭을 사용하지 말자.(특정 db 에서만 먹히지 않는다.)
-- group by 해온 데이터 중에서 sum (salary) 가 5000만원을 넘는 데이터만 보여줘
select depart_no, sum(salary) as sal_sum
from employees group by depart_no having sal_sum > 50000000;
-- 급여 합계가 1000만원 이상인 팀들만 조회 (합계 급여가 큰 순으로 ...)
select depart_no, sum(salary) as sal_sum
from employees group by depart_no having sum(salary) >= 10000000
order by sal_sum desc ;
1. 데이터 조회
+산술표현 : salary / 10000
+as : 별칭 짓기
select first_name, family_name, salary/10000 from employees;
-- as를 사용하면 컬럼에 별칭을 부여 할 수 있다.
select first_name, family_name, salary*12 as 연봉 from employees;
+ concat
select concat(family_name,first_name)as name,
concat(truncate(salary/10000,0),'만원')as 월급 from employees;
2. 특정 조건 조회
and
-- 2-1) and 조건
-- 급여가 100만원 이상이고, 500 만원 이하인 사원 찾기
select * from employees where salary >= 1000000 and salary <=5000000;
or
-- family_name 이 김이거나 급여가 200000인 사람 찾기
select * from employees where salary >= 2000000 or family_name = '김' ;
between
where salary between 50000 ans 40000 ;
3. 중복 제거(distinct)
- 메뉴얼 상에는 1개 이상이 가능하다고 하지만, 1개 이상 넣지 않는게 좋다.
- 중복 제거는 1개 컬럼을 대상으로 하는 것이 가장 정확 (distinct를 다시 거는게 나음)
select distinct family_name from employees;
select distinct family_name , salary from employees;
-> 패밀리 네임의 중복 제거와, 샐러리 중복제거를 한꺼번에 해버림
4. in
공통 컬럼으로 여러 값을 or 조건으로 사용함으로 간략화 시키는거
1. 속도가 우월함
2. 컬럼이 공통되야 함.(family , salary, first name)등등으로 나눠져 있으면 안됨
select * from employees where family_name = '김' or family_name = '박' or family_name = '이' ;
select * from employees where family_name in('김','이','박') ;
5. in null \is not null
select * from employees where commission is null;
select * from employees where commission is not null;
6. like 검색
1) ze% -> ze로 시작하는
2) %com -> com으로 끝나는
3) %se% -> se를 포함하는
4) %s%e% -> s 또는 e를 포함하는 (이건 너무 많은 정보를 보여줘서 비추임!)
select * from employees where email like 'ze%';
select * from employees where email like '%com';
select * from employees where email like '%se%';
select * from employees where email like '%s%e%';
7. order by -asc(오름차순), desc(내림차순)
-- 다중정렬 (1차 정렬 후 ㄷ오률의 데이터에서 2차 정렬 진행.)
** 이거는 distict 처럼 겹치지 않고 잘 한다.
order by는 마지막에 하는게 효율적이다!!!
(마지막에 다하고 정렬하는거니까)
8. group by
- 특정한 칼럼을 묶어서 가져오는것
필요 : 1. 묶인 대상 컬럼 + 2. 연산된 내용
-(보통 칼럼을 묶어서 가져오는게 낫다)
* 1.묶인 대상 컬럼*
depart_no가 1~5 까지 100명의 사람들이 있을거 아니야
1.그러러면 그 사람들의 depart_no 를 1~5 까지 모으는데
2.기준은 월급의 합계로 잡는다면
그룹 별로 월급의 총 합을 알 수 있겠지
+ 연산된 내용
sum, avg, count 사용 가능
select depart_no,sum(salary) from employees group by depart_no ;
9. having
-group by로 얻어온 특정 조건 추출 하는것
** having 에는 별칭을 사용하지 말자
그 뭐 합계 낸거에서 조건 더 걸고 싶을꺼 아녀, 그럴 때 사용(뭐 5천만원 넘는 팀들 고르기)
select depart_no, sum(salary) as sal_sum
from employees group by depart_no having sal_sum > 50000000;