Chapter 3: Introduction To SQL
Chapter 3: Introduction To SQL
Aggregate Functions
• These functions operate on the multiset of
values of a column of a relation, and return a
value
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
2
Aggregate Functions
• Find the average salary of instructors in the Computer Science
department
select avg (salary)
from instructor
where dept_name= ’Comp. Sci.’;
• Find the total number of instructors who teach a course in the
Spring 2010 semester
select count (distinct ID)
from teaches
where semester = ’Spring’ and year = 2010
• Find the number of tuples in the course relation
select count (*)
from course;
3
Aggregate Functions – Group By
• Tuples with the same value on all attributes in the group by clause are placed in
one group.
• Find the average salary of instructors in each department
– select dept_name, avg (salary)
from instructor
group by dept_name;
– Note: departments with no instructor will not appear in result
4
Aggregate Functions – Group By
• Attributes in select clause outside of aggregate
functions must appear in group by list
/* erroneous query */
select dept_name, ID, avg (salary)
from instructor
group by dept_name;
5
Aggregate Functions – Having Clause
• Find the names and average salaries of all
departments whose average salary is greater than
42000
8
Nested Subqueries
• SQL provides a mechanism for the nesting
of subqueries.
• A subquery is a select-from-where
expression that is nested within another
query.
• A common use of subqueries is to perform
tests for set membership, set comparisons,
and set cardinality.
9
Set Membership
• SQL allows testing tuples for membership in a relation.
• The in connective tests for set membership, where the set is a collection
of values produced by a select clause.
• Find courses offered in Fall 2009 and in Spring 2010
select distinct course_id
from section
where semester = ’Fall’ and year= 2009 and
course_id in (select course_id
from section
where semester = ’Spring’ and year= 2010);
• Finding all courses that were taught in Fall 2009 and that are
also members of the set of courses taught in Spring 2010.
• Find those courses that were taught in the Fall 2009 and that appear in
the set of courses obtained in the subquery.
10
Set Membership
select distinct course_id from section
where semester = ’Fall’ and year= 2009 and
course_id in (select course_id from section
where semester = ’Spring’ and year= 2010);
11
Set Membership
• Find courses offered in Fall 2009 but not in Spring 2010
select distinct course_id
from section
where semester = ’Fall’ and year= 2009 and
course_id not in (select course_id
from section
where semester = ’Spring’ and year= 2010);
12
Example Query
• Find the total number of (distinct) students who have taken
course sections taught by the instructor with ID 10101
13
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);
14
Set Comparison
• Find names of instructors with salary greater than that of some (at
least one) instructor in the Biology department.
select name
from instructor
where salary > some (select salary
from instructor
where dept_name = ’Biology’);
15
Definition of Some Clause
• F <comp> some r t r such that (F <comp> t )
Where <comp> can be:
0
(5 < some 5 ) = true
(read: 5 < some tuple in the relation)
6
0
(5 < some 5 ) = false
0
(5 = some 5 ) = true
0
(5 some 5 ) = true (since 0 5)
(= some) in
However, ( some) not in
16
Example Query
• Find the names of all instructors whose salary is greater than
the salary of all instructors in the Biology department.
select name
from instructor
where salary > all (select salary
from instructor
where dept_name = ’Biology’);
17
Definition of all Clause
• F <comp> all r t r (F <comp> t)
0
(5 < all 5 ) = false
6
6
(5 < all 10 ) = true
4
(5 = all 5 ) = false
4
(5 all 6 ) = true (since 5 4 and 5 6)
( all) not in
However, (= all) in
18
Test for Empty Relations
• The exists construct returns the value true if the argument
subquery is nonempty.
• exists r r Ø
• not exists r r = Ø
19
Correlation Variables
• Yet another way of specifying the query “Find all courses taught in
both the Fall 2009 semester and in the Spring 2010 semester”
select course_id
from section as S
where semester = ’Fall’ and year= 2009 and
exists (select *
from section as T
where semester = ’Spring’ and year= 2010
and S.course_id= T.course_id);
• Correlated subquery
• Correlation name or correlation variable
20
Not Exists
• Find all students who have taken all courses offered in the
Biology department.
21
Test for Absence of Duplicate Tuples
• The unique construct tests whether a subquery has any
duplicate tuples in its result.
– (Evaluates to “true” on an empty set)
• Find all courses that were offered at most once in 2009
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 = 2009);
22
Subqueries in the From Clause
• SQL allows a subquery expression to be used in the from clause
• Find the average instructors’ salaries of those departments where the average
salary is greater than $42,000.
select dept_name, avg_salary
from (select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name)
where avg_salary > 42000;
• Note that we do not need to use the having clause
• Another way to write above query
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;
23
With Clause
• The with clause provides a way of defining a temporary
view whose definition is available only to the query in
which the with clause occurs.
• Find all departments with the maximum budget
24
Complex Queries using With
• Clause
With clause is very useful for writing complex queries
• Supported by most database systems, with minor syntax
variations
• Find all departments where the total salary is greater than the
average of the total salary at all departments
26
Modification of the Database
• Deletion of tuples from a given relation
• Insertion of new tuples into a given relation
• Updating values in some tuples in a given relation
27
Modification of the Database – Deletion
• Delete all instructors
delete from instructor
28
Modification of the Database – Deletion
• Delete all instructors whose salary is less than the average salary of
instructors
29
Modification of the Database – Insertion
• Add a new tuple to course
insert into course
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
• or equivalently
insert into course (course_id, title, dept_name, credits)
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
30
Modification of the Database – Insertion
• Add all instructors to the student relation with tot_creds set to 0
insert into student
select ID, name, dept_name, 0
from instructor
• The select from where statement is evaluated fully before any of its
results are inserted into the relation (otherwise queries like
insert into table1 select * from table1
would cause problems, if table1 did not have any primary key defined.
31
Modification of the Database – Updates
• Suppose that annual salary increases are being made, and salaries of
all instructors are to be increased by 5 percent. We write:
update instructor
set salary= salary * 1.05;
32
Modification of the Database – Updates
• Increase salaries of instructors whose salary is
over $100,000 by 3%, and all others receive a 5%
raise
– Write two update statements:
update instructor
set salary = salary * 1.03
where salary > 100000;
update instructor
set salary = salary * 1.05
where salary <= 100000;
– The order is important
33
Modification of the Database – Updates
• SQL provides a case construct that we can use to perform both the
updates with a single update statement, avoiding the problem with
the order of updates.
34
Modification of the Database – Updates
• The general form of the case statement is as follows.
case
when pred1 then result1
when pred2 then result2
...
when predn then resultn
else result0
end
35
Updates with Scalar Subqueries
• Recompute and update tot_creds value for all students
update student S
set tot_cred = ( select sum(credits)
from takes natural join course
where S.ID= takes.ID and
takes.grade <> ’F’ and
takes.grade is not null);
• Sets tot_creds to null for students who have not taken any course
• Instead of sum(credits), use:
case
when sum(credits) is not null then sum(credits)
else 0
end
36
End of Chapter 3