0% found this document useful (0 votes)
26 views7 pages

Revision Mysql Functions & Group by.docx

Uploaded by

max12342732k
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)
26 views7 pages

Revision Mysql Functions & Group by.docx

Uploaded by

max12342732k
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/ 7

REVISION OF MYSQL AGGREGATE FUNCTIONS & GROUP BY

CLASS : XII
COMPUTER SCIENCE

FUNCTIONS IN MYSQL

A function can be defined as a set of predefined commands when called


performs certain computation & return a value.

Function in MySQL are classified in two types:


 Single Row functions
 Multiple Row functions

Single Row functions :

 Single row functions operate on a single value and return a single value.
 When applied to a table, they return a single result for every row of the
queried table.

They are classified into :

 String functions
 Numeric functions
 Date & time functions

Multiple Row functions :

 These are also called aggregate functions


 Aggregate functions is used to perform calculation on group of rows and
return the calculated summary.
AGGREGATE FUNCTIONS

Function Description

MAX() Returns the maximum/highest value among the values in the


given column/expression.
MIN() Returns the minimum/lowest value among the values in the
given column/expression.
SUM() Returns the sum of the values under the specified
column/expression.
AVG() Returns the average of the values under the specified
column/expression.
COUNT() Returns the total number of values/records under the
specified column/expression.

For Ex:

1. Select MAX(Salary) from EMPLOYEE; # 8000

2. Select MIN(Salary) from EMPLOYEE; # 4500

3. Select SUM(Salary) from EMPLOYEE; # 18500

4. Select AVG(Salary) from EMPLOYEE; # 6167

5. Select COUNT(*) from EMPLOYEE ; # 5

6. Select COUNT(DISTINCT City) from EMPLOYEE; # 4

7. Select COUNT(Salary) from EMPLOYEE; # 3


GROUP BY

 Group by clause helps up to divide the records table into logical groups
based on any column value.
 In those logically divided group of records we can apply aggregate
functions.

Syntax:

select <column_list with aggregate functions>


from <table name>
group by <column name>
having <search condition>

For. Ex

Consider the table employee…

Example:

1. select deptno, count(deptno) from employee group by deptno;


2. select deptno, max(sal) , min(sal) from employee group by deptno;

HAVING with GROUP BY

 To filter or restrict some records from the output produced by GROUP


BY, HAVING clause is used.
 It is used to put condition on group of rows.
 With having clause we can use aggregate functions also.
Example:
1. select deptno, sum(sal) from employee
group by deptno having deptno in(10,20);

2. select deptno, count(empno) from employee


group by deptno having count(*) > 1;
Difference between having & where clauses:

QUESTIONS ON GROUP BY CLAUSE


1. Consider the following table GAMES. Write SQL commands for the
following statements.

(i) To display game type and the number of each type of game.
SELECT type , count(type) FROM Game GROUP BY type;
(ii) To display the maximum prize money in each types of game.
SELECT type , max(prizemoney) FROM Game
GROUP BY type;
(iii) To display sum of PrizeMoney for each Type of GAMES.
SELECT type , sum(prizemoney) FROM Game
GROUP BY type;
2. Consider the following table CABHUB. Write SQL commands for the
following statements:

(i) To find the number vehicle of each make in CUBHUB table.


SELECT make,COUNT(make) FROM CABHUB GROUP BY
make;

(ii) To display the highest charges in each type of Make at which a


vehicle can be hired from CABHUB.
SELECT MAX(charge) FROM cabhub GROUP BY make;

(iii) To display the minimum charges of capacity more than 5 in each


type of color.
SELECT color, MIN(charge) FROM cabhub GROUP BY color
HAVING capacity > 5;
3. Write the commands for the following query based on TEACHER Table:
TNO TNAME Desig Age City GENDER SALARY
T01 Rakesh Sharma PGT 45 DELHI MALE 127200
T02 Jugal Mittal TGT 39 DELHI MALE 104000
T03 Sharmila Kaur PGT 35 MEERUT FEMALE 152000
Sandeep
T04 Kaushik PRT 27 MUMBAI MALE 180000
T05 Sangeeta Vats TGT 24 MEERUT FEMALE 126700
T06 Ramesh Kumar PRT 39 MUMBAI MALE 188000
T07 Shyam Arora TGT 38 DELHI MALE 144000
T08 Shiv Om PGT 65 CHENNAI MALE 156000
T09 Vivek Rawat PRT 53 KOLKATA MALE 104000
T10 Rajest Verma PRT 51 MEERUT MALE 187200
T11 Ashok Singhal TGT 29 DELHI MALE 110000

I. To display the total salary for each designation.


SELECT desig, sum(salary) FROM teacher GROUP BY desig ;
II. To display the maximum salary for each designation.
SELECT desig, max(salary) FROM teacher GROUP BY desig;
III. To display the minimum salary for each designation.
SELECT desig, min(salary) FROM teacher GROUP BY desig;
IV. To display the total number of teacher in each designation.
SELECT desig,count(desig) FROM teacher GROUP BY desig ;
V. To display the total number of teacher in each designation whose
age greater than 45;
SELECT desig, count(desig) FROM teacher
GROUP BY desig HAVING age > 45;
VI. To display the total number of teacher in each designation whose
age less than 45;
SELECT desig, sum(salary) FROM teacher
GROUP BY desig HAVING age < 45;
VII. To display the total number of female teacher in each designation.
SELECT desig, count(gender) FROM teacher
GROUP BY desig , gender;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy