Professional Documents
Culture Documents
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