-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql practice.txt
175 lines (158 loc) · 3.05 KB
/
sql practice.txt
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
4-2
select distinct job from emp;
4-3
select
empno as employee_no,
ename as employee_name,
mgr as manager,
sal as salary,
comm as commission,
deptno as department_no
from emp
order by deptno desc, ename asc;
5-1
select *
from emp
where ename like '%S';
5-2
select
empno, ename, job, sal, deptno
from emp
where deptno = 30
and job = 'SALESMAN';
5-3
select
empno, ename, sal, deptno
from emp
where deptno between 20 and 30
and sal > 2000;
--------------------------
select
empno, ename, sal, deptno
from emp
where deptno in (20, 30)
and sal > 2000;
---------------------------
select
empno, ename, sal, deptno
from emp
where deptno = 20
and sal > 2000
union
select
empno, ename, sal, deptno
from emp
where deptno = 30
and sal > 2000;
5-4
select *
from emp
where not sal >=2000
union
select *
from emp
where not sal <=3000;
5-5
select ename, empno, sal, deptno
from emp
where ename like '%E%'
and deptno = 30
and not sal between 1000 and 2000;
5-6
select *
from emp
where comm is null
and mgr is not null
and job in ('CLERK', 'MANAGER')
and ename not like '_L%';
6-1
select
empno,
rpad(substr(empno,1,2), length(empno), '*') as masking_empno,
ename,
rpad(substr(ename,1,2), length(ename), '*') as masking_ename
from emp
where length(ename) >= 5 and length(ename) < 6 ;
6-2
select
empno,
ename,
sal,
trunc(sal/21.5, 2) as day_pay,
round(sal/21.5/8, 1) as time_pay
from emp;
6-3
select
empno,
ename,
hiredate,
to_char(next_day(add_months(hiredate, 3) ,'monday'), 'YYYY-MM-DD') as r_job,
nvl(to_char(comm), 'N/A') as comm
from emp;
6-4
select
empno,
ename,
mgr,
case
when mgr is null then '0000'
when substr(mgr, 1, 2) = 75 then '5555'
when substr(mgr, 1, 2) = 76 then '6666'
when substr(mgr, 1, 2) = 77 then '7777'
when substr(mgr, 1, 2) = 78 then '8888'
else to_char(mgr)
end
as chg_mgr
from emp;
7-1
select
deptno,
trunc(avg(sal)) as avg_sal,
max(sal) as max_sal,
min(sal) as min_sal,
count(*) as cnt
from emp
group by deptno;
7-2
select
job,
count(*)
from emp
group by job
having count(*) >= 3;
7-3
select
extract(year from hiredate) as hire_year,
deptno,
count(*) as cnt
from emp
group by extract(year from hiredate), deptno;
---------------------------------------------------------
select
to_char(hiredate, 'YYYY') as hire_year,
deptno,
count(*) as cnt
from emp
group by to_char(hiredate, 'YYYY'), deptno;
7-4
select
exist_comm,
count(*)
from
(select
case
when comm is null then 'X'
else 'O'
end as exist_comm
from emp)
group by exist_comm;
7-5
select
deptno,
extract(year from hiredate) as hire_year,
count(*) as cnt,
max(sal) as max_sal,
sum(sal) as sum_sal,
avg(sal) as avg_sal
from emp
group by rollup(deptno, extract(year from hiredate));