You are on page 1of 11

MULTIROW FUNCTION

 Multi row functions are also known as group functions.


 Group functions operate on multiple rows and return one result for each group.
 Group functions are usually used after select keyword.
 Multiple group functions can be used in single select statement.
 Group functions ignore the

N no.of input == 1 output

==>min/max/count == work with number and character

==>sum/avg == work with number only

==>only one arguments

like,min(salary)...notlike,min(salary,employee_id)

Types of Multi-Row Functions are

 Maximum(Max)
 Minimum(MIN)
 Average(Avg)
 Sum 
 Count

Maximum(Max)

MAX ( ) Function in SQL:-

Returns the maximum value of a from the given data.

Syntax   :

Max(expression)

FOR EXAMPLE:-
SELECT MAX(SALARY) FROM EMPLOYEES;

Display max salary of whose department_id=20

EX: SELECT MAX(SALARY) FROM EMPLOYEES WHERE DEPARTMENT_ID=50;

MIN FUNCTION:

Min function is opposite to max function.

Query:

Select min(commission_pct), min(nvl(commission_pct,0)), min(hire_date), min(first_name) from


employees;
SUM FUNCTION:

It is exclusively used with numeric data.

Query:

Select sum(salary), sum(distinct salary) from employees

Query:

select employee_id,first_name,sum(salary) from employees

group by employee_id,first_name having sum(salary)>10000;


AVERAGE(AVG) FUNCTION:

avg  ( ) Function in SQL:-

It returns avg value of the given expression

Query: select avg(salary) from employees;

Count ( ) Function in SQL:


Count no of values present in a column

The COUNT() function returns the number of rows that matches a specified criterion.

COUNT() Syntax

SELECT COUNT(column_name)

FROM table_name
WHERE condition;

Query:

select count(salary) from employees where salary >15000;

Query:

select count(*) from employees;


The SQL GROUP BY Statement

The GROUP BY statement groups rows that have the same values into summary rows, like "find the
number of customers in each country".

The GROUP BY statement is often used with aggregate functions (COUNT


(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

GROUP BY Syntax

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

QUERY:

SELECT DEPARTMENT_ID, MAX(SALARY),MIN(SALARY),AVG(SALARY),SUM(SALARY) FROM


EMPLOYEES GROUP BY DEPARTMENT_ID;

--
--WISE KEYWORD USING MULTIROW FUNCTION

E.X:

DEPARTMENT WISE

COUNTRY WISE

CITY WISE

SELECT MAX(SALARY), FIRST_NAME FROM EMPLOYEES

THE ABOVE STATEMENT WAS WRONG BECAUSE OF YOU COULDN’T USE NON-AGGREGATE
FUNCTION IN AGGREGATE FUNCTION

MAX(SALARY) – AGGREGATE FUCNTION

FIRST_NAME – NON –AGGREGATE FUNCTION

SOME IMPORTANT QUERY:

1.FIND THE ERROR OF BELOW QUERY

SELECT DEPARTMENT_ID,AVG(SALARY) FROM EMPLOYEES;

CREATE TABLE F1

SNO NUMBER,
NAME VARCHAR2(25),

TAMIL NUMBER

);

INSERT INTO F1 VALUES (101,'SAKTHI',105);

INSERT INTO F1 VALUES (102,'KARTHI',106);

INSERT INTO F1 (SNO,NAME) VALUES (103,'SATHISH');

INSERT INTO F1 (SNO,NAME) VALUES (104,'DINESH');

INSERT INTO F1 VALUES (102,'KARTHI',NULL);


S.NO GROUP BY ORDER BY

Whereas Order by statement sort the


Group by statement is used to group the result-set either in ascending or in
1. rows that have the same value. descending order.

It may be allowed in CREATE VIEW While it does not use in CREATE VIEW
2. statement. statement.

In select statement, it is always used before While in select statement, it is always


3. the order by keyword. used after the group by keyword.

Whereas in order by statement,


Attribute cannot be in the group by attribute can be under aggregate
4. statement under aggregate function. function.

In group by clause, the tuples are grouped Whereas in order by clause, the result-
based on the similarity between the set is sorted based on ascending or
5. attribute values of tuples. descending order.

Group by controls the presentation of While order by clause controls the


6. tuples(rows). presentation of columns.

SR.NO
. WHERE Clause HAVING Clause

1. WHERE Clause is used to filter the records HAVING Clause is used to filter record
from the table based on the specified from the groups based on the specified
SR.NO
. WHERE Clause HAVING Clause

condition. condition.

WHERE Clause can be used without HAVING Clause cannot be used without
2. GROUP BY Clause GROUP BY Clause

WHERE Clause implements in row HAVING Clause implements in column


3. operations operation

WHERE Clause cannot contain aggregate HAVING Clause can contain aggregate
4. function function

WHERE Clause can be used with SELECT, HAVING Clause can only be used with
5. UPDATE, DELETE statement. SELECT statement.

WHERE Clause is used before GROUP BY HAVING Clause is used after GROUP BY
6. Clause Clause

WHERE Clause is used with single row HAVING Clause is used with multiple row
7. function like UPPER, LOWER etc. function like SUM, COUNT etc.

When we write the query to below method

Query:

select 1,2,3,8 from employees;


Query:

select null from dual;

Query:

select abc from dual;

Query:

select 'abc' from dual;

You might also like