You are on page 1of 5

Find the average salary of instructors in the Computer Science department.

SQL> select * from instructor32;


ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp.Sci. 65000
12121 Wu Finance 90000
15151 Mozart Music 40000
22222 Einstein Physics 95000
32343 El Said History 60000
33456 Gold Physics 87000
45565 Katz Comp.Sci. 75000
58583 Califier History 62000
76543 Singh Finance 80000
76766 Crick Biology 72000
83821 Brandt Comp.Sci. 92000
ID NAME DEPT_NAME SALARY
----- -------------------- -------------------- ----------
98345 Kim Elec.Eng. 80000
>>12 rows selected.
SQL> select avg (salary)
2 from instructor32
3 where dept_name= 'Comp.Sci.';
AVG(SALARY)
-----------
77333.3333
Give a meaningful name to the attribute by using the as clause
SQL> select avg (salary) as avg_salary
2 from instructor32
3 where dept_name= 'Comp.Sci.';
AVG_SALARY
----------
77333.3333
Find the total number of instructors who teach a course in the spring 2010 semester
SQL> select * from teaches32;
ID COURSE_I SEC_ID SEMEST YEAR
----- -------- -------- ------ ----------
10101 CS-101 1 Fall 2009
10101 CS-315 1 Spring 2010
10101 CS-347 1 Fall 2009
12121 FIN-201 1 Spring 2010
15151 MU-199 1 Spring 2010
22222 PHY-101 1 Fall 2009
32343 HIS-351 1 Spring 2010
45565 CS-101 1 Spring 2010
45565 CS-319 1 Spring 2010
76766 BIO-101 1 Summer 2009
76766 BIO-301 1 Summer 2010
ID COURSE_I SEC_ID SEMEST YEAR
----- -------- -------- ------ ----------
83821 CS-190 1 Spring 2009
83821 CS-190 2 Spring 2009
83821 CS-319 2 Spring 2010
98345 EE-181 1 Spring 2009
>>15 rows selected.

SQL> select count (distinct ID)


2 from teaches32
3 where semester = 'Spring' and year = 2010;
COUNT(DISTINCTID)
-----------------
6
Find the number of tuples in the course relation
SQL> select count (*)
2 from course32;
COUNT(*)
----------
13
Find the average salary in each department.
SQL> select dept_name, avg (salary) as avg_salary
2 from instructor32
3 group by dept_name;
DEPT_NAME AVG_SALARY
-------------------- ----------
Physics 91000
Finance 85000
Elec.Eng. 80000
Comp.Sci. 77333.3333
Biology 72000
Music 40000
History 61000
>>7 rows selected.
Find the number of instructors in each department who teach a course in the spring
2010 semester.
SQL> select dept_name, id from instructor32 natural join teaches32;
DEPT_NAME ID
-------------------- -----
Comp.Sci. 10101
Comp.Sci. 10101
Comp.Sci. 10101
Finance 12121
Music 15151
Physics 22222
History 32343
Comp.Sci. 45565
Comp.Sci. 45565
Biology 76766
Biology 76766

DEPT_NAME ID
-------------------- -----
Comp.Sci. 83821
Comp.Sci. 83821
Comp.Sci. 83821
Elec.Eng. 98345
>>15 rows selected.
SQL> select dept_name, count (distinct ID) as instr_count
2 from instructor32 natural join teaches32
3 where semester = 'Spring' and year = 2010
4 group by dept_name;

DEPT_NAME INSTR_COUNT
-------------------- -----------
Comp.Sci. 3
Finance 1
History 1
Music 1
Each instructor in a particular group (defined by dept name) can have a different
ID, and since only one tuple is output for each group, there is no unique way of
choosing which ID value to output.
SQL> select dept_name, ID, avg (salary)
2 from instructor32
3 group by dept_name;
select dept_name, ID, avg (salary)
*
ERROR at line 1:
ORA-00979: not a GROUP BY expression
SQL> select dept_name, avg (salary)
2 from instructor32
3 group by dept_name;
DEPT_NAME AVG(SALARY)
-------------------- -----------
Physics 91000
Finance 85000
Elec.Eng. 80000
Comp.Sci. 77333.3333
Biology 72000
Music 40000
History 61000
>>7 rows selected.
Find the departments where the average salary of the instructors is more than
$42,000
SQL> select dept_name, avg (salary) as avg_salary
2 from instructor32
3 group by dept_name
4 having avg (salary) > 42000;
DEPT_NAME AVG_SALARY
-------------------- ----------
Physics 91000
Finance 85000
Elec.Eng. 80000
Comp.Sci. 77333.3333
Biology 72000
History 61000
>>6 rows selected.
----------Set Membership
Find all the courses taught in the both the Fall 2009 and Spring 2010 semesters
SQL> select distinct course_id
2 from section32
3 where semester = 'Fall' and year= 2009 and
4 course_id in (select course_id
5 from section32
6 where semester = 'Spring' and year= 2010);
COURSE_I
--------
CS-101
Find all the courses taught in the Fall 2009 semester but not in the Spring 2010
semester
SQL> select distinct course_id
2 from section32
3 where semester = 'Fall' and year= 2009 and
4 course_id not in (select course_id
5 from section32
6 where semester = 'Spring' and year= 2010);
COURSE_I
--------
CS-347
PHY-101
Selects the names of instructors whose names are neither �Mozart� nor �Einstein�.
SQL> select distinct name
2 from instructor32
3 where name not in ('Mozart', 'Einstein');
NAME
--------------------
Crick
El Said
Srinivasan
Gold
Brandt
Wu
Kim
Singh
Katz
Califier
>>10 rows selected.
-------------Set Comparison
Find the names of all instructors whose salary is greater than at least one
instructor in the Biology department.
SQL> select name
2 from instructor32
3 where salary > some (select salary
4 from instructor32
5 where dept_name = 'Biology');
NAME
--------------------
Einstein
Brandt
Wu
Gold
Kim
Singh
Katz
>>7 rows selected.
SQL> select distinct T.name
2 from instructor32 T, instructor32 S
3 where T.salary>S.salary and S.dept_name = 'Biology';
NAME
--------------------
Einstein
Gold
Brandt
Wu
Kim
Singh
Katz
>>7 rows selected.
Find the names of all instructors that have a salary value greater than that of
each instructor in the Biology department.
SQL> select name
2 from instructor32
3 where salary > all (select salary
4 from instructor32
5 where dept_name = 'Biology');
NAME
--------------------
Wu
Einstein
Gold
Katz
Singh
Brandt
Kim
>>7 rows selected.
Find the departments that have the highest average salary
SQL> select dept_name
2 from instructor32
3 group by dept_name
4 having avg (salary) >= all (select avg (salary)
5 from instructor32
6 group by dept_name);
DEPT_NAME
--------------------
Physics
-------------Test for Empty Relations
Find all courses taught in both the Fall 2009 semester and in the Spring 2010
semester.
SQL> select course_id
2 from section32 S
3 where semester = 'Fall' and year= 2009 and
4 exists (select *
5 from section32 T
6 where semester = 'Spring' and year= 2010 and
7 S.course_id= T.course_id);
COURSE_I
--------
CS-101

You might also like