/*
oder by == sriting (정렬) -> 오름,내림
*/
SELECT employee_id, salary
from employees
order by
--테이블 내용을 아래 구문을 사용해서 매번 확인하기 번거로울 때
--왼쪽 카테고리 란에서 원하는 테이블 클릭 , 상단 창에서 objext explorer 옆에 있는 표모양 클릭
--새창 생기면서 테이블 내용들만 확인할 수 있게 된다
select *
from
-- asc: 오름차순 정리
SELECT employee_id, hire_date
from employees
order by hire_date asc;
-- asc: 오름차순 정리 문구는 생략이 가능하다
--출력시 자동으로 오름차순 정리 된다
SELECT employee_id, hire_date
from employees
order by hire_date;
--desc: 내림차순 정리
SELECT first_name, salary
from employees
order by salary desc; -- salary를 내림차순으로 정리해 줘
--IT 부서에 근무하는 사원의 월급을 많이 받는 순으로 정렬하라 -> 내림차순으로 출력하라
SELECT employee_id, first_name, job_id, salary
from employees
where job_id = 'IT_PROG'
ORDER by salary desc; --오더 문구는 항상 맨 아래에 넣기
--월급을 내림차순으로 정리하되, 이름은 오름차순으로 정리
SELECT salary, first_name
from employees
order by salary desc, first_name asc;
--매니저 아이디를 오름차순 기준으로 정렬
--근데 맨 아래에 null 존재
SELECT first_name, manager_id
from employees
order by manager_id asc;
-- null인 경우 제일 처음으로 해서 출력하기
SELECT first_name, manager_id
from employees
order by manager_id asc nulls FIRST;
--역순 정리
--null 이 맨 위로 온다
select first_name, commission_pct
from employees
order by commission_pct desc;
select first_name, commission_pct
from employees
--where commission_pct is not null // nulls last 안 쓰고 같은 상황일 때 where 문 더 자주 사용
order by commission_pct desc nulls last ; --굳이: null 값은 출력 안 함함
--연봉 출력하기
select FIRST_name, salary * 12 as 연봉
from employees
order by salary * 12 desc;
-- select 에서 별명을 연봉으로 설정해서 order에서 연봉이라고 써도 같은 값 출력
select FIRST_name, salary * 12 as 연봉
from employees
order by 연봉 desc;
select FIRST_name, salary * ((12/2)+6) as 연봉
from employees
order by salary * 12 desc;
/*
group by: 그룹으로 묶는 기능. 통계를 위해서
group function : count , sum , avg , max , min ... 표준편차
having : 그룹으로 묶은 후의 조건
distinct : select 절에서만 묶는 데 사용이 가능하다
*/
-- 이렇게 출력하면 몇 번까지 있는지 너무 헷갈린다 이럴 때 묶어서 사용하는 것이 distinct
--distinct 사용 전
select department_id
from employees;
--distinct 사용 후
select distinct department_id
from employees;
--distinct 사용시 order by와의 조합
select distinct department_id
from employees;
order by department_id asc;
--에러에러
--distinct 사용 시 두 번 사용하는 건 불가능 반드시 하나의 카테고리로만 묶을 수 있다
select distinct department_id, distinct job id
from employees;
order by department_id asc;
select distinct distinct job_id
from employees;
order by job_id asc;
--GROUP by 랑 같이 쓰기
select department_id
from employees
GROUP by department_id;
--GROUP by 랑 order by 같이 쓰기
select department_id
from employees
GROUP by department_id
order by department_id asc;
-- group function
SELECT count (employee_id), sum(salary), avg(salary),
max(salary), min(salary)
from employees
where job_id = 'IT_PROG';
SELECT count (employee_id), sum(salary), trunc(avg(salary)),
max(salary), min(salary)
from employees
where job_id = 'IT_PROG';
-- 그룹 바이로 부서번호로 묶고 나서 실행하기
SELECT department_id, count(*), sum(salary), round(avg(salary),1),
max(salary), min(salary)
from employees
group by department_id;
--업무 별로 급여의 합계(sum)가 15000불 이상인 업무 명을 출력하라
--에러에러
select *
from employees
group by job_id
having sum (salary) >= 150000;
-- 업무 (job_id)별로 급여의 합계가 (sum )150000 이상인 사람을 출력
--group by 로 묶었을 떄는 반드시 묶은 칼럼으로만 볼 수 있다
select job_id,sum (salary)
from employees
group by job_id
having sum (salary) >= 150000; --having 절은 묶음에 들어간다
--grou buy 를 반드시 하나로 묶어야 되는 것은 아니다
--20번 부서로 묶은 다음에 그 안에서 따로 묶은 것 20 | "MK_REP" 20 | "MK_MAN"
-- 부서로 묶고 업무로 한 번 더 묶기
SELECT department_id, job_id
from employees
GROUP by department_id, job_id
order by department_id asc;
'PostgresSQL > PostgresSQL 개념정리' 카테고리의 다른 글
| table (0) | 2026.06.17 |
|---|---|
| Join (0) | 2026.06.15 |
| from (0) | 2026.06.12 |
| where (0) | 2026.06.12 |
| [standard function] 문자, 숫자, 날짜, 문자 <->날짜, 원하는 형식, 지정값 대체 (0) | 2026.06.10 |