-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL_19.text
206 lines (166 loc) · 18.7 KB
/
SQL_19.text
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
SQL> select * from emp
2 where ename=(select * from emp
3
SQL> select * from emp
2 where mgr=(select * from emp
3
SQL> select * from emp
2 where mgr=(select mgr from emp
3 where ename='SCOTT');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
SQL> SELECT * FROM EMP;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> SELECT * FROM EMP
2 WHERE EMPNO=(SELECT MGR FROM EMP WHERE ENAME='SCOTT');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
SQL> SELECT *
2 FROM EMP
3 WHERE MGR=(SELECT EMPNO FROM EMP WHERE ENAME='SCOTT');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
SQL> SELECT * FROM EMP
2 WHERE SAL>AVG(SAL)
3 ;
WHERE SAL>AVG(SAL)
*
ERROR at line 2:
ORA-00934: group function is not allowed here
SQL> SELECT * FROM EMP
2 WHERE SAL > (SELECT AVG(SAL) FROM EMP);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20
6 rows selected.
SQL> SELECT AVG(SAL) FROM EMP;
AVG(SAL)
----------
2073.21429
SQL> SELECT * FROM EMP
2 WHERE SAL>(SELECT SAL FROM EMP
3 WHERE ENAME='BLAKE');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20
SQL> SELECT * FROM EMP
2 WHERE DEPTNO=20,SAL > (SELECT SAL FROM EMP
3 WHERE
4
SQL> SELECT * FROM EMP
2 WHERE DEPTNO=20,SAL > (SELECT AVG(SAL) FROM EMP
3 WHERE DEPTNO=20);
WHERE DEPTNO=20,SAL > (SELECT AVG(SAL) FROM EMP
*
ERROR at line 2:
ORA-00933: SQL command not properly ended
SQL> SELECT * FROM EMP
2 WHERE DEPTNO=20,SAL > (SELECT AVG(SAL) FROM EMP
3 WHERE DEPTNO=20));
WHERE DEPTNO=20,SAL > (SELECT AVG(SAL) FROM EMP
*
ERROR at line 2:
ORA-00933: SQL command not properly ended
SQL> SELECT * FROM EMP
2 WHERE DEPTNO=(SELECT DEPTNO FROM EMP
3 WHERE SAL >(SELECT AVG(SAL) FROM EMP WHERE DEPTNO=20));
WHERE DEPTNO=(SELECT DEPTNO FROM EMP
*
ERROR at line 2:
ORA-01427: single-row subquery returns more than one row
SQL> SELECT * FROM EMP
2 WHERE DEPTNO IN(SELECT DEPTNO FROM EMP
3 WHERE SAL >(SELECT AVG(SAL) FROM EMP WHERE DEPTNO=20));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
14 rows selected.
SQL> SELECT * FROM EMP
2
SQL> SELECT AVG(SAL) FROM EMP
2 WHERE DEPTNO=20;
AVG(SAL)
----------
2175
SQL> SELECT * FROM EMP
2 WHERE SAL>(SELECT AVG(SAL) FROM EMP
3 WHERE DEPTNO=20);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20
6 rows selected.
SQL> SELECT * FROM EMP
2 WHERE DEPTNO=20 AND ( SELECT * FROM EMP
3 WHERE SAL>(SELECT AVG(SAL) FROM EMP
4 WHERE DEPTNO=20));
WHERE DEPTNO=20))
*
ERROR at line 4:
ORA-00936: missing expression
SQL> SELECT * FROM EMP
2 WHERE DEPTNO=20 AND SAL>SELECT * FROM EMP
3
SQL> SELECT * FROM EMP
2 WHERE DEPTNO=20 AND SAL>(SELECT AVG(SAL) FROM EMP WHERE DEPTNO=20);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
SQL> SELECT MAX(SAL) FROM EMP WHERE JOB='CLERK';
MAX(SAL)
----------
1300
SQL> SELECT ENAME FROM EMP
2 WHERE JOB='SALESMAN' AND SAL>(SELECT MAX(SAL) FROM EMP WHERE JOB='CLERK');
ENAME
----------
ALLEN
TURNER
SQL> SPOOL OFF