-
728x90
SQL 기초1
select 문 작성
select는 표시할 열을 식별한다.
from은 이러한 열을 포함한 테이블을 식별한다
select * from departments; //*은 전체를 보여달라는 의미이다. select department_id, location_id from departments; //특정열 선택도 가능연산자
-산술연산자 select last_name, salary, salary + 300 from employees; //월급에 300 추가한 결과값 출력 -연산자 우선순위 select 12*salary+100 <===========>select 12*(salary+100) //괄호 먼저 실행 -날짜에 산술 연산자 사용 select (sysdate-hiredate)/7 as weeks //고용날부터 현재 날짜까지 몇 주가 지났는지 출력 // 'weeks'를 엘리어스라고 칭함 -연결 연산자(||) ||를 사용하여 문자열을 다른 열과 연결한다 select last_name||'is a' || job_id 출력값:Abel is a SA_REP 여기서 is a 를 리터러 문자열이라고 칭함Null값 정의
- Null은 사용할 수 없거나, 할당되지 않았거나, 알 수 없거나, 적용할 수 없는 값
- Null은 0이나 공백과는 다름
- Null값과 산술연산자 적용시 Null값으로 출려된다(알 수 없기 때문)
WHERE절사용
select employee_id, last_name, job_id, department_id from employees where department_id = 90; - 부서번호가 90번이라는 조건을 걸어 줌 select last_name from employees where last_name = 'Whalen' and hire_date = '17-OCT-03' - 조건절에 문자열 및 날짜 값을 작은따움표로 묶는다비교연산자
연산자 의미 > 보다 큼 >= 보다 크거나 같음 < 보다 작음 <= 보다 작거나 같음 between and 두 값 사이 in 갑 리스트 중 리치하는 값 검색 like 일치하는 문자 패턴 검색 is null null 값인지 여부 --- //between and 사용 select last_name, salary from employees where salary between 2500 and 3500 //in 사용 select employee_id from employees where manager_id in (100, 101, 201); //Like 연산자 이용 select first_name from employees where first_name like 'S%'; //S로 시작하는 이름을 출력 where last_name like '_O%'// 이름의 두번째 글자가 'O'인정보출력 where manager_id is null;//manager_id가 null인 정보 출력논리 연산자(AND, OR, NOT)
AND 연산자 // 두 구성 요소 조건이 모두 참이어야 함 select employee_id, last_name, job_id, salary from employees where salary >= 10000 and job_id like '%MAN%' //salary가 10000보다 같거나 크면서 job_id에 MAN이 들어간 정보 출력 OR연산자 where salary >=10000 or job_id like '%MAN%' //salary가 10000보다 보다 크거나 같거나 job_Id에 man이 들어간 정보 출력 NOT연산자 where job_id not in('IT_PROG', 'ST_CLERK','SA-REP') // 괄호안에 포함되지 않는 정보들 출력 -우선순위는 AND가 OR보다 먼저 처리정렬(ORDER BY)
- ASC: 오름차순, 기본값
- DESC: 내림차순
select last_name, job_id, department_id, hire_date from employees order by hire_date; //고용날짜 순으로 정렬 order by department_id DESC; //부서번호 내림차순으로 정렬 select employee_id, last_name, salary*12 annsal from employees order by annsal; //annsal이라는 엘리어스로 정렬 select last_name, job_id, department_id, hiredate from emplloyees order by 3; //숫자 3(department_id)로 정렬 order by department_id, salary DESC;//여러열을 기준으로 정렬