반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Inner Join : 일반적 Join */
 
select e.first_name, e.last_name, e.email, d.department_name
from employees e, departments d
where e.department_id = d.department_id;
 
 
 
select emp.first_name, emp.last_name, emp.email, emp.department_id, 
       dep.department_name, job.job_title, loc.city
from employees emp, 
     departments dep,
     jobs job,
     locations loc
where emp.department_id = dep.department_id
  and emp.job_id = job.job_id
  and dep.location_id = loc.location_id
  and loc.city = 'Seattle';
 
 
/* Self Join : 자신의 테이블에서 자신의 칼럼을 Join */
 
select emp1.employee_id, emp1.first_name, emp2.employee_id "상사ID", emp2.first_name "상사 이름"
from employees emp1, employees emp2
where emp1.manager_id = emp2.EMPLOYEE_ID;
 
 
/* 
   Outter Join
   (+) : 두개의 테이블 중 하나의 테이블 컬럼 데이터가 없을 경우 
         ROW가 누락되기 때문에 모든 데이터를 누락없이 출력하기 위해 사용
*/
 
select emp.employee_id, emp.first_name, emp.department_id, dep.department_name, loc.city
from employees emp, departments dep, locations loc
where emp.department_id = dep.department_id (+)
  and dep.location_id = loc.location_id (+);
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] CREATE, ALTER, DROP, TRUNCATE  (0) 2017.08.10
[Oracle SQL] SubQuery 서브쿼리  (0) 2017.08.10
[Oracle SQL] GROUP BY, HAVING, ROLLUP  (0) 2017.08.08
[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 숫자함수  (0) 2017.08.04
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Group by 절 */
 
select distinct(department_id) 
from employees;
 
 
select department_id 
from employees 
group by department_id;
 
 
 
/* 부서 별 급여 합계 */
 
select department_id, sum(salary) 
from employees 
group by department_id;
 
 
 
/* 단일 그룹이 아니므로 에러 */
 
select distinct(department_id), sum(salary) 
from employees; 
 
 
 
/* 부서 별 사원수와 급여 평균 */
 
select department_id, sum(salary), count(salary), avg(salary)
from employees
group by department_id;
 
 
 
/*  부서별 직급별 사원수와 급여 평균 */
 
select department_id, job_id, sum(salary), count(salary), avg(salary)
from employees
group by department_id, job_id
order by department_id, job_id;
 
 
 
select department_id, job_id,
   to_char(sum(salary),'999,999'"총급여",
   to_char(avg(salary),'999,999'"평균급여"
from employees
where department_id = 80
group by department_id, job_id
order by department_id, job_id;
 
 
 
/* 현재 부서 별 사원수 */
 
select department_id, count(*
from employees
where department_id is not null
group by department_id;
 
 
 
/* Where절에서는 집계함수 사용 불가 */
 
select department_id, count(*)
from employees
where department_id is not null
    and count(*< 10
group by department_id;  
 
 
 
/* Having 절 */
 
select department_id, count(*
from employees
where department_id is not null
group by department_id
Having count(*>=10;
 
 
 
/* Rollup : 그룹별 합계 정보를 추가해 보여주는 함수 */
 
select l.city, d.department_name, e.job_id,
  count(*"사원수", sum(e.salary) 총급여
from employees e, departments d, locations l
where e.department_id = d.department_id
  and d.location_id = l.location_id
group by l.city, d.department_name, e.job_id
order by l.city, d.department_name, e.job_id;
 
 
 
select l.city, d.department_name, e.job_id,
  count(*"사원수", sum(e.salary) 총급여
from employees e, departments d, locations l
where e.department_id = d.department_id
    and d.location_id = l.location_id
group by rollup(l.city, d.department_name, e.job_id)
order by l.city, d.department_name, e.job_id;
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] SubQuery 서브쿼리  (0) 2017.08.10
[Oracle SQL] Join  (0) 2017.08.08
[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] 문자 함수  (0) 2017.08.03
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 날짜 함수 */
 
/* sysdate()  : 현재날짜 (system의 날짜) */
 
select sysdate 
from dual; 
 
 
 
/* months_between(date1-최근날짜,date2-이전날짜) */
 
select first_name, last_name, months_between(sysdate,hire_date) 
from employees
where department_id = 50/* 두 날짜 사이의 개월수 차이 (고용일부터 현재까지)*/
 
 
 
/* add_months() */
 
select add_months(sysdate, 4-- 개월수를 더함
from dual; 
 
 
 
/* next_day() */
 
select next_day(sysdate,'일요일'-- 다가올 일요일에 해당하는 날짜
from dual; 
 
 
select next_day(sysdate,'월'
from dual; 
 
 
 
/* last_day() */
 
select last_day(sysdate) -- 해당 달의 마지막 일수 
from dual;
 
 
 
/* to_char() */
 
select to_char(sysdate, 'yyyy-mm-dd'-- 문자열 형태로 출력 
from dual; 
 
 
select to_char(sysdate, 'yy/mm/dd')
from dual;
 
 
 
/* to_date() */
 
select to_date('2015/03/03''yyyy/mm/dd'-- 문자열을 날짜형태로 바꿈 
from dual; 
 
 
 
/* nvl() : null값을 다른 데이터로 변경하는 함수 */
 
select first_name, last_name, nvl(commission_pct, 0) commission 
from employees;
 
 
 
/* decode() : switch문의 역할을 하는 함수 */
 
select department_id, decode(department_id, 20'마케팅부'60'IT'90'경영','부서')
from employees;
 
 
/* case() : else if 문과 같은 역할을 하는 함수 */
 
select first_name, department_id
 
    case when department_id = 20 then 'marketing'
         when departmnet_id = 60 then '전산실'
         when department_id = 90 then '경영'
         else 'Others'
         and "부서명"
 
 from employees;
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] Join  (0) 2017.08.08
[Oracle SQL] GROUP BY, HAVING, ROLLUP  (0) 2017.08.08
[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] 문자 함수  (0) 2017.08.03
[Oracle SQL] Select 문 And,OR, NOT, BETWEEN  (0) 2017.07.29
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Number function */
/* 
    dual : 오라클에서 제공되는 dummy table, 테이블이 없을 때 사용 
    all 
    distinct : 중복제거
*/
 
 
/* abs() : 절대값 */
 
select abs(-23
from dual;
 
 
 
/* sign() */
 
select sign(23), sign(-23), sign(0
from dual;
 
 
 
/* round(n1,n2) : 반올림 */
 
select round(0.123), round(0.543), round(0.125
from dual;
 
 
select round(0.123456789,6), round(0.543465,2), round(0.125254,4
from dual;
 
 
 
/* trunc(n1,n2) : 소수점 자르기 */
 
select trunc(1234.1234567) zero 
from dual;
 
 
select trunc(1234.1234567,0
from dual;
 
 
select trunc(1234.1234567,2
from dual;
 
 
select trunc(1234.1234567,-1
from dual;
 
 
 
/* ceil() : 올림 */
 
select ceil(32.8) ceil 
from dual;
 
 
select ceil(32.3) ceil 
from dual;
 
 
 
/* floor() : 내림 */
 
select floor(32.8) floor 
from dual;
 
 
select floor(32.3) floor 
from dual;
 
 
 
/* power(n1,n2) : 제곱 */
 
select power(4,2) power 
from dual;
 
 
 
/* mod(n1,n2) : 나머지 */
select mod(7,4) mod 
from dual;
 
 
/* sqrt() : 제곱근 */
select sqrt(2), sqrt(3
from dual;
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] GROUP BY, HAVING, ROLLUP  (0) 2017.08.08
[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 문자 함수  (0) 2017.08.03
[Oracle SQL] Select 문 And,OR, NOT, BETWEEN  (0) 2017.07.29
[Oracle SQL] 토드 설치  (0) 2017.07.29
반응형





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Character function */
 
/* concat(char1, char2) or || : 문자열 연결*/
 
select concat('Hello','Bye'), concat('Good','Bad'
from dual;
 
 
select 'Hello'||'Bye''Good'||'Bad' 
from dual;
 
 
 
/* initcap(char) : 첫문자 대문자 변환 */
 
select initcap('good mornig'
from dual;
 
 
select initcap('good/bad mornig'
from dual;
 
 
 
/* Lower(char) : 소문자로 변환 */
 
select lower('GOOD'
from dual;
 
 
 
/* Upper(char) : 대문자로 변환 */
 
select upper('good'
from dual;
 
 
 
/* Lpad(expr1,n,expr2) : expr1을 n만큼 왼쪽으로 늘려서 반환 */
 
select lpad('good',6"LAPD1",
       lpad('good',6,'@'"LPAD2",
       lpad('good',8,'L'"LPAD3"
from dual;
 
 
 
/* 
   Rpad(expr1,n,expr2) : expr1을 n만큼 오른쪽으로 늘려서 반환 
   한글은 한글자를 2byte로 계산하여 나머지를 늘임
*/
 
select rpad('good',6"RPAD1",
       rpad('good',7,'#'"RPAD2",
       rpad('good',8,'L'"RPAD3",
       rpad('안녕',7,'#'"RPAD4"
from dual;
 
 
 
/* Ltrim(char,set) : char에서 set으로 지정된 물자를 왼쪽에서 제거 */
 
select ltrim('goodbye'
from dual;
 
 
select ltrim('goodbye',' '
from dual;
 
 
select ltrim('goodbye','g'), ltrim('goodbye','o'), ltrim('goodbye','go'
from dual;
 
 
 
/* Rtrim(char,set) : char에서 set으로 지정된 물자를 오른쪽에서 제거 */
 
select rtrim('goodbye'
from dual;
 
 
select rtrim('goodbye','go'
from dual;
 
 
select rtrim('goodbye','e'), rtrim('goodbye','ye'
from dual;
 
 
/* Substr(char,pos,leng) : 문자열 일부 반환 */
 
select substr('good morning john',8,4
from dual;
 
 
select substr('good morning john',8
from dual;
 
 
select substr('good morning john',-4
from dual;
 
 
select substr('good morning john',-4,2/*pos가 음수이므로 문자열 끝에서부터 2자리 가져옴*/
from dual; 
 
 
select substr('good morning john',-4,0)  /*length가 0이므로 결과 없음*/
from dual;
 
 
 
/* Substrb(char,pos,byte) : 문자열 일부 반환 */
 
select substrb('good morning john',8,4)  /*byte 수 만큼 가져옴*/
from dual;
 
 
 
/* Replace(char,src_str,rep_str) : 문자열 일부를 대체해서 반환 */
 
select replace('good morning Tom','morning','evenning'
from dual;
 
 
 
/* Translate(expr,frm_str,to_str) : 문자자체를 일대일로 대응, 변환 */
 
select replace('You are not alone''You''We') replaces, 
/*결과 We are not alone*/
       translate('You are not alone''You''We')translates  
/*결과 We are net alene : 문자'o'를 'e'로 변환*/
from dual;
 
 
/* Trim([Leading,Trailing,Both],[trim_chr][FROM]trim_src) : 
trim_src에서 trim_chr을 앞,뒤 혹은 모두에서 제거한 결과 반환 */
 
select trim(leading from 'good'
from dual;
 
 
select length(trim(leading from ' good ')) 
from dual; /*앞(왼쪽)의 공백 삭제하므로 결과 5출력*/
 
 
select length(trim(trailing from ' good ')) 
from dual; /*뒤(오른쪽)의 공백 삭제하므로 결과 5출력*/
 
 
select length(trim(both from ' good ')) 
from dual; /*양쪽의 공백 삭제하므로 결과 4출력*/
 
 
select trim(leading 'g' from 'good'
from dual; /*왼쪽의 g를 삭제하므로 결과 ood출력*/
 
 
select trim(trailing 'd' from 'good'
from dual; /*오른쪽의 d를 삭제하므로 결과 ood출력*/
 
 
select trim(both 'g' from 'good'
from dual; /*양쪽의 g를 삭제하므로 결과 ood출력*/
 
 
 
/* ASCII(char) : ASCII코드 값 반환 */
 
select ascii('A'
from dual;
 
 
/* Instr(string,src_str,pos,occur) : 특정 문자열의 위치를 찾아 반환 */
 
select instr('good morning john','or',1
from dual;
 
 
select instr('good morning john','n'1,3
from dual;
 
 
 
/* Length(char) : 문자열 길이 반환 */
 
select length('good'
from dual;
 
 
  cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] 날짜함수  (0) 2017.08.06
[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] Select 문 And,OR, NOT, BETWEEN  (0) 2017.07.29
[Oracle SQL] 토드 설치  (0) 2017.07.29
[SQL] 기초함수 예제  (0) 2017.07.07
반응형






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
select * from tab;
 
 
select * from employees;
 
 
 
select employee_id, last_name 
from employees 
where last_name= 'Smith';
 
 
desc employees; 
 
 
select employee_id, salary 
from employees 
where last_name='Smith';
 
 
 
select employee_id, first_name, salary 
from employees 
where last_name='Smith';
 
 
 
select employee_id as "종업원", salary as "SAL" 
from employees;
 
 
 
select employee_id "ID", salary "SAL" 
from employees;
 
 
 
select employee_id "종업원", salary "연봉" 
from employees 
where last_name='Smith';
 
 
 
select distinct JOB_ID 
from employees;
 
 
 
select first_name, last_name, salary 
from employees 
where salary >= 5000;
 
 
 
select first_name, last_name, hire_date 
from employees 
where hire_date >= '04/01/01';
 
 
 
/* AND */
 
select first_name, last_name, job_id, department_id 
from employees
where department_id = 50 and job_id = 'SH_CLERK';
 
 
 
select first_name, last_name, department_id, manager_id 
from employees
where department_id = 50 and manager_id = 124;
 
 
 
/* OR */
 
select first_name, last_name, department_id, manager_id 
from employees
where department_id = 50 or manager_id = 124;
 
 
 
 
/* NOT */
 
select first_name, last_name, job_id, department_id 
from employees
where not (department_id =50);
 
 
 
select first_name, last_name, job_id, department_id 
from employees
where department_id <>40;
 
 
 
/* salary 4000 ~ 8000*/
select first_name, last_name, salary 
from employees 
where salary >= 4000 and salary <= 8000;
 
 
 
/* BETWEEN */
select first_name, last_name, salary 
from employees 
where salary between 4000 and 8000;
 
 
 
/* salary 4000 ~ 8000 END*/
select first_name, last_name, salary 
from employees 
where salary = 6500 or salary = 7700 or salary = 13000;
 
 
 
select first_name, last_name, salary 
from employees 
where salary in (6500770013000);
 
 
select first_name, last_name 
from employees
where first_name like 'D%';
 
select first_name, last_name from employees
where first_name like '%d';
 
cs


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] 문자 함수  (0) 2017.08.03
[Oracle SQL] 토드 설치  (0) 2017.07.29
[SQL] 기초함수 예제  (0) 2017.07.07
[SQL] 기초함수 1  (0) 2017.07.06
반응형







1. 토드월드 접속

https://www.toadworld.com/ 


2. downloads 접속


3. Toad for Oracle 선택


4. OS에 맞는 것 선택


5. Download


====

1) Equal Join (두 개의 테이블 데이터 row가 동일 할 경우 사용)


select e.ename, d.dname

from emp e, dept d

where e.deptno = d.deptno;




====


select * from dept;

=


select * from emp where deptno=10;

=



2) Non-Equal Join (범위에 대한 Join)


select * from salgrade;

=


select ename, sal from emp where rownum <5;

=


select e.ename, e.sal, s.grade

from emp e, salgrade s

where e.sal between s.losal and s.hisal;


두 개의 테이블 데이터 중 where이하에 있는 e.sal의 데이터 값이 s.losal과 s.hisal의 범위에 알맞은 s.grade를 출력하라는 의미


=====


3) Outer Join (두개의 테이블 row가 같지 않을 경우 사용)


select e.ename, d.deptno, d.dname from emp e, dept d

where e.deptno (+) = d.deptno;


테이블 row가 부족한 부분에 (+)를 붙임


=====


4) Self Join (하나의 테이블 내에서 두 개의 컬럼데이터가 동일한 데이터를 가지고 있을 때 사용)


select empno, ename, job, mgr from emp;

=


select a.ename || '의 상사는 ' || b.ename ||' 이다. ' as 직속상사관계

from emp a, emp b

where a.mgr = b.empno;



===

(연습문제)

select * from emp;

select * from dept;


1. 사원 테이블에서 사원번호, 사원명, 부서번호 출력 ( 5개)

select empno, ename, deptno from emp where rownum < 6;


2.

select empno, ename, deptno from emp order by sal desc;


3.

select empno, ename, comm from emp where job='SALESMAN' and comm is not null;


4.

select ename, sal, hiredate from emp;


5.

select ename, sal, to_char(hiredate,'yyyy-mm-dd') hh from emp;


6.

select empno, ename, deptno from emp where to_char(hiredate,'yyyy') = '1980'


7.

select ename||'('||empno||')' from emp where ename='SMITH'


8.

select e.empno, e.ename, d.dname, e.hiredate from emp e join dept d on e.deptno = d.deptno


9.

select * from emp where deptno = (select deptno from emp where ename='SMITH')


10.

select * from emp where deptno = (select deptno from emp where ename='SMITH') and sal>(select sal from emp where ename='SMITH') 


11.

select ename,deptno,sal from emp where sal = (select max(sal) from emp where deptno = (select deptno from emp where ename='SMITH'))



12.

select b.* from 
    (select rownum rn, a.* from 
        (select ename, sal from emp order by sal desc) a
    ) b
where rn <=5;


13.
select b.* from 
    (select rownum rn, a.* from 
        (select ename, sal from emp order by sal desc) a
    ) b
where 4<rn and rn<11;


14.
select ename from emp where ename like '%S$_%' escape '$';

=======


select ename,sal from emp where rownum < 5;

select ename,sal from emp order by sal desc;

select rownum, ename, sal from emp;

select rownum, ename, sal from emp where rownum <= 5;

select rownum, ename, sal from emp where rownum <5 order by sal desc;

select rownum, ename, sal from emp order by sal desc;

select rownum, ename, sal from emp where rownum >2 ;


select rownum rn, a.* from (select rownum rn, ename, sal from emp) a where rn > 2;


반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] 숫자함수  (0) 2017.08.04
[Oracle SQL] 문자 함수  (0) 2017.08.03
[Oracle SQL] Select 문 And,OR, NOT, BETWEEN  (0) 2017.07.29
[SQL] 기초함수 예제  (0) 2017.07.07
[SQL] 기초함수 1  (0) 2017.07.06
반응형

세상에 나쁜개는 없다 시즌 2

버려진 개 생명




오늘은 참 마음아픈 이야기네요

저도 임신한상태에서 학대받은 유기견을 발견해 키우고 있는 입장에서 

유기견 소식을 접할때마다 정말 마음이 너무 아픕니다

제발 약한생명을 학대하고 유기하는 사람들이 없어졌으면 좋겠어요








생명이가 얼른 건강을 회복해서 좋은 가족 만나길 바래요

반응형

+ Recent posts