-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL_16.text
313 lines (249 loc) · 24 KB
/
SQL_16.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
SQL> SELECT *
2 FROM EMP
3 WHERE HIREDATE=MAX(HIREDATE);
WHERE HIREDATE=MAX(HIREDATE)
*
ERROR at line 3:
ORA-00934: group function is not allowed here
SQL> SELECT MAX(HIREDATE)
2 FROM EMP;
MAX(HIRED
---------
23-MAY-87
SQL> SELECT MIN(HIREDATE)
2 FROM EMP;
MIN(HIRED
---------
17-DEC-80
SQL> SELECT *
2 FROM EMP
3 WHERE HIREDATE=(SELECT MIN(HIREDATE) FROM EMP);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
SQL> SELECT * FROM DEPT;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> SELECT * FROM EMP
2
SQL> SELECT * FROM DEPT
2 WHERE DNAME='SALES';
DEPTNO DNAME LOC
---------- -------------- -------------
30 SALES CHICAGO
SQL> SELECT * FROM EMP
2 WHERE JOB=(SELECT * FROM DEPT WHERE DNAME='SALES');
WHERE JOB=(SELECT * FROM DEPT WHERE DNAME='SALES')
*
ERROR at line 2:
ORA-00913: too many values
SQL> SELECT * FROM EMP
2
SQL> SELECT * FROM EMP
2 WHERE DEPTNO=(SELECT DEPTNO FROM DEPT WHERE DNAME='SALES');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
6 rows selected.
SQL> SELECT *
2 FROM EMP
3 WHERE ENAME IN(ALLEN,SCOTT);
WHERE ENAME IN(ALLEN,SCOTT)
*
ERROR at line 3:
ORA-00904: "SCOTT": invalid identifier
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 ENAME='ALLEN ' AND ENAME='SCOTT';
no rows selected
SQL> SELECT * FROM EMP
2 WHERE ENAME='ALLEN ' OR ENAME='SCOTT';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
SQL> SELECT DEPTNO
2 FROM EMP
3 WHERE ENAME='ALLEN' AND ENAME='SCOTT';
no rows selected
SQL> SELECT DEPTNO
2 FROM EMP
3 WHERE ENAME='ALLEN' OR ENAME='SCOTT';
DEPTNO
----------
30
20
SQL> SELECT DNAME.LOC
2 FROM DEPT
3 WHERE DEPTNO IN( SELECT DEPTNO
4 FROM EMP
5 WHERE ENAME='ALLEN' OR ENAME='SCOTT');
SELECT DNAME.LOC
*
ERROR at line 1:
ORA-00904: "DNAME"."LOC": invalid identifier
SQL> SELECT DNAME,LOC
2 FROM DEPT
3 WHERE DEPTNO IN( SELECT DEPTNO
4 FROM EMP
5 WHERE ENAME='ALLEN' OR ENAME='SCOTT');
DNAME LOC
-------------- -------------
RESEARCH DALLAS
SALES CHICAGO
SQL> SELECT * FROM DEPT
2 WHERE LOC LIKE'%O%;
ERROR:
ORA-01756: quoted string not properly terminated
SQL> SELECT * FROM DEPT
2 WHERE LOC LIKE'%O%';
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> SELECT * FROM EMP
2 WHERE LOC IN( SELECT * FROM DEPT
3 WHERE LOC LIKE'%O%');
WHERE LOC IN( SELECT * FROM DEPT
*
ERROR at line 2:
ORA-00904: "LOC": invalid identifier
SQL> SELECT * FROM EMP
2 WHERE DEPT=(SELECT * FROM DEPT
3 WHERE LOC LIKE'%O%');
WHERE DEPT=(SELECT * FROM DEPT
*
ERROR at line 2:
ORA-00904: "DEPT": invalid identifier
SQL> SELECT * FROM EMP
2 WHERE DEPTNO IN( SELECT * FROM DEPT
3 WHERE LOC LIKE'%O%');
WHERE DEPTNO IN( SELECT * FROM DEPT
*
ERROR at line 2:
ORA-00913: too many values
SQL> SELECT DEPTNO FROM DEPT
2 WHERE LOC LIKE'%O%';
DEPTNO
----------
10
30
40
SQL> SELECT * FROM EMP
2 WHERE DEPTNO IN( SELECT DEPTNO FROM DEPT
3 WHERE LOC LIKE'%O%');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
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
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
9 rows selected.
SQL> SELECT DEPTNO
2 FROM DEPT
3 WHERE DNAME IN('ACCOUNTING','SALES');
DEPTNO
----------
10
30
SQL> SELECT * FROM EMP
2 WHERE DEPTNO IN(SELECT DEPTNO
3 FROM DEPT
4 WHERE DNAME IN('ACCOUNTING','SALES');
WHERE DNAME IN('ACCOUNTING','SALES')
*
ERROR at line 4:
ORA-00907: missing right parenthesis
SQL> > SELECT * FROM EMP
SP2-0734: unknown command beginning "> SELECT *..." - rest of line ignored.
SQL> WHERE DEPTNO IN(SELECT DEPTNO
SP2-0734: unknown command beginning "WHERE DEPT..." - rest of line ignored.
SQL> FROM DEPT
SP2-0042: unknown command "FROM DEPT" - rest of line ignored.
SQL> WHERE DNAME IN('ACCOUNTING','SALES')SELECT * FROM EMP
SP2-0734: unknown command beginning "WHERE DNAM..." - rest of line ignored.
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.
SQL> WHERE DEPTNO IN(SELECT DEPTNO
SP2-0734: unknown command beginning "WHERE DEPT..." - rest of line ignored.
SQL> FROM DEPT
SP2-0042: unknown command "FROM DEPT" - rest of line ignored.
SQL> WHERE DNAME IN('ACCOUNTING','SALES')
SP2-0734: unknown command beginning "WHERE DNAM..." - rest of line ignored.
SQL>
SQL> SELECT * FROM EMP
2 WHERE DEPTNO IN(SELECT DEPTNO
3 FROM DEPT
4 WHERE DNAME IN('ACCOUNTING','SALES'));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
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
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
9 rows selected.
SQL> SELECT MAX(SAL)
2 FROM EMP
3 WHERE SAL!=5000;
MAX(SAL)
----------
3000
SQL> SELECT MAX(SAL)
2 FROM EMP
3 WHERE SAL<(SELECT MAX(SAL) FROM EMP);
MAX(SAL)
----------
3000
SQL> SELECT * FROM EMP
2 WHERE SAL=SELECT MAX(SAL)
3 FROM EMP
4 WHERE SAL<(SELECT MAX(SAL) FROM EMP);
WHERE SAL=SELECT MAX(SAL)
*
ERROR at line 2:
ORA-00936: missing expression
SQL> SELECT * FROM EMP
2 WHERE SAL=( SELECT MAX(SAL)
3 FROM EMP
4 WHERE SAL<(SELECT MAX(SAL) FROM EMP));
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> SPOOL OFF;