Oracle SQL - Select 개발일지(2)

Nam-Soomin·2022년 8월 26일
0

Oracle SQL

목록 보기
2/7

📌 1. 비교 조건

다른 비교 조건

between ...and... : 두 값 사이(지정한 값 포함)
IN(set) : 값 목록 중의 값과 일치
LIKE : 문자 패턴 일치
is null : 널 값
*is not null


between ...and...

자바에서 다음과 같이 표현한다면,

if(salary >= 2500 && salary <= 3500){}

SQL에서는

SELECT last_name, salary 
from employees 
WHERE salary between 2500 and 3500;

자연어에 가깝게 표현한다.


IN 조건 사용


Like 조건 사용

  • % : 0개 이상의 문자를 의미한다.
    아무것도 없어도 되고, 아무거나 와도 된다.
  • _ : 문자 하나를 나타낸다.

예) S --> 이름이 S(이름이 한글자)
SX--> 이름이 두글자(앞글자 S)
SXX, SXXXX, SXXXXX, SXXXXXXX

where first_name like 'S%';

-->성의 앞글자가 대문자 s인 사원

where first_name like 'S_';

-->성이 반드시 두 글자이고, 첫글자가 대문자 S인 사원

where first_name like '___';

-->성이 세글자인 사원

select last_name, hire_date from employees
where hire_date LIKE '05%';

-->05로 시작하는 모든것. 이 쿼리는 05년도에 입사인 모든 직원을 찾는 것이다.
(oracle에서 작은 따옴표 붙는건 날짜와 문자열. )

*패턴 일치 문자를 결합할 수 있습니다.

select last_name from employees
where last_name like '_o%';

-->이름의 두번째 글자가 소문자 o인 사원

*이름에 %가 있다. 이름에 가 있다.
이름에
가 있는 사원의 월급을 출력해라.

는 Escape 식별자를 이용한다.

또 다른 예시)

select employee_id, last_name, job_id
from employees
where job_id like '%sa\_%' escape '\';

\가 붙어있는 _는 그 자체로 보게된다.


null 조건 사용

관리자가 없는 사원의 이름과 관리자 사번을 찾아라.

where manager_id = null; X

정확한 방법은

WHERE manager is null; O

관리매니저가 없는 사원은?

select last_name from employees where manager_id is null;


논리 조건(연산자)

And : 구성 요소 모두가 true -> true
Or : 구성 요소 중 하나라도 true면 -> true
*Not : 뒤따르는 조건이 false -> true
(is null에는 is not null라고 쓴다. 나머지는 다 연산자 앞에 not사용)


우선순위 규칙

괄호를 사용해서 우선순위를 강제로 지정합니다.


order by 절

  • ASC : 오름차순, 기본값 (생략하면 기본적으로 오름차순)
  • DESC : 내림차순

우리 회사에 입사한 사람중에 가장 최근에 입사한 사람

select last_name, job_id, department_id, hire_date
from employees
order by hire_date;


열별칭을 기준으로 정렬(order by)

예)월급이 작은순에서 큰순으로 정렬
월급이 큰순에서 작은순 (desc)


📝요약)
조건
연산자
정렬


📌 2. 2장의 연습문제

연습2)118p.
1.

select last_name, salary
from employees
where salary>12000;

select last_name, department_id
from employees
where employee_id = 176;



(like는 문자가 맞냐기 때문에, 답이 같아도 맹신하지 말것!)

select last_name, salary from employees
where salary not between 5000 and 12000;

select last_name, job_id, hire_date from employees
where hire_date between '08/02/20' and '08/05/01'
order by hire_date asc;

(4번은 연도를 2008년으로 바꿔서 할 것)

  1. 논리연산자
select last_name, department_id from employees
where department_id between 20 or 50;

또는 IN

select last_name, department_id from employees
where department_id in(20,50);

select last_name "employee", salary "monthly salary" 
from employees
where salary between 5000 and 12000
AND department_id between 20 and 50;
select last_name "employee", salary "monthly salary" 
from employees
where (salary between 5000 and 12000)
AND (department_id in (20,50));

select last_name, hire_date from employees
where hire_date between '04/01/01' and '04/12/31';
select last_name, hire_date from employees
where hire_date like '04%';

select last_name, job_id from employees 
where manager_id is null;

select last_name, salary, commission_pct from employees
where commission_pct is not null
order by salary desc, commission_pct desc;

select last_name from employees
where last_name like'__a%';

select last_name from employees
where last_name like'%a%'
AND last_name like '%e%';

select last_name, job_id, salary from employees
where job_id in ('ST_CLERT', 'SA_REP')
and salary not in (2500, 3500, 7000);

📝문자열 부분은 고유명사이기에, 무조건 써져있는 그대로 대문자면 대문자인대로 쓸 것!

select last_name "employee", salary "monthly salary", commission_pct 
from employees
where commission_pct =.2


📝 개인 메모 ---

  • 다음부터는 정확학 정답 확인에 용이하게 몇건의 데이터가 나왔는지도 기록해두자!

명령 프롬프트 한눈에 보기 편하게 할 때 sqlplus명령어

set linesize (숫자)
  • 그리고 이건 정말 개인적인 일이지만, 나름 개발의 발전으로서 나의 새로운 두번째 기계식 키보드를 손에 넣어 지금 이 글을 작성하였다. 새로운 기계식은 콕스의 갈축!

데이터베이스 3일차

profile
🇰🇷Dreaming Full-Stack WEB Developer

0개의 댓글

관련 채용 정보