Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Week 9 lec 1 2 3Aggregate Functions

Uploaded by

saadbink1234
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Week 9 lec 1 2 3Aggregate Functions

Uploaded by

saadbink1234
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Aggregate Functions

Week 9 lec 1 2 3
Introduction
• SQL aggregation function is used to perform the
calculations on multiple rows of a single column of a
table. It returns a single value.
• It is also used to summarize the data.
• Aggregate functions ignore null values (except for
Count())
Types
COUNT FUNCTION

• COUNT function is used to Count the number of rows in


a database table. It can work on both numeric and non-
numeric data types.
• COUNT function uses the COUNT(*) that returns the
count of all the rows in a specified table. COUNT(*)
considers duplicate and Null.
• Syntax
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Example (Count(*) and Count with
WHERE
1. SELECT COUNT(*)
FROM PRODUCT_MAST;
Output:
10
2. COUNT with WHERE
SELECT COUNT(*)
FROM PRODUCT_MAST;
WHERE RATE>=20;
Output:
7
Example(Count(Distinct) and Count
with Group by)
SELECT COUNT(DISTINCT COMPANY)
FROM PRODUCT_MAST;
Output:
3
COUNT() with GROUP BY
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY;
Output:
Com1 5
Com2 3
Com3 2
Example( Count with Having)
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
Output:
Com1 5
Com2 3
SUM Function

• Sum function is used to calculate the sum of all selected


columns. It works on numeric fields only.
• Syntax
SUM()
or
SUM( [ALL|DISTINCT] expression )
Example: SUM()
SELECT SUM(COST)
FROM PRODUCT_MAST;
Output:
670
Example: SUM() with WHERE
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;
• Output:
320
Example: SUM() with GROUP BY
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3
GROUP BY COMPANY;
Output:
Com1 150
Com 2 170
Example: SUM() with HAVING
SELECT COMPANY, SUM(COST)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING SUM(COST)>=170;
• Output:
• Com1 335
• Com3 170
AVG function

• The AVG function is used to calculate the average value of the


numeric type. AVG function returns the average of all non-Null values.
• Syntax:
AVG()
or
AVG( [ALL|DISTINCT] expression )
• Example:

SELECT AVG(COST)
FROM PRODUCT_MAST;
• Output :

67.00
MAX Function

• MAX function is used to find the maximum value of a


certain column. This function determines the largest value
of all selected values of a column.
• Syntax:
MAX()
or
MAX( [ALL|DISTINCT] expression )
• Example
SELECT MAX(RATE)
FROM PRODUCT_MAST;
• Output:
30
Min
MIN()
or
MIN( [ALL|DISTINCT] expression )
• Example:
SELECT MIN(RATE)
FROM PRODUCT_MAST;
• Output
10
Practice questions
1. From the orders table, write a SQL
query to calculate total purchase
amount of all orders placed on specific
date. Return total purchase amount.

The query should also display the


no of orders sold on those dates.
2. From the orders table, calculate the
average purchase amount of all orders.
Return average purchase amount.
3. From the orders table, write a SQL
query that counts the number of unique
salespeople. Return number of
salespeople.
Practice questions
1. write a SQL query to count the
number of customers from
customers table. Return number
of customers.
2. From the customers table, write
a SQL number of customers
who received at least one grade
for their activity.
3. write a SQL query to find the
highest grade of the customers
in each city. Return city,
maximum grade.
Solution
1. SELECT SUM(purch_amt) -- Specifies the table from which
to retrieve the data (in this case, 'orders’).
FROM orders
Group by order_date
Having count(order_no);

2. SELECT AVG(purch_amt)
FROM orders;
3. SELECT COUNT(DISTINCT salesman_id)
FROM orders;
4. SELECT COUNT(*) FROM customer;
Solution
5. SELECT COUNT(ALL grade)
FROM customer;
6. SELECT city, MAX(grade)
FROM customer
GROUP BY city;
7.

You might also like