<SELECT>
=원하는 열 선택하여 보기
selection=원하는 행 선택
projection = 원하는 열 선택
join= 다른 테이블과 링크 되어 데이터를 가져온다
🍏 SELECT 기본 구조
select
[distinct]
*|columnName [as nickname],
from
table name | view name | subquery
where
행 제한 조건
group by
열이름|표현식|열인덱스
having
그룹행제한 조건식
order by
열 이름 | 열별칭 | 열인덱스
🍏실행 순서
= from → where → group by → having → select → order by
🍏예제
--print specific column
select first_name, last_name, salary from employees;
--print every column
select * from employees;
--you can choose the order of the columne
select department_id, location_id from departments;
select location_id, department_id from departments;
--string,date=left align / nuber=right align
--column heading = capital letter
🍏NULL
= 값이 없는 값.
🍏 alias (열 별칭)
= 열 헤딩 이름 변경 ( as 넣는 것이 좋다)
select first_name as 이름, salary 급여 from employees;
select first_name "Employee Name", salary*12 "Annual Salary" from employees;
🍏 literal
문자은 작은 따옴표, 문자열 연결은 | | 사용
select first_name||' '||last_name|| '''s salary is $' || salary as "Employess Details"
from employees;
🍏 DISTINCT
중복된 행 제거. select절에 사용
select distinct department_id from employees;
🍏 ROWID
= 데이터베이스 행의 주소.
🍏 ROWNUM
=select구문을 실행 한 후 순서대로 번호를 매긴 뒤 행의 번호를 출력
(order by는 적용되지 않음)
select rowid,rownum, employee_id, first_name from employees;
+) 주석 = --
+) 실행 단축키 = ctrl + enter
<데이터 제한>
WHERE
=선택 되는 행 제한
🍏 문자 스트링과 날짜
=' '로 둘러써여 있다.
select first_name, last_name, hire_date
from employees
where last_name='King';
🍏 비교 연산자
= | 같다 |
> | 보다 크다 |
>= | 보다 크거나 같다 |
< | 보다 작다 |
<= | 보다 작거나 같다 |
<>, != | 같지 않다 |
🍏 BETWEEN
= 값 범위 지정 가능
between A and B : A이상 B이하
select first_name, salary
from employees
where salary between 10000 and 12000;
🍏 IN
=목록중 하나라도 같다면 반환
select employee_id,first_name, salary, manager_id
from employees
where manager_id in(101,102,103);
select first_name, last_name, job_id, department_id
from employees
where job_id in ('IT_PROG','FI_MGR','AD_VP');
🍏 LIKE
=검색에 사용
% | 문자가 없거나 하나 이상 |
_ | 하나의 문자 |
\ | _ 자체가 인식도록 사용 |
select first_name, last_name, job_id, department_id
from employees
where job_id like 'IT%';
select first_name, email from employees where email like '_A%';
select first_name, job_id from employees
where job_id like 'SA\_M%' escape '\';
🍏 IS NULL
=NULL값인지 확인.
IS NOT NULL
=NULL값이 아닌지 확인
🍏 논리 연산자
AND = 둘다 참이면 TRUE
OR = 둘 중 하나가 참이면 TRUE
NOT = 반대 결과 return
우선순위 : NOT>AND>OR
select first_name, job_id,salary from employees
where job_id='IT_PROG'
or job_id='FI_MGR'
and salary>6000;
<데이터 정렬>
ORDER BY
=행 정렬 기능
가장 마지막에 위치한다.
ASC : 오름차순 / DESC : 내림차순
select first_name, hire_date
from employees
order by hire_date DESC;
🍏 열 별칭을 사용한 정렬
select first_name, salary*12 as annsal
from employees order by anssal;
🍏 열 순서 사용한 정렬
select first_name, salary*12 as annsal
from employees order by 2;
'개발 > database' 카테고리의 다른 글
[DB] JOIN (1) | 2024.12.17 |
---|---|
[DB] 분석 함수 (2) | 2024.12.17 |
[DB] GROUP BY (0) | 2024.12.17 |
[DB] 함수 (0) | 2024.12.16 |
[DB] 데이터베이스란 (0) | 2024.12.16 |