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

Ex 8-1

The document discusses various aggregate functions used in SQL including AVG, COUNT, STDDEV, VARIANCE, SUM, MAX, and MIN. It provides examples of using these functions to return average salary, count number of employees, calculate standard deviation and variance of salaries, sum total salaries, and return maximum and minimum salaries. It also discusses concepts like ignoring null values and grouping data.

Uploaded by

Shweni Doobaree4
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)
64 views6 pages

Ex 8-1

The document discusses various aggregate functions used in SQL including AVG, COUNT, STDDEV, VARIANCE, SUM, MAX, and MIN. It provides examples of using these functions to return average salary, count number of employees, calculate standard deviation and variance of salaries, sum total salaries, and return maximum and minimum salaries. It also discusses concepts like ignoring null values and grouping data.

Uploaded by

Shweni Doobaree4
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

University of technology

DOOBAREE Phanipriya Shweni

Stud id: 2110_22784

Assignment 8-1

• Calculates average value excluding nulls- AVG group

function

• Returns the number of rows with non-null values for the

expression- COUNT group function

• For two sets of data with approximately the same mean,

the greater the spread, the greater the standard

deviation.- STDDEV group function

• Operate on sets of rows to give one result per group any group function

• Returns minimum value ignoring nulls- MIN group

function

• Used with columns that store numeric data to calculate

the spread of data around the mean- VARIANCE group

function

• Calculates the sum ignoring null values- SUM group

function

• Returns the maximum value ignoring nulls- MAX group

function

• To gather into a sum or whole- Aggregate functions

return a single result row based on groups of rows. Aggregate is

something that is formed by combining several separate

elements.

1. AVG, COUNT :

· Calculates average value excluding nulls- AVG group function


· Returns the number of rows with non-null values for the

expression- COUNT group function

SELECT AVG(salary) || ' is Average Salary of ' || COUNT(salary) || '

employees. This table has primary key employee_id which won''t be

null. So, ' || (COUNT(employee_id) - COUNT(salary)) || ' rows are

skipped in Average Salary calculation.' "Example"

FROM EMPLOYEES;

(I may use COUNT(*) instead of COUNT(employee_id), if I want to)

Output:

8775 is Average Salary of 20 employees. This table has primary key

employee_id which won't be null. So, 0 rows are skipped in Average

Salary calculation.

Please note that in this concatenation and minus has same

precedence. So () were required.

MAX, MIN:

· Returns minimum value ignoring nulls- MIN group function

· Returns the maximum value ignoring nulls- MAX group

function

SELECT 'The maximum of ' || COUNT(salary) || ' salaries in

employees table is ' || MAX(salary) ||'. The minimum of

'|| COUNT(salary) || ' salaries in employees table is '|| MIN(salary) ||'.'

"Example2"

FROM EMPLOYEES;

Output:
The maximum of 20 salaries in employees table is 24000. The

minimum of 20 salaries in employees table is 2500.

STDDEV, VARIANCE:

· Used with columns that store numeric data to calculate the

spread of data around the mean- VARIANCE group function

· For two sets of data with approximately the same mean, the

greater the spread, the greater the standard

deviation.- STDDEV group function

SELECT 'The standard deviation of ' || COUNT(salary) || ' salaries in

employees table is ' || ROUND(STDDEV(salary), 4) ||'. The variance

of '|| COUNT(salary) || ' salaries in employees table is

'|| ROUND(VARIANCE(salary), 4) ||'.' "Example3"

FROM EMPLOYEES;

SUM:

· Calculates the sum ignoring null values- SUM group function

SELECT 'The sum of ' || COUNT(salary) || ' salaries in employees

table is ' || SUM(salary) ||'.' "Example4"

FROM EMPLOYEES;

Output:

The sum of 20 salaries in employees table is 175500.

2. SELECT ROUND(AVG(cost),2) as "Average Cost"

FROM d_events;

9000
3. SELECT TO_CHAR(ROUND(AVG(salary),2), '$999999.99')

as "Average Salary"

FROM f_staffs

WHERE manager_id = 19;

$8.38

4. SELECT TO_CHAR(ROUND(SUM(salary),2), '$999999.99')

as "Total Salary"

FROM f_staffs

WHERE id in (12, 19);

$66.75

5. SELECT MIN(salary) "lowest salary", MAX(hire_date) "most

recent hire date", MIN(last_name) "top last name",

MAX(last_name) "bottom last name"

FROM employees

WHERE department_id in (50, 60);

6.one

7. SELECT AVG(NVL(salary, hourly_rate* hrs_worked_in_yr ))

This way the null fields beings ignored will also be counted in.

8. March 30, 1969

9. SELECT 'Average of ' || COUNT(order_number) || ' orders is

: ' || AVG(NVL(order_total, 0)) as "Average"

FROM f_orders

WHERE order_date BETWEEN TO_DATE('January 1, 2002',


'fmMonth DD, YYYY') AND TO_DATE('December 21, 2002',

'fmMonth DD, YYYY');

Average of 1 orders is : 103.02

10. SELECT MAX(hire_date) as "the last"

FROM employees;

29-Jan-2000

11. SUM must be be ‘equal or greater than’ average.

12. a. FROM event_date

FALSE, this is a column. It will say ORA-00942: table or view does

not exist

b. SELECT SUM(cost)

TRUE

c. SELECT SUM(event_date)

FALSE. This column is not a number. ORA-00932: inconsistent

datatypes: expected NUMBER got DATE

d. SELECT description, AVG(cost) AS "Expense"

FALSE. ORA-00937: not a single-group group function. Remove

either description (to get single row output), or remove avg(cost)

e. WHERE MIN(id) = 100

FASLE. MIN is a group function. ORA-00934: group function is

not allowed here


f. SELECT MAX(AVG(cost)

FALSE. ORA-00978: nested group function without GROUP BY

g. SELECT MIN(event_date)

TRU50)

You might also like