개발/DB(MariaDB)

4. DB - (DML-2, ORDER BY, GROUP BY, HAVING)

oneidsin 2025. 3. 17. 17:17

중복 제거 DISTINCT

-- SELECT DISTINCT [출력할 컬럼, ...] FROM [테이블명];
-- DISTINCT 이후의 컬럼에 대한 중복을 제거

select salary, family_name from employees;
select distinct family_name from employees;

-- 중복 제거의 경우 하나의 컬럼만을 대상으로 하는 것이 가장 효과적이다.
select distinct family_name, salary from employees order by family_name, salary;

 

IN (OR 조건을 대신하여 사용)

-- OR 조건에 사용되는 컬럼들이 전부 동일해야 함
-- SELECT [컬럼, ...] FROM [테이블명] WHERE [컬럼] IN (값, ...);
-- family_name 이 '김' 이거나 '이' 이거나 '박' 인 사람들의 모든 정보를 가져와라

select * from employees where family_name = '김' or family_name = '이' or family_name = '박';

-- 속도가 OR 조건보다 빠르다.
select * from employees where family_name in ('김', '이', '박');

 

IS NULL | IS NOT NULL

select * from employees where commission is null;
select * from employees where commission is not null;

 

LIKE

일부가 비슷한 내용을 검색. 단 속도가 느리다.
-- WHERE [컬럼명] LIKE '%[문자열]%'
-- ze% : ze 로 시작하는
-- %com : com 으로 끝나는
-- %se% : se 를 포함하는
-- %s%e% : s 와 e 를 포함하는

select * from employees where email like 'on%';
select * from employees where email like '%com';
select * from employees where email like '%se%';
select * from employees where email like '%s%e%';

 

ORDER BY

데이터를 특정 컬럼 기준으로 정렬
-- SELECT [컬럼] FROM [테이블] ORDER BY [컬럼][ASC | DESC];
-- ASC : 오름차순(생략가능)
-- DESC : 내림차순

select * from employees order by salary desc;
select * from employees order by family_name asc;

-- 2차 정렬(1차에서 동률일 경우 2차로 정렬)
select * from employees order by family_name asc, first_name asc;

-- family_name 오름차순, salary 내림차순
select * from employees order by family_name asc, salary desc;

-- salary 가 200만원 이상은 사람들을 family_name 순으로 오름차순 하시오.
-- 데이터를 확보해놓고 정렬해야 한다.
-- DB 에서는 데이터를 확보해놓고 시작하는 것이 속도에 유리하다.
select * from employees where salary >= 2000000 order by family_name asc;

 

GROUP BY

데이터를 특정 컬럼 기준으로 묶어서 가져오는 경우 사용(통계에 많이 사용)
GROUP BY 를 할 때 사용하는 집계 함수 : SUM, AVG, COUNT
-- SELECT [컬럼], [집계] FROM [테이블명] GROUP BY [그룹핑할 컬럼];

-- dev001 팀의 급여 합산
select depart_no, sum(salary) as total from employees where depart_no = 'dev001';
select depart_no, sum(salary) as total from employees where depart_no = 'dev002';
select depart_no, sum(salary) as total from employees where depart_no = 'dev003';
select depart_no, sum(salary) as total from employees where depart_no = 'dev004';
select depart_no, sum(salary) as total from employees where depart_no = 'dev005';

 

GROUP BY 를 쓰지 않고 급여 합산을 출력하려면 이렇게 부서마다 하나하나 작성하여 확인해야한다.

그러나 GROUP BY 를 쓰면 한번에 묶어서 가져오기 때문에 한 줄로 확인 가능하다.

select depart_no, sum(salary) as total from employees group by depart_no;
select depart_no, avg(salary) as average from employees group by depart_no order by average desc;

 

참고로 위와 같이 GROUP BY 에도 ORDER BY 사용이 가능하다.

 

HAVING

HAVING은 GROUP BY에 조건을 줄 때 사용한다. (WHERE와 비슷)

-- 부서별 급여 합계
select depart_no, sum(salary) as total from employees group by depart_no;
-- 합계가 2500만원 이상만 보고 싶을 경우?
-- HAVING 절에는 가급적 별칭을 쓰지 않는다.(특정 DB 에서는 에러 발생)
select depart_no, sum(salary) as total from employees 
	group by depart_no having sum(salary) > 25000000 order by sum(salary) desc;

 

'개발 > DB(MariaDB)' 카테고리의 다른 글

6. DB - (key)  (0) 2025.03.17
5. DB - (트랜잭션)  (0) 2025.03.17
3. DB - (DML-1)  (0) 2025.03.17
2. DB - (테이블 생성, 수정)  (0) 2025.03.17
1. DB - (유저 생성, 삭제, 권한)  (0) 2025.03.17