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

DBMS PPT6

The document discusses SQL query language and various SQL concepts. It covers aggregate functions like count, sum, max, min, and avg that operate on tables and columns. It describes group by queries and how they subset records into groups. Set operations like union, intersection, and difference are explained along with examples. Views are introduced as virtual tables created from the results of a select query on one or more tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views29 pages

DBMS PPT6

The document discusses SQL query language and various SQL concepts. It covers aggregate functions like count, sum, max, min, and avg that operate on tables and columns. It describes group by queries and how they subset records into groups. Set operations like union, intersection, and difference are explained along with examples. Views are introduced as virtual tables created from the results of a select query on one or more tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Database Management Systems

SQL Query Language (3)

1 Database Management Systems, Roya Choupani 3/21/2016


Topics
 Aggregate Functions in Queries
 count
 sum
 max
 min
 avg
 Group by queries
 Set Operations in SQL Queries
 Views

2 Database Management Systems, Roya Choupani 3/21/2016


Aggregate Functions
 Tables are collections of records with a schema.
 Aggregate functions are used to get information about the
records of a table such as:
 The maximum value in a column of a table
 The number of records in a table
 Average of the values in a column of a table

3 Database Management Systems, Roya Choupani 3/21/2016


Aggregate Functions
 Most important aggregate functions are:
 count
 sum
 max
 min
 avg

4 Database Management Systems, Roya Choupani 3/21/2016


Count Function
 Count returns the number of rows or distinct values.
 Count is used with SELECT query
 Count can be used with distinct option

 Syntax
Select count( [distinct] attribute list )
From table_name
where condition

5 Database Management Systems, Roya Choupani 3/21/2016


Example
 Find the number of employees:
Select count(*)
From Employee

 Find the number of different values on the attribute Salary for all
the rows in Employee:

Select count(distinct Salary)


From Employee

6 Database Management Systems, Roya Choupani 3/21/2016


Example Query Results

 Query 1 : 8
 Query 2 : 6

7 Database Management Systems, Roya Choupani 3/21/2016


Sum Function
 Sum finds the total sum of the values in an attribute.
 Sum cannot be used with non-numeric attributes.
e.g. Find the sum of the salaries of the Administration department
select sum(Salary) as SumSalary
from Employee
where Dept = ‘Administration’

Result:

8 Database Management Systems, Roya Choupani 3/21/2016


Min, Max and Avg functions
 Min, Max, and Avg functions are used to find the minimum,
maximum and average of the values in an attribute.

e.g. Find the maximum salary among the


employees who work in a department
based in London.

9 Database Management Systems, Roya Choupani 3/21/2016


Example Query
Select max(Salary) as MaxLondonSal
From Employee, Department
Where
Employee.DeptCode = Department.DeptCode
AND
Department.City = ‘London’

10 Database Management Systems, Roya Choupani 3/21/2016


11 Database Management Systems, Roya Choupani 3/21/2016
Group By
 Aggregate functions can also be used with sub-sets of the
table records.
 Group By puts the records in subsets (groups)
 Aggregate functions can be used with Group By

12 Database Management Systems, Roya Choupani 3/21/2016


Group By Example
 Find the sum of salaries of all the employees of each
department:

Select Dept, sum(Salary)as TotSal


From Employee, Department
Where
Employee.DeptCode = Department.DeptCode
Group by Dept

13 Database Management Systems, Roya Choupani 3/21/2016


How Group By Works?
First the query is executed
Then the query result is divided into subsets
Next aggregate function is executed for each group

14 Database Management Systems, Roya Choupani 3/21/2016


Conditions on Aggregate Function
Result
 When conditions are on the result of an aggregate operator, it is
necessary to use the having clause

 e.g. Find which departments spend more than 100 on salaries

Select Dept, sum(Salary) as TotSal


from Employee, Department
Where
Employee.DeptCode = Department.DeptCode
group by Dept
having sum(Salary) > 100
Result:
15 Database Management Systems, Roya Choupani 3/21/2016
Example
 Find the departments in which the average salary of employees
older than 30 is higher than 25:

Select Dept, avg(Salary)


from Employee, Department
Where
Employee.DeptCode = Department.DeptCode
AND
Age > 30
group by Dept
having avg(Salary) > 25

16 Database Management Systems, Roya Choupani 3/21/2016


Set Operations in SQL Queries
 Union, Intersection and Set Difference operations can be
carried out with SQL
 e.g. Find the first names and surnames of the employees

This query tries to find the union of two sets


1- Set of first names
2- Set of surnames

17 Database Management Systems, Roya Choupani 3/21/2016


Union in SQL Queries
Select FirstName as Name
from Employee
union
Select Surname
from Employee

 Duplicates are removed by UNION

18 Database Management Systems, Roya Choupani 3/21/2016


Union Query Result

19 Database Management Systems, Roya Choupani 3/21/2016


Intersection in SQL
Find the surnames of employees that are also
first names:

select FirstName as Name


from Employee
intersect
select Surname
from Employee

20 Database Management Systems, Roya Choupani 3/21/2016


Intersect Query Result

21 Database Management Systems, Roya Choupani 3/21/2016


Set Difference in SQL
 Find the first names of employees that are not
also surnames:
select FirstName as Name
from Employee
except
select Surname
from Employee

22 Database Management Systems, Roya Choupani 3/21/2016


Set Difference Result

23 Database Management Systems, Roya Choupani 3/21/2016


Views
 A view is a virtual table. It does not physically exist. Rather,
it is created by a query joining one or more tables
 A view can be a subset of a table
 Syntax:
create view ViewName [ (AttributeList) ] as SelectSQL

24 Database Management Systems, Roya Choupani 3/21/2016


Creating a Sample View
 Create a view for the employees of Administration
department using their Name, Surname, and Salary.

 The view is the result of a select query on the employee table

25 Database Management Systems, Roya Choupani 3/21/2016


Example Creating Views
Create view AdminEmployee
(FirstName,Surname,Salary) as

select FirstName,Surname,Salary
from Employee
where Dept = ‘Administration’

26 Database Management Systems, Roya Choupani 3/21/2016


Example View

27 Database Management Systems, Roya Choupani 3/21/2016


Summary
 Aggregate functions are used to get information about the
records of a table
 Set operations are also used in SQL tables
 A virtual table named view can be created using some of the
records or attributes of one or more tables.

28 Database Management Systems, Roya Choupani 3/21/2016


Questions?

29 Database Management Systems, Roya Choupani 3/21/2016

You might also like