Вы находитесь на странице: 1из 8

1.

SQL> select name,dept_name from student1;

NAME DEPT_NAME
-------------------- --------------------
Zhang Comp. Sci.
Shankar Comp. Sci.
Brandt History
Chavez Finance
Peltier Physics
Levy Physics
Williams Comp. Sci.
Sanchez Music
Snow Physics
Brown Comp. Sci.
Aoi Elec. Eng.

NAME DEPT_NAME
-------------------- --------------------
Bourikas Elec. Eng.
Tanaka Biology

13 rows selected.

2.SQL> select name from instructor where dept_name='Comp. Sci.' ;

NAME
--------------------
Srinivasan
Katz
Brandt

3.SQL> select title from course where dept_name='Comp. Sci.' and credits=3 ;

TITLE
--------------------------------------------------
Robotics
Image Processing
Database System Concepts

4.SQL> select takes.course_id,title from takes,course where takes.course_id=course


.course_id and takes.ID='12345' ;

COURSE_I TITLE
-------- --------------------------------------------------
CS-101 Intro. to Computer Science
CS-190 Game Design
CS-315 Robotics
CS-347 Database System Concepts

5.SQL> select name from instructor where salary between 40000 and 90000;

NAME
--------------------
Srinivasan
Wu
Mozart
El Said
Gold
Katz
Califieri
Singh
Crick
Kim

10 rows selected.

6.SQL> update advisor set i_id='1001' where s_id between '12345' and '12348' ;
update advisor set i_id='1001' where s_id between '12345' and '12348'
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.SYS_C007066) violated - parent key not
found

7.SQL> update instructor set dept_name='Comp. Sci.' where dept_name='IT' ;

0 rows updated.

8.SQL> update instructor set salary=0.05*salary+salary;

12 rows updated.

SQL> select salary from instructor;

SALARY
----------
68250
94500
42000
99750
63000
91350
78750
65100
84000
75600
96600

SALARY
----------
84000

12 rows selected.

9.SQL> delete from department where dept_name='IT' ;

0 rows deleted.

10.SQL> delete from prereq where course_id='101' ;

0 rows deleted.

11.SQL> delete from course where course_id not in(select course_id from takes);

1 row deleted.

12.SQL> alter table student1 add(DOB date) ;

Table altered.
13.SQL> alter table student1 modify(dept_name varchar(20)) ;

Table altered.

14.SQL> alter table student1 drop column DOB;

Table altered.

15.SQL> select ID from instructor where ID not in(select id from teaches);

ID
-----
33456
58583
76543

16.SQL> select student1.name,course.title,takes.year from student1,course,takes,sec


tion where student1.ID=takes.ID and course.course_id=takes.course_id and takes.c
ourse_id=section.course_id and section.room_number='100' ;

NAME TITLE
-------------------- --------------------------------------------------
YEAR
----------
Peltier Physical Principles
2009

Levy Image Processing


2010

Brown Image Processing


2010

17.SQL> select student1.name,course.course_id,course.title c_name from student1,cou


rse,takes where student1.ID=takes.ID and takes.course_id=course.course_id and ta
kes.year='2015' ;

no rows selected

18.SQL> select name,salary inst_salary from instructor where salary>some(select sal


ary from instructor where dept_name='Comp. Sci.');

NAME INST_SALARY
-------------------- -----------
Einstein 99750
Brandt 96600
Wu 94500
Gold 91350
Kim 84000
Singh 84000
Katz 78750
Crick 75600

8 rows selected.

19.SQL> select name from instructor where dept_name like '%ch' ;

no rows selected
20.SQL> select course.course_id,course.title,prereq.prereq_id from course,prereq wh
ere course.course_id=prereq.course_id order by title desc,course_id asc,prereq_i
d asc;

COURSE_I TITLE PREREQ_I


-------- -------------------------------------------------- --------
CS-315 Robotics CS-101
EE-181 Intro. to Digital Systems PHY-101
CS-319 Image Processing CS-101
BIO-301 Genetics BIO-101
CS-190 Game Design CS-101
CS-347 Database System Concepts CS-101

6 rows selected.

21.SQL> (select course_id from takes where semester='Fall' and year='2009')union(se


lect course_id from takes where semester='Spring' and year='2010') ;

COURSE_I
--------
CS-101
CS-315
CS-319
CS-347
FIN-201
HIS-351
MU-199
PHY-101

8 rows selected.

22.SQL> (select course_id from takes where semester='Fall' and year='2009')intersec


t(select course_id from takes where semester='Spring' and year='2010') ;

COURSE_I
--------
CS-101

23.SQL> (select course_id from takes where semester='Fall' and year='2009')minus(se


lect course_id from takes where semester='Spring' and year='2010') ;

COURSE_I
--------
CS-347
PHY-101

24.SQL> select avg(salary) from instructor group by dept_name having dept_name='Com


p. Sci.' ;

AVG(SALARY)
-----------
81200

SQL> select avg(salary) from instructor where dept_name='Comp. Sci.' group by de


pt_name ;

AVG(SALARY)
-----------
81200

SQL> select avg(salary) from instructor where dept_name in(select dept_name from
instructor where dept_name='Comp. Sci.') ;

AVG(SALARY)
-----------
81200

25.SQL> select count(distinct ID) from teaches where semester='Spring' and year='20
10' ;

COUNT(DISTINCTID)
-----------------
6

26.SQL> select course_id,count(distinct ID) from takes group by course_id ;

COURSE_I COUNT(DISTINCTID)
-------- -----------------
HIS-351 1
EE-181 1
CS-101 6
CS-347 2
FIN-201 1
CS-319 2
BIO-101 1
PHY-101 1
CS-315 2
CS-190 2
MU-199 1

COURSE_I COUNT(DISTINCTID)
-------- -----------------
BIO-301 1

12 rows selected.
(Distinct since CSE-101 offered in fall and spring both)

27.SQL> select dept_name,count(ID) from student1 group by dept_name having count(ID


)>10;

no rows selected

28.SQL> select count(course_id),dept_name from course group by dept_name ;

COUNT(COURSE_ID) DEPT_NAME
---------------- --------------------
1 Elec. Eng.
1 Physics
5 Comp. Sci.
1 Finance
2 Biology
1 History
1 Music

7 rows selected.

29.SQL> select dept_name,avg(budget) from department group by dept_name having avg(


budget)>42000 ;

DEPT_NAME AVG(BUDGET)
-------------------- -----------
Biology 90000
Comp. Sci. 100000
Elec. Eng. 85000
Finance 120000
History 50000
Music 80000
Physics 70000

7 rows selected.

30.SQL> select course_id from course minus(select course_id from takes);

no rows selected

31.SQL> select distinct course_id from takes where semester='Fall' and year='2009'
and course_id in(select course_id from takes where semester='Spring' and year='2
010');

COURSE_I
--------
CS-101

32.SQL> select count(distinct ID) from takes where course_id in(select course_id fr
om teaches where id='10101') ;

COUNT(DISTINCTID)
-----------------
6

33.SQL> select course_id from takes where semester='Fall' and year='2009' and cours
e_id not in(select course_id from takes where semester='Spring' and year='2010')
;

COURSE_I
--------
CS-347
CS-347
PHY-101

34.SQL> select name from instructor where salary>some(select salary from instructor
where dept_name='Biology');

NAME
--------------------
Einstein
Brandt
Wu
Gold
Kim
Singh
Katz

7 rows selected.

35.SQL> select name from instructor where salary>all(select salary from instructor
where dept_name='Biology');

NAME
--------------------
Katz
Singh
Kim
Gold
Wu
Brandt
Einstein

7 rows selected.

36.
SQL> select distinct course_id from takes T where semester='Spring' and year='20
10' and exists(select course_id from takes S where semester='Fall' and year='200
9' and T.course_id=S.course_id);

COURSE_I
--------
CS-101

37.SQL> select id from student1 S where not exists((select course_id from course wh
ere dept_name='Biology')minus(select T.course_id from takes T where S.id=T.id));

ID
-----
98988

38.
SQL> select course_id from section where year='2009' group by course_id having c
ount(course_id)<2 ;

COURSE_I
--------
BIO-101
CS-101
CS-347
EE-181
PHY-101

39.

40.SQL> with T as(select sum(Salary) total,dept_name from instructor group by dept_


name)select max(total) from T;

MAX(TOTAL)
----------
243600

SQL> select salary from instructor where dept_name in(select dept_name from inst
ructor group by dept_name having sum(salary)=(select max(s) from(select sum(sala
ry) s from instructor group by dept_name))) ;

SALARY
----------
96600
78750
68250 (Why isnt it working?)

41.SQL> with T as(select avg(salary) total,dept_name from instructor group by dept_


name)select total from T where total>42000 ;

TOTAL
----------
84000
95550
81200
89250
75600
64050

6 rows selected.

42.SQL> with max_bufget(value)as(select max(budget) from department)select dept_nam


e,budget from department,max_bufget where department.budget=max_bufget.value;

DEPT_NAME BUDGET
-------------------- ----------
Finance 120000

(Qs 37-43 can only be done by this method)

43.refer lab manual

Вам также может понравиться