You are on page 1of 19

Module 10

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

Module Summary Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur

ppd@cse.iitkgp.ac.in

Database Management Systems Partha Pratim Das 10.1


Module Recap PPD

Module 10

Partha Pratim • Completed the understanding of basic query structure


Das

Objectives &
Outline

Set Operations

Null Values
Three Valued Logic

Aggregate
Functions
Group By
Having
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.2


Module Objectives PPD

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

Database Management Systems Partha Pratim Das 10.3


Module Outline PPD

Module 10

Partha Pratim • Set Operations: union, intersect, except


Das
• Null Values
Objectives &
Outline • Aggregate Functions: avg, min, max, sum, and count
Set Operations
◦ Group By
Null Values
Three Valued Logic ◦ Having
Aggregate ◦ Null Values
Functions
Group By
Having
Null Values

Module Summary

Database Management Systems Partha Pratim Das 10.4


Set Operations PPD

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

Database Management Systems Partha Pratim Das 10.5


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)

Database Management Systems Partha Pratim Das 10.6


Set Operations (2)

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

Database Management Systems Partha Pratim Das 10.7


Set Operations (3)

Module 10

Partha Pratim • Set operations union, intersect, and except


Das
◦ Each of the above operations automatically eliminates duplicates
Objectives &
Outline • To retain all duplicates use the corresponding multiset versions union all, intersect all,
Set Operations and except all.
Null Values
Three Valued Logic • Suppose a tuple occurs m times in r and n times in s, then, it occurs:
Aggregate
Functions
◦ m + n times in r union all s
Group By ◦ min(m, n) times in r intersect all s
Having
Null Values
◦ max(0, m − n) times in r except all s
Module Summary

Database Management Systems Partha Pratim Das 10.8


Null Values PPD

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

Database Management Systems Partha Pratim Das 10.9


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

Database Management Systems Partha Pratim Das 10.10


Null Values (2): Three Valued Logic

Module 10

Partha Pratim • Three values – true, false, unknown


Das
• Any comparison with null returns unknown
Objectives &
Outline ◦ Example: 5 < null or null <> null or null = null
Set Operations
• Three-valued logic using the value unknown:
Null Values
Three Valued Logic ◦ OR: (unknown or true) = true,
Aggregate
Functions
(unknown or false) = unknown
Group By (unknown or unknown) = unknown
Having
Null Values
◦ AND: (true and unknown) = unknown,
Module Summary (false and unknown) = false,
(unknown and unknown) = unknown
◦ NOT: (not unknown) = unknown
◦ “P is unknown“ evaluates to true if predicate P evaluates to unknown
• Result of where clause predicate is treated as false if it evaluates to unknown

Database Management Systems Partha Pratim Das 10.11


Aggregate Functions PPD

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

Database Management Systems Partha Pratim Das 10.12


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

Database Management Systems Partha Pratim Das 10.13


Aggregate Functions (2)

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;

Database Management Systems Partha Pratim Das 10.14


Aggregate Functions (3): Group By

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

Database Management Systems Partha Pratim Das 10.15


Aggregate Functions (4): Group By

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

Database Management Systems Partha Pratim Das 10.16


Aggregate Functions (5): Having Clause

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

Database Management Systems Partha Pratim Das 10.17


Null Values and Aggregates

Module 10

Partha Pratim • Total all salaries


Das
select sum (salary )
Objectives &
Outline
from instructor ;
Set Operations ◦ Above statement ignores null amounts
Null Values ◦ Result is null if there is no non-null amount
Three Valued Logic

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

Database Management Systems Partha Pratim Das 10.18


Module Summary PPD

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”.

Database Management Systems Partha Pratim Das 10.19

You might also like