-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter3.sql
More file actions
377 lines (329 loc) · 7.44 KB
/
Copy pathChapter3.sql
File metadata and controls
377 lines (329 loc) · 7.44 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*create table instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
*/
/*
create table instructor2(
ID char(5),
name varchar(20) not null ,
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) REFERENCES department);
*/
/*create table student(
ID varchar(5),
name varchar(20) not null ,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) REFERENCES department);
*/
/*create table takes(
ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
grade varchar(2),
primary key (ID,course_id,sec_id,semester,year),
FOREIGN key (ID) REFERENCES student,
foreign key (course_id,sec_id,semester,year) REFERENCES section);
*/
/*CREATE table course(
course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0),
primary key (course_id),
FOREIGN key (dept_name) REFERENCES department);
*/
--insert into instructor values('10211','Smith','Biology',66000);
--delete from student
--select name from instructor
--select all dept_name from instructor
-- select * from instructor
/*select name from instructor
where dept_name = 'Comp. Sci.' */
/*
select name
from instructor
where dept_name = 'Comp. Sci.' and salary > 70000;
*/
/*
create table teaches (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2));*/
/*select *
from instructor , teaches;
*/
/*
SELECT name , course_id
from instructor , teaches
where instructor.ID=teaches.ID;
*/
/*
select DISTINCT T.name
from instructor as T , instructor as S
where T.salary>S.salary and S.dept_name = 'Comp. Sci.'; */
/*
select name
from instructor
where name like '%dar%'; */
/*
SELECT DISTINCT name
from instructor
order by name;*/
/*
select name
from instructor
where salary BETWEEN 90000 and 100000;*/
/*
select name,course_id
from instructor , teaches
WHERE(instructor.ID,dept_name) = (teaches.ID,'Biology');
*/
/*
select course.id from section where sem = 'Fall' and year=2017
UNION
select course.id from section where sem = 'Spring' and year=2018;
*/
-- Union = shows fall and spring sections if they have same sections,shows once.
/*
select course.id from section where sem = 'Fall' and year=2017
INTERSECT
select course.id from section where sem = 'Spring' and year=2018;
*/
--intersect = shows common courses found in both lists .
/*
select course.id from section where sem = 'Fall' and year=2017
EXCEPT
select course.id from section where sem = 'Spring' and year=2018;
*/
--except : Fall section - spring section => special fall sections .
/*
SELECT name
from instructor
where salary is NULL; */
/*
SELECT avg(salary)
from instructor
where dept_name = 'Comp. Sci.';
*/
/*
select count(DISTINCT ID)
from teaches
where semester = 'Spring' and year = 2018;
*/
/*
select count(*)
from course;
*/
/*
select dept_name avg(salary) as avg_salary
from instructor
GROUP BY dept_name;
*/
/*
select dept_name,ID,avg(salary)
from instructor
GROUP by dept_name;
*/
/*
SELECT dept_name , avg(salary) as avg_salary
from instructor
GROUP BY dept_name
HAVING avg(salary) > 42000;
*/
/*
select DISTINCT course_id
from section
where semester = 'Fall' and year=2017 and course_id in (SELECT course_id
from section
where semester = 'Spring' and year = 2018);*/
/*
SELECT DISTINCT course_id
from section
where semester = 'Fall' and year = 2017 and
course_id not in (select course_id
from section
where semester = 'Spring' and year = 2018);
*/
/*
select DISTINCT name
FROM instructor
where name not in ('Mozart' , 'Einstein');
*/
/*
select count(DISTINCT ID)
from takes
where(course_id,sec_id,semester,year) in
(select course_id,sec_id,semester,year
from teaches
where teaches.ID=10101);
*/
/*
select DISTINCT T.name
from instructor as T,instructor as S
where T.salary > S.salary and S.dept_name = 'Biology';
*/
/*
select name
from instructor
where salary>some(select salary
from instructor
where dept_name = 'Biology');
*/
/*
select name
from instructor
where salary > all(select salary
from instructor
where dept_name='Biology'); */
/*
select course_id
from section as S
where semester = 'Fall' and year = 2017 AND
EXISTS(SELECT *
from section as T
where semester = 'Spring' and year = 2018
and S.course_id = T.course_id);
*/
/*
select distinct S.ID, S.name
from student as S
where not exists ( (select course_id
from course
where dept_name = 'Biology')
except
(select T.course_id
from takes as T
where S.ID = T.ID));
*/
/*
select T.course_id
from course as T
where UNIQUE (SELECT R.course_id
from section as R
where T.course_id = R.course_id
and R.year= 2017);
*/
/*
select dept_name , avg_salary
from(select dept_name,avg(salary) as avg_salary
from instructor
GROUP by dept_name)
where avg_salary>42000;
*/
/*
select dept_name,avg_salary
from (SELECT dept_name,avg(salary)
from instructor
GROUP by dept_name)
as dept_avg (dept_name,avg(salary)
where avg_salary > 42000;
*/
/*
with max_budget(value) as
(select max(budget)
from department)
select department.name
from department,max_budget
where department.budget = max_budget.value;
*/
/*
with dept_total(dept_name,value) as
(select dept_name , sum(salary)
from instructor
GROUP by dept_name),
dept_total_avg(value) as
(select avg(value)
from dept_total)
select dept_name
from dept_total,dept_total_avg
where dept_total.value > dept_total_avg.value;
*/
/*
select dept_name,
( select count(*)
from instructor
where department.dept_name = instructor.dept_name)
as num_instructors
from department;
*/
/*
delete from instructor
where dept_name= 'Finance’;
*/
/*
delete from instructor
where dept_name in (select dept_name
from department
where building = 'Watson');
*/
/*
delete from instructor
where salary < (select avg (salary)
from instructor);
*/
/*
insert into course
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
*/
/*
insert into course (course_id, title, dept_name, credits)
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
*/
/*
insert into student
values ('3003', 'Green', 'Finance', null);
*/
/*
insert into instructor
select ID, name, dept_name, 18000
from student
where dept_name = 'Music' and total_cred > 144;
*/
/*
update instructor
set salary = salary * 1.05
*/
/*
update instructor
set salary = salary * 1.05
where salary < 70000;
*/
/*
update instructor
set salary = salary * 1.05
where salary < (select avg (salary)
from instructor);
*/
/*
update instructor
set salary = salary * 1.03
where salary > 100000;
update instructor
set salary = salary * 1.05
where salary <= 100000;
*/
/*
update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end
*/
/*
update student S
set tot_cred = (select sum(credits)
from takes, course
where takes.course_id = course.course_id and
S.ID= takes.ID.and
takes.grade <> 'F' and
takes.grade is not null);
*/