본문 바로가기

Database24

[SQL] 테이블에 데이터 추가하기 1. 모든 컬럼의 데이터를 빠짐없이 추가할 때 insert into employee values(100, '홍길동', '800101-1234567', 'gildong@gmail.com', '01055556666'); insert into {tablename} values(열1값, 열2값 ...); 모든 컬럼의 값을 추가할 때는 컬럼명을 입력할 필요가 없다. 2. 특정 컬럼에만 데이터를 추가할 때 3. 여러 테이블에 동시에 추가할 때 insert all into emp_dept_d1 values(emp_id, emp_name, dept_code, hire_date) into emp_manager values(emp_id, emp_name, manager_id) select emp_id, emp_name, .. 2021. 3. 10.
[오라클] 순위 가져오기 중복된 순위 갯수만큼을 다음 행의 순위에 더해주기 - RANK() OVER select emp_name, salary * 12 as "연봉", RANK() over (order by salary desc) as "순위" from employee; 3순위 동점자가 3명일 경우, 순위는 1, 2, 3, 3, 3, 6, 7, 8 ... 이 된다. 실행 결과 중복된 순위가 있더라도 다음 순위는 1만 더해주기 - DENSE_RANK() OVER select emp_name, salary * 12 "연봉", DENSE_RANK() over (order by salary desc) "순위" from employee; 3순위 동점자가 3명일 경우, 1, 2, 3, 3, 3, 4, 5, 6 ... 이 된다. 실행 결과 2021. 3. 10.
[오라클] 테이블 합치기 1. 테이블 합치기 - JOIN (INNER JOIN) select emp_id, emp_name, dept_code, dept_title from employee join department on (dept_code = dept_id); employee테이블의 dept_code와 department테이블의 dept_id 두 컬럼의 값이 같으므로 합쳐서 출력한다. 이 때 합치는 기준컬럼이 두 테이블 중 한 쪽이라도 null인 행은 출력되지 않는다. 2021. 3. 9.
[오라클] 그룹 / 집합 함수 1. 여러 그룹별로 나누어 출력하기 - GROUP BY select dept_code, sum(salary) from employee group by dept_code; 직원들의 salary 합계를 dept_code 별로 나누어서 계산한다. dept_code가 겹치는 행이 없어진다. 실행결과 2. 각 그룹별 집계를 함께 출력하기 - ROLLUP select dept_code, sum(salary) from employee group by rollup(dept_code, job_code) order by 1; 실행결과 3. 중간집계를 모두 모아서 출력하기 - CUBE select dept_code, sum(salary) from employee group by cube(dept_code, job_code).. 2021. 3. 9.
[오라클] 데이터 변환 함수 1. NULL 값을 다른 값으로 변환 - NVL select nvl(null, 0), nvl(null, '없음') from dual; 1번 인자의 값이 null일 경우, 2번 인자로 출력한다. 실행 결과 2. 여러 값 중 일치하는 값 선택 - DECODE select decode(substr('점심메뉴는 삼겹살 입니다', 7, 3), '삼겹살', '육류', '고등어', '어류') as "삼겹살 입력", decode(substr('점심메뉴는 고등어 입니다', 7, 3), '삼겹살', '육류', '고등어', '어류') as "고등어 입력" from dual; DECODE ( 값, A, a, B, b ) '값'이 A면 a를 출력하고, B면 b출력한다. 실행결과 3. 복수 선택값 - CASE select cas.. 2021. 3. 8.