Day18 Datebase

웰치스·2022년 11월 16일
0

22/11/15

📌 2. Where절과 Order by절

📂 WHERE절(조건절)

🔹 [비교연산자3] In : 다중행비교연산자, 우변에 값리스트가 올 수 있음.

     (=, OR)의 성격을 내포하고 있음.
     우변에 값리스트와 비교해서 하나 이상 동일하면 true를 반환하는 비교연산자.

select employee_id, last_name, salary, manager_id
from employees
where manager_id in (100,101,201); 
-- (==)
select employee_id, last_name, salary, manager_id
from employees
where manager_id = 100 or manager_id = 101 or manager_id = 201; 

select employee_id, last_name, job_id
from employees
where job_id in ('SA_REP','IT_PROG');

🔹 [비교연산자4] Like : 패턴일치 여부를 비교하는 연산자

  • 기호 - 1) % : 0 또는 여러개의 문자
             2) _ : 반드시 1개의 문자
    -- a로 시작되는 문자열 : 'a%'
    -- a가 포함된 문자열 : '%a%'
    -- a로 끝나는 문자열 : '%a'
    -- a로 시작해서 b로 나는 문자열 : 'a%b'
    -- 두번째 문자가 a인 문자열 : '_a%'
    -- 끝에서 세번째 문자가 a인 문자열 : '%a__'
select last_name
from employees
where last_name like '_o%';

select employee_id, last_name, job_id
from employees
where job_id like '%rep';

select employee_id, last_name, salary, hire_date
from employees
where hire_date like '198%';

select employee_id, last_name, salary, hire_date
from employees
where hire_date like '%-09-%';

// where hire_date like '%09%';			
( 9월에 입사한 사람을 찾고 싶은데 '%09%' 이렇게 적으면 
1909-00-00/0000-09-00/0000-00-09 다 포함되서 나옴)

🔹 [비교연산자5] is null : 값이 null인지를 비교해주는 연산자

✍ 우리 회사 사장을 출력!
     (= manager_id가 null인 사람)

select employee_id, last_name, manager_id
from employees
where manager_id is null;

✍ 수당을 받지 않는 사원 출력!
     (= commission_pct가 null인 사람)

select employee_id, last_name, salary, commission_pct
from employees
where commission_pct is null;

🔹 where절에 여러개의 조건문 작성 시 and, or 논리연산자로 연결

  • 우선순위 : and > or
    우선순위 지정을 원하는 경우 괄호 사용해햐 함.
select employee_id, last_name, job_id, salary
from employees
where salary >= 10000
and job_id like '%MAN%';

select employee_id, last_name, job_id, salary
from employees
where salary >=10000
or job_id like '%MAN%';

select employee_id, last_name, salary, job_id, department_id
from employees
where job_id = 'SA_REP'
or job_id = 'IT_PROG'
and salary >= 10000;

// 우선순위 괄호 사용
select employee_id, last_name, salary, job_id, department_id
from employees
where (job_id = 'SA_REP'
or job_id = 'IT_PROG')
and salary >= 10000;
-- (==)
select employee_id, last_name, salary, job_id, department_id
from employees
where job_id in ('SA_REP','IT_PROG')
and salary >= 10000;

💻 [비교연산자 정리]

-- =                        <--> <>, !=
-- >, >=                   <--> <, <=
-- between A and b   <--> not between A and B : A미만 B초과
-- in => (=,or)           <--> not in => (<>, and: 모두와 같지 않아야 함)
-- like                      <--> not like
-- is null                   <--> is not null

select employee_id, last_name, salary, department_id
from employees
where salary not between 5000 and 20000;

select employee_id, last_name, job_id
from employees
where job_id not in ('sa_rep','it_prog','st_man');

select employee_id, last_name, hire_date
from employees
where hire_date not like '19%';

select employee_id, last_name, manager_id
from employees
where manager_id is not null;			-- 사장 빼고 106명 출력

✍ employees테이블로부터 커미션을 받는 사원들의 employee_id, last_name, commission_pct, salary를
    출력하는 구문을 작성하시오.

select employee_id, last_name, commission_pct, salary
from employees
where commission_pct is not null;

📂 order by절(정렬) : 특정 컬럼을 기준으로 정렬된 결과를 출력해줌.

[문법] select * | 컬럼명1, 컬럼명2, 컬럼명3
         from 테이블명
         [where 조건문]
         [order by 컬럼명 | 표현식 | 컬럼alias | 위치표기법 [asc | desc]];

  • 정렬방법 : 1) asc : 오름차순, default(기본값)
                  2) desc : 내림차순
select employee_id, last_name, salary, department_id
from employees
order by salary desc;

select employee_id, last_name, salary, department_id
from employees
order by salary asc;

🔹 order by절에 위치표기법 사용

select employee_id, last_name, salary, job_id, department_id
from employees
where department_id < 80
order by 5 desc;

🔹 다중 컬럼을 기준으로 정렬하기

select employee_id, last_name, salary, job_id, department_id
from employees
order by department_id, salary desc;


✏️ <연습문제>

  1. employees 테이블로부터 2000년도에 입사한 모든 사원의 last_name과 hire_date를 출력하시오.
    (between, like 두개 다 사용하기)
select last_name, hire_date
from employees
where hire_date between '2000-01-01' and '2000-12-31';
-- (==)
select last_name, hire_date
from employees
where hire_date like '2000%';
  1. employees 테이블로부터 커미션을 받지 않는 모든 사원의 last_name, salary, commission_pct를 출력하되 salary를 기준으로 내림차순 정렬하시오.
select last_name, salary, commission_pct
from employees
where commission_pct is null
order by salary desc;

📌 3. HR 스키마 - ERD 만들기

  • 데이터모델링 단계
    1) 요구사항 수집 및 분석
    2) 개념 모델링 -> 피터젠의 ERD(뼈대)
                            테이블, 컬럼, 관계 등이 결정됨.
    3) 논리 모델링 -> 구체화된 ERD(IE표기법, 테이블 차트)
                            데이터타입, 컬럼사이즈, 제약조건 등이 결정됨
    4) 물리 모델링 -> DB에 테이블 구현(create table ----;)

<역모델링>

: 현재 DB에 존재하는 테이블을 기준으로 ERD를 그려주는 기능

  • 왼쪽 상단에 집모양 클릭해서 home으로 > 왼쪽에 돌고래 밑에 메뉴 > Models 옆 >모양 클릭 > [Create EER Model from Database] > stored Connention > Windows > next > next > hr선택 > next >next > 7 Total ~ > execute > next > finish
  • 왼쪽 상단에 집모양 클릭해서 home으로 > 왼쪽에 돌고래 밑에 메뉴 > Models 옆 >모양 클릭 > [Create EER Model from Database] > stored Connention > Windows > next > next > shop db선택 > next >next > stu20 빼기 > execute > next > finish

📌 4. 조인(join)

📂 조인 : 여러 테이블의 문법을 함께 출력하기 위한 문법

[문법] select 컬럼명1, 컬럼명2, 컬럼명3
        from 테이블1 join 테이블2
        on 테이블1.컬럼명 = 테이블2.특정컬럼명 -- 조인조건문

Ex1) employees 테이블과 departments 테이블로부터 전체 사원들의
     employee_id, last_name, salary, departments_id, departments_name을 함께 출력하시오.

desc employees;
desc departments;

select employee_id, last_name, salary, employees.department_id, department_name
from employees join departments
on employees.department_id = departments.department_id;

0개의 댓글

관련 채용 정보