You are on page 1of 2

CHAPTERNO 4 :GROUP FUNCTION :IS THE FUNCTION WHICH APPLIES ON SET OF ROWS AND GIVES ONE RESULT PER

SET 1)MIN 2)MAX 3)AVG 4)SUM 5)COUNT select sum(salary),max(salary),avg(salary),min(salary) from employees; select max(salary),min(salary) from employees; select max(hire_date),min(hire_date) from employees; select max(last_name),min(last_name) from employees; 1) COUNT FUNCTION: 1) COUNT FUNCTION: -------------------------syntax: select count(*) from employees; -> it results how many rows that the table contains. NOTE: it considers NULL + NOT NULL + DUPLICATE. WHEN COUNT FUNCTION WITH THE COLUMN: ---------------------------------------------------------syntax: select count(commission_pct) from employees; NOTE: -> it considers NOT NULL+DUPLICATE values. WHEN COUNT FUNCTION WITH DISTINCT FUNCTION: -----------------------------------------------------------------syntax : select count(distinct(commission_pct)) from employees; NOTE: -> it considers only NOT NULL+ UNIQUE values. NOTE: -------> all group functions except COUNT ignores null values. GROUP BY CLAUSE: -----------------------> it is used to gouped the data select max(salary) from employees; select max(salary) from employees group by department_id; select department_id,max(salary) from employees ; ORA-00937: not a single-group group function NOTE: -------

It is mandatory to specify the department_id column in the group by clause else it throws an error. select department_id,max(salary) from employees group by department_id; QUESTION: ------------1) i want to display the department_id,job_id for all employees who are having m ax(salary) > 10000 select department_id,job_id,max(Salary) from employees where max(salary) > 10000 group by department_id, job_id; OUTPUT: ----------> it throws an error because we are not using where clause in grouping functions. CORRECT ONE: -----------------select department_id,job_id,max(Salary) from employees group by department_id, job_id having max(salary) > 10000; by using having clause only you can get the result. -> because having clause only you may restrict the rows. USAGE OF WHERE CLAUSE: --------------------------------if you use where clause in group function with that particular condition only th e query will be executed. EXAMPLE: -----------select department_id,job_id,max(Salary) from employees where department_id in (20,50) group by department_id, job_id having max(salary) > 5000; NOTE: ------we can nest group functions uplo a level of maximun 2 only.

You might also like