sql operations aggregate functions having clause database access layer a2 teacher up skilling...

21
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5

Upload: harry-black

Post on 24-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

SQL Operations

Aggregate Functions

Having Clause

Database Access Layer

A2 Teacher Up skilling

LECTURE 5

What’s to come today?

• SQL Operations

• Aggregate Functions

• Having Clause

• Database Access Layer

SQL Operations

• Over the next few slides, we will take a look at a few useful operations

that we can perform using SQL.

• Firstly, we will look at the rename operation which allows for the

renaming of relations and attributes in queries.

• Then we will proceed to look a string operations within SQL, that

allow us to craft SQL queries that search for specific character

patterns.

Rename Operation

• SQL allows renaming relations and attributes using the as clause:

old-name as new-name

Example:

select ID, name, salary/12 as monthly_salary

from instructor;

• Sometimes we may rename a relation just to get another copy.

Example:

Find the names of all instructors who have a higher salary than

some instructor in ‘Comp. Sci’.

select distinct T. name

from instructor as T, instructor as S

where T.salary > S.salary and S.dept_name = ‘Comp. Sci.’;

• Keyword as is optional and may be omitted.

String Operations

• SQL includes a string-matching operator for comparisons on character

strings.

• The operator “like” uses patterns that are described using two special

characters.

• percent (%) - The % character matches any substring.

• underscore (_) - The _ character matches any character.

Example:

Find the names of all instructors whose name includes the substring

“dar”.

select name

from instructor

where name like '%dar%‘;

String Operations (cont.)

• Pattern matching examples

• ‘Perry%’ matches any string beginning with “Perry”.

• ‘%idge%’ matches any string containing “idge” as a substring,

for example, ‘Perryeidge’, ‘Rock Ridge’, ‘Mianus Bridge’, and

‘Ridgeway’.

• ‘---’ matches any string of exactly three characters.

• ‘---%’ matches any string of at least three characters.

• Match the string “100 %”

like ‘100 \%' escape '\’

Ordering display of rows

• List in alphabetic order the names of all instructors

select distinct name

from instructor

order by name;

• The rows in the result table will be ordered according to the values of

the name attribute.

• We may specify desc for descending order or asc for ascending

order, for each attribute; ascending order is the default.

Example:

order by name desc

• Can sort on multiple attributes

Example:

order by dept_name, name

Set Operations

• A set is an unordered collection of elements, that contains no

duplicates.

• A table is basically a set of rows. So a set operator can be applied to

two tables (two sets of rows) just as two sets are in set theory.

• We will take a look at the following set operations:

• Union

• Intersection

• Difference

Set Union

• Given two sets A and b, an Union

operation between these sets

returns distinct elements that

belong to either A or B or both.

Example

• Find courses that ran in Fall 2009 or in Spring 2010

(select course_id from section where sem = ‘Fall’ and year =

2009)

union

(select course_id from section where sem = ‘Spring’ and year

= 2010);

Set Intersection

• Given two sets A and B, a

intersection operation between

these sets returns the distinct

elements that belong to both A

and B.

Example

• Find courses that ran in Fall 2009 and in Spring 2010

(select course_id from section where sem = ‘Fall’ and year =

2009)

intersect

(select course_id from section where sem = ‘Spring’ and year

= 2010);

Set Difference

• Given two sets A and B, a

difference operation (A\B)

between these sets returns the

elements that belong to A but

don’t belong to B.

Example

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

Aggregate Functions

• These functions operate on the multi-set of values of a column of a

relation or tuples in a relation, and return a value.

• A multi-set is a set which allows duplicates.

• Below we have listed a number of commonly used aggregate

functions.

avg: average value

min: minimum value

max: maximum value

sum: sum of values

count: number of values

Avg() Aggregate Function

• Find the average salary of instructors in the Computer Science

department

select avg (salary)

from instructor

where dept_name= ’Comp. Sci.’;

• Note that a set of salaries is first generated and then the aggregate

function avg is applied to the set.

Avg() Aggregate Function Example

ID name dept_name salary

10101 Srinivasan Comp. Sci. 65000

45565 Kaze Comp. Sci. 75000

83821 Brandt Comp. Sci. 92000

select avg (salary)from instructorwhere dept_name= ’Comp. Sci.’

avg (salary)

77333

Count() Aggregate Function

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

• The Count() function can be used to calculate the number of rows in a

table.

select count (*)

from course;

Group By Aggregate Function

• The Group By function group the result set by one or more columns.

• Find the average salary of instructors in each department

select dept_name, avg (salary)

from instructor

group by dept_name;

Group By Aggregate Function (cont.)• Attributes in select clause outside of aggregate functions must appear

in group by list

Example: erroneous query as the group by statement should include

both the dept_name and ID attributes.

select dept_name, ID, avg (salary)

from instructor

group by dept_name;

Having Clause

• The having clause is similar to where clause.

• Note: The conditions in the having clause are applied after the

formation of groups whereas conditions in the where clause are

applied before forming groups. The having clause applies to the result

of an aggregate function.

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;

Having Clause Example

select dept_name, avg (salary)

from instructor

group by dept_name

having avg (salary) > 42000;

dept_name salary

Biology 72000

Comp. Sci. 77333

Elec. Eng. 80000

Finance 85000

History 61000

Physics 91000

Data Access Layer

• You may have noticed in some of the practical classes, that we

created classes called ProjectDAL or CustomerDAL. The DAL postfix

in this class name, is just a reminder to the programmer that this

class is the layer that performs SQL operations on the data in the

database.

• A Data Access Layer is a good idea, as it helps to separate database

logic from the business and presentation logic of the application.

• By separating layers, this allows our application to be more flexible to

change.

What’s to come next time

• Week 6

• System Methodologies

• Roundup