select 문  = 데이터 검색

 

select [ALL(*) / distinct] // 원하는 컬럼명(열이름),...  // distinct => 중복제거

from 테이블이름

[where 조건]

[group by {컬럼,,,,}]

[having 조건]

[order by {컬럼,,,,,}[asc(오름), desc(내림)]]

 

1. select * from employees; : employees 테이블에서 전체 컬럼을 보여달라 (테이블 전체내용보기)

2. select 필드명, 필드명, 필드명 from 테이블이름 ; 해당 필드명의 자료만 검색

  

 ** 필드명의 자료가  Number, 날짜 인 경우는 연산이 가능하다 

3. select employee_id, first_name, job_id, salary, salary*12 from employees;

 

 ** 필드명을 별칭으로 사용 수 있다. (단, 진짜변경되는 것은 아님)

 ** 별칭 사용법 ( 1. 필드명 as 별칭,  2. 필드명 별칭)

4. select employee_id, first_name, job_id, salary as 급여, salary*12 연봉 from employees;

 

5. select 필드명,,,,, from 테이블명 where 조건절

  => 조건은 대소문자 구별한다. 문자열과 날짜는 반드시 홑따옴표 한다.

  => 조건절의 구성(where 필드명 연산자 조건 )

 select employee_id, first_name, job_id, salary from employees where salary >= 10000 ;

 select employee_id, first_name, job_id, salary, job_id from employees where job_id = 'IT_PROG' ;

 

   and 조건 : (필드명 연산자 조건 and 필드명 연산자 조건)

               주어진 조건을 모두 만족하는 필드만 검색

              ~부터 ~ 까지의 범위를 포함하는 검색 (where 필드명 between 조건 and 조건)

 

select employee_id, first_name, job_id, salary from employees 

where job_id = 'IT_PROG' and salary >=5000 

select employee_id, first_name, job_id, salary from employees 

where employee_id >=150 and employee_id<=180 ;

select employee_id, first_name, job_id, salary from employees 

where employee_id >=100 and employee_id<=180 and job_id ='IT_PROG';

select employee_id, first_name, job_id, salary, hire_date from employees 

where hire_date >= '89/01/01' and hire_date <= '89/12/31';

select employee_id, first_name, job_id, salary, hire_date from employees 

where hire_date  between '89/01/01' and '89/12/31';

select employee_id, first_name, job_id, salary from employees 

where job_id = 'IT_PROG' or salary >=5000 

 

 or 조건 : (필드명 연산자 조건 or 필드명 연산자 조건)

             주어진 조건들 중 하나라도 만족하는 것이 있으면 모두 검색

   (in 연산자 : 같은 필드에서 or 연산자를 사용할 경우 사용) 

      => in  연산자 형식 : where 필드명 in(조건, 조건....)

select employee_id, first_name, job_id, salary from employees

where job_id = 'IT_PROG' or job_id = 'AD_VP' or job_id = 'FI_ACCOUNT' ;

select employee_id, first_name, job_id, salary from employees 

where job_id  in('IT_PROG','AD_VP','FI_ACCOUNT') ;

 

  ** distinct => 중복제거 

 select distinct 필드명 from 테이블이름 , => O (해당필드의 중복제거 됨)

 select distinct 필드명, 필드명 from 테이블이름 => X (중복제거 안됨), 

 select distinct job_id from employees;

 

  ** like 연산자 : 정확하지 않은 데이터를 찾을 때 

     % : 모든(*) , _ : 어떤 한 글자 ( 전체 글자수까지 맞춰어야 한다.) 

  select employee_id, first_name, job_id, salary from employees where first_name like 'J%'; (시작)

  select employee_id, first_name, job_id, salary,hire_date from employees where hire_date like '89%';

  select employee_id, first_name, job_id, salary from employees where first_name like '%i%'; (포함)

  select employee_id, first_name, job_id, salary from employees where first_name like 'J____';

 

  ** is null 과 in not null

   select *  from employees where COMMISSION_PCT = null => 데이터 못 찾음

   select *  from employees where COMMISSION_PCT is null => 데이터가 null인 경우 찾기

   select *  from employees where COMMISSION_PCT is not null => 데이터가 null인 아닌 경우

 

  정렬 : null => 오름차순일경우 맨 마지막, 내림차순일 경우 맨 처음

  select * from employees order by salary => 오름

  select * from employees order by salary asc => 오름

  select * from employees order by salary desc => 내림

 

////////////////////////////////////////////////////////////////////////////////////////////////////

 

dual : 오라클 가상 테이블

sysdate : 시스템의 오늘 날짜

 

숫자함수

 

ABS : 절대값  예) select 10 , -10 , abs(-10) from dual

 

FLOOR : 소수점 아래 버림

        예) select 34.6789, 34.4798, 34.5789,floor(34.6789), floor(34.4798), floor(34.5789) from dual 

 

ROUND : 반올림 (숫자) => 소숫점 첫째자리에서 반올림

        반올림 (숫자, 자릿수) : 

       select round(34.6489,1),round(34.6598,1) from dual // 소숫점 첫째자리까지구함 34.6 // 34.7

       select round(134.6489,-1),round(135.6498,-1) from dual // 130 // 140

 

TRUNC : 버림(숫자, 자릿수)

       select TRUNC(34.6489,1),TRUNC(34.6598,1) from dual // 소숫점 첫째자리까지구함 34.6 // 34.6

       select TRUNC(134.6489,-1),TRUNC(135.6498,-1) from dual // 130 // 130

 

MOD : 나머지 구하기

       SELECT MOD(27,2), MOD(27,5), MOD(27,7) FROM DUAL

       select * from employees where mod(employee_id,2) = 1 ; // 사원번호가 홀수인 사람구하기

 

////////////////////////////////////////////////////////////////////////////////////////////////////

 

날짜 함수

 

sysdate : 시스템 오늘 날짜 , 연산가능

select sysdate, sysdate+100 from dual

 

months_between : 두날짜 사이의 개월 수 구하기

select months_between(sysdate,'96/06/26')/12 from dual

select floor( months_between(sysdate,'96/06/26')/12) from dual

select trunc( months_between(sysdate,'96/06/26')/12) from dual

 

next_day : 해당 요일의 가장 가까운 날짜를 반환(지나간 요일을 찾지는 않음)

next_day(날짜, 요일) : 요일 => 일요일 = 1

select next_day(sysdate,'일요일'),next_day(sysdate,1),next_day('14/08/18',1) from dual

 

last_day : 해당 월의 마지막 날짜 반환

          select last_day(sysdate) from dual 

////////////////////////////////////////////////////////////////////////////////////////////////////

 형변환 함수

  

  NVL : null 값을 다른 값으로 변환 시킴

  NVL(널값, 변환시킬값) 

   select  COMMISSION_PCT, NVL(COMMISSION_PCT,0) from employees

+ Recent posts