0% found this document useful (0 votes)
14 views

PRACTICAL NO-5 To Study Aggregate Functions With Query.

Uploaded by

barot kajal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

PRACTICAL NO-5 To Study Aggregate Functions With Query.

Uploaded by

barot kajal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical 5

Aim: To study aggregate functions with query.


SQL Aggregate Functions

An aggregate function is a function that performs a calculation on a set of values, and returns a single
value.

Aggregate functions are often used with the GROUP BY clause of the SELECT statement. The GROUP
BY clause splits the result-set into groups of values and the aggregate function can be used to return
a single value for each group.

The most commonly used SQL aggregate functions are:

 MIN() - returns the smallest value within the selected column

 MAX() - returns the largest value within the selected column

 COUNT() - returns the number of rows in a set

 SUM() - returns the total sum of a numerical column

 AVG() - returns the average value of a numerical column

Aggregate functions ignore null values (except for COUNT()).

We will go through the aggregate functions above in the next chapters.

1. COUNT() Function

The COUNT() function returns the number of rows in a database table.

Syntax:

COUNT(*)

or

COUNT( [ALL|DISTINCT] expression )

2. SUM() Function

The SUM() function returns the total sum of a numeric column.

Syntax:

SUM()

or

SUM( [ALL|DISTINCT] expression )

3. AVG() Function

The AVG() function calculates the average of a set of values.

Syntax:

AVG()
or

AVG( [ALL|DISTINCT] expression )

4. MIN() Function

The MIN() aggregate function returns the lowest value (minimum) in a set of non-NULL values.

Syntax:

MIN()

or

MIN( [ALL|DISTINCT] expression )

5. MAX() Function

The MAX() aggregate function returns the highest value (maximum) in a set of non-NULL values.

Syntax:

AVG()

or

AVG( [ALL|DISTINCT] expression )


(1) List total deposit from deposit.
Query: SELECT COUNT (AMOUNT) "TOTAL AMOUNT" FROM DEPOSIT;
Output: No. Total Amount
1 8

(2) List total loan from karolbagh branch


Query: SELECT COUNT (LOAN NO) "TOTAL LOAN" FROM BORROW WHERE BNAME='KAROLBAGH';
Output: No. Total Loan
1 1

(3) Give maximum loan from branch vrce.


Query: SELECT MAX (AMOUNT)"MAX. LOAN" FROM BORROW WHERE BNAME='VIRAR';
Output: No. Max.Loan
1 8000

(4) Count total number of customers


Query: SELECT COUNT (CNAME) "TOTAL NUMBERS" FROM CUSTOMERS;
Output: No. Total Numbers
1 10

(5) Count total number of customer’s cities.


Query: SELECT COUNT(DISTINCT CITY) "TOTAL NUMBERS" FROM CUSTOMERS;
Output: No. Total Number
1 7

(6) Create table supplier from employee with all the columns.
Query: CREATE TABLE SUPPLIER (EMP_NO,EMP_NAME,EMP_SAL) AS SELECT
EMP_NO,EMP_NAME,EMP_SAL FROM EMPLOYEE;
Output: EMP_NO EMP_NAME EMP_SAL
101 Smith 800
102 Snehal 1600

(7) Create table sup1 from employee with first two columns.
Query: CREATE TABLE SUP1 (EMP_NO,EMP_NAME) AS SELECT EMP_NO,EMP_NAME FROM
EMPLOYEE;
Output: EMP_NO EMP_NAME
101 Smith
102 Snehal

(8) Create table sup2 from employee with no data


Query: CREATE TABLE SUP2 (EMP_NO,EMP_NAME,EMP_SAL) AS SELECT
EMP_NO,EMP_NAME,EMP_SAL FROM EMPLOYEE WHERE 1=0;
Output: EMP_NO WMP_NAME EMP_SAL

(9) Insert the data into sup2 from employee whose second character should be ‘n’ and string
should be 5 characters long in employee name field.
Query:INSERT INTO SUP2(EMP_NO,EMP_NAME,EMP_SAL) SELECT EMP_NO,EMP_NAME,EMP_SAL
WHERE EMP_NAME '_n___';
Output: -------------------------------------

(10) Delete all the rows from sup1.


Query: DELETE FROM SUP1;
Output: 8 rows deleted.

(11) Delete the detail of supplier whose sup_no is 103.


Query: DELETE FROM SUP1 WHERE SUP_NO = 103;
Output: -----------------------------------

(12) Rename the table sup2.


Query:RENAME SUP1 TO SUP2;
Output: SUP2

(13) Destroy table sup1 with all the data.


Query: DROP TABLE SUP1;
Output: -----------------

(14) Update the value dept_no to 10 where second character of emp. name is ‘m’.
Query: UPDATE EMPLOYEE SET DEPT_NO=10 WHERE EMP_NAME LIKE ('_m%');
Output: EMP_NAME DEPT_NO
Rmaj 10

(15) Update the value of employee name whose employee number is 103.
Query:UPDATE EMPLOYEE SET EMP_NAME='RAM' WHERE EMP_NO=103;
Output: EMP_NAME EMP_NO
RAM 103

You might also like