-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL-Cookbook-Query-Solutions-and-Techniques.sql
265 lines (259 loc) · 5.9 KB
/
SQL-Cookbook-Query-Solutions-and-Techniques.sql
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
--# BOOK: SQL ZAPYTANIA I TECHNIKI DLA BAZODANOWC�W
--## PREPARE ORACLE DB 23C FREE DEVELOPER VM WITH HR SCHEMA
create synonym emp for hr.employees
create synonym dep for hr.departments
;
select *
from dep;
;
select *
from emp
;
drop synonym emp;
--## 1.x
--## 1.9 limit query results
select *
from emp
where rownum < 5
;
--*free practice*
select *
from hr.employees emp
where emp.department_id = 10
or emp.commission_pct is not null
or emp.salary <= 2000 and emp.department_id = 20
;
--## 1.7
/*tip: znaki konkatencji to tak naprawde skrot do funkcji concat*/
WITH NAZWISKA as (
SELECT
first_name AS fname,
last_name AS lname
FROM
emp e
WHERE
e.department_id = 10
OR e.commission_pct IS NOT NULL
OR e.salary <= 2000
AND e.department_id = 20
)
SELECT concat(fname,' ' ,lname) as dane
FROM NAZWISKA N
WHERE n.lname like 'R%'
;
/*case when*/
SELECT
first_name
, last_name
, salary
, case
when salary <=10000 then 'BIEDA'
when salary >=20000 then 'NIEBIEDA'
else 'OK'
end kasa
from EMP
WHERE ROWNUM<15
;
## 1.10 generate random values
select
dbms_random.value() as random
from dual
;
## 1.12 replace null
select
emp.*
,coalesce(cast(emp.manager_id as varchar2(50)),'test') as test_varchar
,coalesce(emp.manager_id,-1) as test_nvl
,coalesce(emp.manager_id, emp.commission_pct, emp.salary,-1) as return_first_notnull_val_or_new_val -- if any passed row columns contain null then return value coalesce(par,par, returnedValue), works like java method with example(args...columnsName, value)
from emp
where manager_id is null
;
## 1.13
desc emp;
select * from emp;
/*pattern 1, % can replace > 1 chars*/
select e.last_name
from emp e
where e.last_name like 'K%'
;
/*pattern 2, precise char number '_' */
select e.last_name, e.first_name
from emp e
where e.last_name like 'K___'
and e.first_name like 'A%'
;
## 2.1 Sort
/*Oldest first*/
select e.last_name, d.department_name
from emp e
join dep d on e.department_id = d.department_id
where e.department_id = 80
order by hire_date desc
;
## 2.2 Sort by several column
/**/
select *
from emp e
order by e.employee_id, e.last_name, e.first_name
fetch first 10 rows only;
## 2.3 Sort by subString last chars - 3
select
e.*
,substr(first_name, length(first_name) - 2, length(first_name))
,substr(first_name, length(first_name) - 2)
from emp e
order by substr(first_name, length(first_name) - 2, length(first_name))
fetch first 10 rows only;
;
## 2.4 Sort by mixed columns (chars + number)
/**/
with tab1 as (
select last_name ||' ' ||employee_id as col
from emp
)
select
t.*
,translate(col,'0123456789','##########') as a1
,replace(
translate(col,'0123456789','##########')
,'#','') as a2
,replace(col, --exampe, given: Ernst 104, Ernst, ''
replace(
translate(col,'0123456789','##########')
,'#',''),'') as a3
,replace('TESTTTT','T') as a4 -- delete all occurance of T
,replace(COL,'e') as a5 -- delete e char
from tab1 t
order by replace(col,
replace(
translate(col,'0123456789','##########')
,'#',''),'')
;
## 2.5 Order and handle null
/*
Default behavior
asc = nulls lasts
desc = nulls first
*/
select *
from emp
order by COMMISSION_PCT asc
;
--oracle
select *
from emp
order by COMMISSION_PCT asc nulls first
;
--workaround, group by null and non null
select *
from
(
select
e.*,
case when e.commission_pct is null then 0 else 1
end as is_non_null
from emp e
)
order by is_non_null, COMMISSION_PCT asc
;
## 2.6
select *
from
(
select
e.*,
case when e.commission_pct is null then 0 else 1
end as is_non_null
from emp e
)
order by is_non_null, COMMISSION_PCT ascselect * from
(
select
e.*,
case when e.commission_pct is null then 0 else 1
end as is_non_null
from emp e
)
order by is_non_null, COMMISSION_PCT asc;
;
## 2.7
-- dynamic order
select
e.*,
case when job_id = 'IT_PROG' then salary || '$'
else 'dep.' || to_char(e.department_id)
end as salary_info_else_department
from emp e
order by case when e.job_id = 'IT_PROG' then e.salary
else e.department_id
end
;
## 3.1 simple union
SELECT to_char(employee_id) FROM emp
UNION ALL
SELECT '----------------' FROM dual
UNION ALL
SELECT to_char(employee_id) FROM emp
;
SELECT to_char(employee_id) FROM emp
UNION
SELECT '----------------' FROM emp
UNION ALL
SELECT to_char(employee_id) FROM emp
;
## 3.2
desc emp;
select e.employee_id, j.job_title
from emp e, jobs j
where e.job_id = j.job_id
and e.employee_id < 110
;
select e.employee_id, j.job_title
from emp e
inner join jobs j on e.job_id = j.job_id
where e.employee_id < 110
;
## 3.3 the same rows in the tables
--free
with q1 as (
select * from emp where job_id = 'IT_PROG' or job_id = 'AD_VP'
)
,q2 as (
select * from emp where job_id = 'IT_PROG')
select *
from q1 q1
join q2 on q1.employee_id = q2.employee_id
;
-- by first and last name
with q1 as (
select first_name, last_name from emp where job_id = 'IT_PROG' or job_id = 'AD_VP'
)
,q2 as (
select first_name, last_name from emp where job_id = 'IT_PROG'
)
select q1.first_name, q1.last_name
from q1 q1
join q2 on (q1.first_name = q2.first_name
and q1.last_name = q2.last_name)
;
--intersect
with q1 as (
select first_name, last_name from emp where job_id = 'IT_PROG' or job_id = 'AD_VP'
)
,q2 as (
select first_name, last_name from emp where job_id = 'IT_PROG'
)
select * from q1
intersect
select * from q2
;
## 3.4 subtraction
with q1 as (
select first_name, last_name, job_id from emp where job_id = 'IT_PROG' or job_id = 'AD_VP'
)
,q2 as (
select first_name, last_name,job_id from emp where job_id = 'IT_PROG'
)
select * from q1
minus
select * from q2