0% found this document useful (0 votes)
12 views37 pages

Chapter 3: Introduction To SQL

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views37 pages

Chapter 3: Introduction To SQL

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

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

select dept_name, avg (salary)


from instructor
group by dept_name
having avg (salary) > 42000;

• Note: predicates in the having clause are applied after


the formation of groups whereas predicates in the
where clause are applied before forming groups
6
Null Values and Aggregates
• Total all salaries
select sum (salary )
from instructor
– Above statement ignores null amounts
– Result is null if there is no non-null amount
• All aggregate operations except count(*) ignore
tuples with null values on the aggregated attributes
• What if collection has only null values?
– count returns 0
– all other aggregates return null
Null Values and Aggregates

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

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);

 Note: The above query can be written in a much simpler


manner. The formulation above is simply to illustrate SQL
features.

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 distinct T.name


from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = ’Biology’;

 Same query using > some clause

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.

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));
 Note that X – Y = Ø  X Y
 Note: Cannot write this query using = all and its variants

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

with max_budget (value) as


(select max (budget)
from department)
select budget
from department, max_budget
where department.budget = max_budget.value;

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

with dept _total (dept_name, tvalue) as


(select dept_name, sum (salary)
from instructor
group by dept_name),
dept_total_avg (avalue) as
(select avg (tvalue)
from dept_total)
select dept_name
from dept_total, dept_total_avg
where dept_total.tvalue >= dept_total_avg.avalue; 25
Scalar Subquery
• Scalar subquery is one which is used where a single value is expected
• E.g. select dept_name,
(select count (*)
from instructor
where department.dept_name = instructor.dept_name)
as num_instructors
from department;

• E.g. select name


from instructor
where salary * 10 >
(select budget from department
where department.dept_name = instructor.dept_name)

• Runtime error if subquery returns more than one result tuple

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

• Delete all instructors from the Finance department


delete from instructor
where dept_name= ’Finance’;
• Delete all tuples in the instructor relation for those instructors
associated with a department located in the Watson building.
delete from instructor
where dept_name in (select dept_name
from department
where building = ’Watson’);

28
Modification of the Database – Deletion
• Delete all instructors whose salary is less than the average salary of
instructors

delete from instructor


where salary< (select avg (salary) from instructor);

 Problem: as we delete tuples from deposit, the average salary


changes
 Solution used in SQL:
1. First, compute avg salary and find all tuples to delete
2. Next, delete all tuples found above (without recomputing avg or

retesting the tuples)

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);

• Add a new tuple to student with tot_creds set to null


insert into student
values (’3003’, ’Green’, ’Finance’, null);

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;

• If a salary increase is to be paid only to instructors with salary of less


than $70,000, we write:
update instructor
set salary = salary * 1.05
where salary < 70000;

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.

• Same query as before but with case statement


update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end

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

You might also like