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

Aggregate Functions

Uploaded by

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

Aggregate Functions

Uploaded by

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

Types of SQL Functions

1. Single Row functions


2. Multiple Row functions (Aggregate Functions)

Single row functions : It works with a single row at a time. A single


row function returns a result for every row of a queried table.
Example : concat(), instr(), mid(), round() etc…

Multiple row functions or Group functions : It work with data of


multiple rows at a time and return aggregated value which is a
single value.
AGGREGATE FUNCTIONS (Multiple Row Functions)
• So far we discussed about functions that operate on individual
rows in a table. E.g., Single row functions produce output from
each row which satisfy the given condition.
• AGGREGATE Functions are also called Multiple row functions.
• The aggregate functions work upon a group of rows and produce
a single output.
• Many group functions accept the following options :
1. DISTINCT : This option causes a group function to consider
only distinct values of the argument expression.
2. ALL : This option causes a group function to consider all
values including all duplicates.
AVG :
• This function computes the average of given data.
Q : Calculate the average salary of all employees listed in empl
table .
Ans : Select avg(salary) “Average” from empl;
COUNT :
• This function counts the number of rows in a given column or
expression.
Syntax: count(*) or count([DISTINCT]<column name>)
• count(*) will return the number of rows in the query.
• count(<column name>) will return the number of rows without
null value in the given column.
• count(distinct <column name>) will return the number of rows
without duplicate and which are not null.
MAX() : To find the maximum value from a column.
MIN() : To find the minimum value from a column.
SUM() : To find the sum values of a column.
Q1: Find the number of Q2: Find the number of
rows in the table empl. values in the column comm
without null value.

Q3: Find the number Q4: Find the number of


values in job column in different jobs in empl.
empl
Grouping Result – GROUP BY
• The GROUP BY clause combines all those records that have
identical values in a particular field or a group of fields.
• In other words, the GROUP BY clause is used in SELECT
statements to divide the table into groups.
• Grouping can be done by a column name, or with aggregate
functions in which case the aggregate produces a value for each
group.
Q5: Find the number of employees with job ‘Clerk’

Q6: Find the number of employees in each job.


Q5: Find the average salary of employees in each dept
ORDER BY clause
The ORDER BY clause is used to sort the result set in ascending or
descending order.
Syntax :
SELECT <comma separated select list> FROM <table>
[<WHERE condition>]
ORDER BY <fieldname1>[ASC | DESC] [,<fieldname2> [ASC |
DESC], …];
Consider table Data with the following fields
Data(Rno int primary key, Name varchar(20), Marks float, Grade
char(3), Section char(2))

Q1 : Display the table in the ascending order of Marks


Ans : SELECT * from Data order by Marks;
Or
Ans : SELECT * from Data order by Marks ASC;

Q2 : Display the table in the Ascending order of Sections first, and


then in the order of Marks
Ans : SELECT * from Data order by Section ASC, Marks DESC;
Sometimes you need to display the result of a calculation or a
mathematical expression in the result set. The ORDER BY clause
allows you to include the mathematical expression to order the
result set by it.

Q3: Display the Rno, Name, Grade and Section on the basis of
calculation Marks*0.35 for those who got more than 70 marks.
Display the result section wise, descending order of the
calculation.

Ans : SELECT Rno, Name, Grade, Marks * 0.35 from data


WHERE Marks > 70
ORDER BY Section ASC, Marks * 0.35 DESC;
Q4: Display the Rno, Name, Grade and Section on the basis of
calculation Marks*0.35 for those who got more than 70 marks.
Display the result section wise, descending order of the
calculation. Name the column Marks*0.35 as Term-1

Ans : SELECT Rno, Name, Grade, Marks * 0.35 as Term1 from data
WHERE Marks > 70
ORDER BY Section ASC, Term1 DESC;
Sometimes you have a column where you want to arrange data as
per your own specified order. Then you can use the following
command.
Syntax : SELECT……ORDER BY FIELD(<column name>,<values
specifying order>);

Suppose you have a column Project with the following possible


values :
Pending, Submitted, Evaluated, Assigned.

Q5: Display the table Data with Project field having values in this
order : ‘Evaluated, ‘Pending’, Submitted’ and ‘Assigned’.

Ans : SELECT * from Data


ORDER BY FIELD(Project, ’Evaluated’, ’Pending’, ’Submitted’,
’Assigned’);

You might also like