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

Lab 06 RDBMS

Uploaded by

Muhammad Areesh
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 views6 pages

Lab 06 RDBMS

Uploaded by

Muhammad Areesh
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/ 6

Lab # 6: Group Functions SWE-209

LAB # 6

GROUP FUNCTIONS

OBJECTIVE

• Identify the available group functions

• Describe the use of group functions

• Group data by using the group by clause

• Include or exclude grouped rows by using the having clause

THEORY

What Are Group Functions?

Group functions operate on sets of rows to give one result per group
.

37
Lab # 6: Group Functions SWE-209

Types of Group Functions


• AVG
• COUNT
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE

Group Functions: Syntax

Using the AVG and SUM Functions


You can use AVG and SUM for numeric data

38
Lab # 6: Group Functions SWE-209

Creating Groups of Data

GROUP BY Clause Syntax

You can divide rows in a table into smaller groups by using the GROUP BY clause.
All columns in the SELECT list that are not in group functions must be in the
GROUP BY clause.

39
Lab # 6: Group Functions SWE-209

The GROUP BY column does not have to be in the SELECT list.

SELECT AVG(salary) FROM


employees GROUP BY department_id ;

Grouping by More Than One Column

40
Lab # 6: Group Functions SWE-209

Restricting Group Results with the HAVING Clause

When you use the HAVING clause, the Oracle server restricts groups as follows:
1. Rows are grouped.
2. The group function is applied.
3. Groups matching the HAVING clause are displayed.

Using the HAVING Clause

Nesting Group Functions


Display the maximum average salary

41
Lab # 6: Group Functions SWE-209

LAB TASKS:

1. Consider the schema for CompanyDatabase:


EMPLOYEE (SSN, Name, Address, Gender, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)

Write SQL queries to

i. Make a list of all project numbers for projects that involve an employee whose last
name is ‘Scott’, either as a worker or as a manager of the department that controls
the project.
ii. Show the resulting salaries if every employee working on the ‘IoT’ project is
given a 10 percentraise.
iii. Find the sum of the salaries of all employees of the ‘Accounts’ department, as
well as the maximum salary, the minimum salary, and the average salary in this
department
iv. Retrieve the name of each employee who works on all the projects controlled by
department number (use NOT EXISTS operator).
v. 5 For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than Rs.6,00,000.

2. From the following table,


i. write a SQL query to find the highest grade of the customers for each of the city.
Return city, maximum grade.
ii. write a SQL query to find the total number of Salesman in each city. Return total
salesman and city name.
iii. write a SQL query to compute the average grade for unique city. Return average
price and city name.

42

You might also like