Professional Documents
Culture Documents
Partha Pratim
Das
Objectives &
Outline Database Management Systems
Set Operations
Module 10: Introduction to SQL/3
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Partha Pratim Das
Null Values
ppd@cse.iitkgp.ac.in
Module 10
Objectives &
Outline
Set Operations
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Module Summary
Module 10
Partha Pratim • To familiarize with set operations, null values and aggregation
Das
Objectives &
Outline
Set Operations
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Module Summary
Module 10
Module Summary
Module 10
Partha Pratim
Das
Objectives &
Outline
Set Operations
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Module Summary
Set Operations
Module 10
Partha Pratim • Find courses that ran in Fall 2009 or in Spring 2010
Das
(select course id from section where sem = ’Fall’ and year = 2009)
Objectives &
Outline
union
Set Operations
(select course id from section where sem = ’Spring’ and year = 2010)
Null Values • Find courses that ran in Fall 2009 and in Spring 2010
Three Valued Logic
Aggregate
(select course id from section where sem = ’Fall’ and year = 2009)
Functions intersect
Group By
Having (select course id from section where sem = ’Spring’ and year = 2010)
Null Values
Module Summary
• Find courses that ran in Fall 2009 but not in Spring 2010
(select course id from section where sem = ’Fall’ and year = 2009)
except
(select course id from section where sem = ’Spring’ and year = 2010)
Module 10
Partha Pratim • Find the salaries of all instructors that are less than the largest salary
Das
select distinct T .salary
Objectives &
Outline
from instructor as T, instructor as S
Set Operations
where T .salary < S.salary
Null Values • Find all the salaries of all instructors
Three Valued Logic
Aggregate
select distinct salary
Functions from instructor
Group By
Having • Find the largest salary of all instructors
Null Values
Module Summary
(select “second query” )
except
(select “first query”)
Module 10
Module 10
Partha Pratim
Das
Objectives &
Outline
Set Operations
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Module Summary
Null Values
Module 10
Partha Pratim • It is possible for tuples to have a null value, denoted by null, for some of their attributes
Das
• null signifies an unknown value or that a value does not exist
Objectives &
Outline • The result of any arithmetic expression involving null is null
Set Operations
◦ Example: 5 + null returns null
Null Values
Three Valued Logic • The predicate is null can be used to check for null values
Aggregate
Functions ◦ Example: Find all instructors whose salary is null
Group By
Having
select name
Null Values from instructor
Module Summary where salary is null
• It is not possible to test for null values with comparison operators, such as =, <, or <>
We need to use the is null and is not null operators instead
Module 10
Module 10
Partha Pratim
Das
Objectives &
Outline
Set Operations
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Module Summary
Aggregate Functions
Module 10
Partha Pratim • These functions operate on the multiset of values of a column of a relation, and return
Das
a value
Objectives &
Outline avg: average value
Set Operations min: minimum value
Null Values max: maximum value
Three Valued Logic
sum: sum of values
Aggregate
Functions count: number of values
Group By
Having
Null Values
Module Summary
Module 10
Partha Pratim • Find the average salary of instructors in the Computer Science department
Das
select avg (salary )
Objectives &
Outline
from instructor
Set Operations
where dept name = ’Comp. Sci’;
Null Values • Find the total number of instructors who teach a course in the Spring 2010 semester
Three Valued Logic
Aggregate
select count (distinct ID)
Functions from teaches
Group By
Having where semester = ’Spring’ and year = 2010;
Null Values
Module Summary
• Find the number of tuples in the course relation
select count (*)
from courses;
Module 10
Partha Pratim
• Find the average salary of instructors in each department
Das
select dept name, avg(salary ) as avg salary
Objectives & from instructor
Outline
group by dept name;
Set Operations
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Module Summary
Module 10
Partha Pratim • Attributes in select clause outside of aggregate functions must appear in group by list
Das
/* erroneous query */
Objectives &
Outline
select dept name, ID, avg(salary )
Set Operations
from instructor
Null Values group by dept name;
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Module Summary
Module 10
Partha Pratim • Find the names and average salaries of all departments whose average salary is greater
Das
than 42000
Objectives &
Outline
select dept name, avg(salary )
Set Operations
from instructor
Null Values group by dept name
Three Valued Logic
having avg(salary ) > 42000;
Aggregate
Functions Note: predicates in the having clause are applied after the formation of groups whereas
Group By
Having predicates in the where clause are applied before forming groups
Null Values
Module Summary
Module 10
Aggregate
• All aggregate operations except count(*) ignore tuples with null values on the
Functions
Group By
aggregated attributes
Having
Null Values
• What if collection has only null values?
Module Summary ◦ count returns 0
◦ all other aggregates return null
Module 10
Partha Pratim • Completed the understanding of set operations, null values, and aggregation
Das
Objectives &
Outline
Set Operations
Null Values
Three Valued Logic
Aggregate
Functions
Group By
Having
Null Values
Slides used in this presentation are borrowed from http://db-book.com/ with kind
Module Summary
permission of the authors.
Edited and new slides are marked with “PPD”.